Replies: 0
I wrote this tid-bit of code as an attempt to grab the data from a timesheet form I created with contact form 7, save that data into a csv formatted the way I want, and then send that over to email specified in the “Mail” section of the Contact Form. Unfortunately, I’ve ran into 2 problems that I can’t seem to wrap my head around.
Problem 1: Can seem to get save the csv document into the directory I defined. Or save at all for that matter. Here’s my code:
<?php
add_action( 'wpcf7_before_send_mail', 'add_form_as_attachment', 10, 1 );
function add_form_as_attachment(&$WPCF7_ContactForm) {
$current_user = wp_get_current_user();
$pay_rate = get_user_meta( $current_user->ID, 'pay-rate', true );
$list = array (
array( 'Employee Name:', $current_user->display_name),
array( 'Selected Week:', $pay_rate),
array( 'Weekday', 'Job Number', 'Labor Desc', 'Labor Code', 'Total Hours', 'Break Hours'),
array(
'Sunday',
$WPCF7_ContactForm->posted_data['job-number-sun'],
$WPCF7_ContactForm->posted_data['labor-desc-sun'],
$WPCF7_ContactForm->posted_data['labor-code-sun'],
$WPCF7_ContactForm->posted_data['total-hours-sun'],
$WPCF7_ContactForm->posted_data['break-hours-sun']
),array(
'Monday',
$WPCF7_ContactForm->posted_data['job-number-mon'],
$WPCF7_ContactForm->posted_data['labor-desc-mon'],
$WPCF7_ContactForm->posted_data['labor-code-mon'],
$WPCF7_ContactForm->posted_data['total-hours-mon'],
$WPCF7_ContactForm->posted_data['break-hours-mon']
),array(
'Tuesday',
$WPCF7_ContactForm->posted_data['job-number-tue'],
$WPCF7_ContactForm->posted_data['labor-desc-tue'],
$WPCF7_ContactForm->posted_data['labor-code-tue'],
$WPCF7_ContactForm->posted_data['total-hours-tue'],
$WPCF7_ContactForm->posted_data['break-hours-tue']
),array(
'Wednesday',
$WPCF7_ContactForm->posted_data['job-number-wed'],
$WPCF7_ContactForm->posted_data['labor-desc-wed'],
$WPCF7_ContactForm->posted_data['labor-code-wed'],
$WPCF7_ContactForm->posted_data['total-hours-wed'],
$WPCF7_ContactForm->posted_data['break-hours-wed']
),array(
'Thursday',
$WPCF7_ContactForm->posted_data['job-number-thu'],
$WPCF7_ContactForm->posted_data['labor-desc-thu'],
$WPCF7_ContactForm->posted_data['labor-code-thu'],
$WPCF7_ContactForm->posted_data['total-hours-thu'],
$WPCF7_ContactForm->posted_data['break-hours-thu']
),array(
'Friday',
$WPCF7_ContactForm->posted_data['job-number-fri'],
$WPCF7_ContactForm->posted_data['labor-desc-fri'],
$WPCF7_ContactForm->posted_data['labor-code-fri'],
$WPCF7_ContactForm->posted_data['total-hours-fri'],
$WPCF7_ContactForm->posted_data['break-hours-fri']
),array(
'Saturday',
$WPCF7_ContactForm->posted_data['job-number-sat'],
$WPCF7_ContactForm->posted_data['labor-desc-sat'],
$WPCF7_ContactForm->posted_data['labor-code-sat'],
$WPCF7_ContactForm->posted_data['total-hours-sat'],
$WPCF7_ContactForm->posted_data['break-hours-sat']
),array(
'Travel Mileage (Miles):',
$WPCF7_ContactForm->posted_data['mileage'],
'Travel Mileage (MPG):',
$WPCF7_ContactForm->posted_data['mpg']
),array(
'Remarks:',
$WPCF7_ContactForm->posted_data['remarks']
)
);
$fp = fopen( site_url() . '/wp-content/uploads/timesheets/timesheet.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
Problem 2: I can’t seem to define this file in the “Mail” section of Contact Form 7. I get this error, and obviously the file doesn’t exist yet, but how else can I define where contact form should get the attachment, or better yet when?
Here’s a screenshot:

Any help will be definitely appreciated. Been banging my head against the wall trying to figure this one out for a while.