0

I want to export the form data and send it as an attachment. In this case, can i rewrite the file (/uploads/2020/08/sample.csv) with the form data and send it as an attachment.

right now, it is just a blank attachment when i receive it in my email

Here is my code in function.php, Please help me:

add_action( 'wpcf7_before_send_mail', 'add_form_as_attachment', 10, 1 ); 

function add_form_as_attachment(&$WPCF7_ContactForm) {
$contact_form = WPCF7_ContactForm::get_current();
$Fname = $formdata['firstName'];
$Lname = $formdata['lastName'];
$email = $formdata['email'];

$list = array (
array( 'First Name:',$Fname),
array( 'Last Name:', $Lname),
array( 'Email:',  $email),

);

$fp = fopen( site_url() . 'uploads/2020/08/sample.csv', 'w');

foreach ($list as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);

}

2 Answers 2

1

There are a few things omitted in the above solution, like defining the most params of wp_mail. Also I didn't want to send yet another concurrent email, but instead attach the csv directly to the mail that wpcf7 sends. I think this is the cleaner solution.

Here's my take:

add_filter( 'wpcf7_before_send_mail', 'add_form_as_attachment', 10, 3 );
function add_form_as_attachment( $contact_form, $abort, $submission ) {

    $form_id = $contact_form->id();

    //Check for selected form id and actual submission
    if ($form_id == 123 && $submission){

        $posted_data = $submission->get_posted_data() ;
        // Check again for posted data
        if ( empty ($posted_data)){
            return; 
        }

        //Get your form fields
        $firstname = $posted_data['your-name'];
        $lastname = $posted_data['your-lastname'];
        $email = $posted_data['your-email'];

        $list = array (
        array( 'First Name:',$firstname),
        array( 'Last Name:', $lastname),
        array( 'Email:',  $email),

        );
        // Save file path separately 
        $upload_dir = wp_upload_dir(); 
        $file_path = $upload_dir['basedir'].'/yourtempfolder/sample.csv'; // check File permission for writing content and remember to protect this folder from the public with htaccess for example

        $fp = fopen( $file_path , 'w'); //overwrites file at each submission
        foreach ($list as $fields) {
            fputcsv($fp, $fields);
        }
        fclose($fp);

        // Modification of mail(1) start here
        $properties = $contact_form->get_properties();
        $properties['mail']['attachments'] = $file_path;
        $contact_form->set_properties($properties);

        return $contact_form;
    }//endif ID && submission
}
0

I solved mine problem with this code. Hope it could help others too -

add_action( 'wpcf7_before_send_mail', 'add_form_as_attachment', 10, 1 ); 

function add_form_as_attachment(&$WPCF7_ContactForm) {
$contact_form = WPCF7_ContactForm::get_current();
$Fname = $formdata['firstName'];
$Lname = $formdata['lastName'];
$email = $formdata['email'];

$list = array (
array( 'First Name:',$Fname),
array( 'Last Name:', $Lname),
array( 'Email:',  $email),

);
// Save file path separately 
$file_path = site_url() . 'uploads/2020/08/sample.csv'; // check File permission for writing content
$fp = fopen( $file_path , 'w');

foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
//MAIL starts here 
$attachments = array( $file_path );
$headers = 'From: name <[email protected]>' . "\r\n";
 
wp_mail($to, $subject, $message,$headers, $attachments);
// You are done 
}
8
  • thank you for the reply.. by the way where did you upload the 'uploads/2020/08/sample.csv' ? in media library ?
    – Yucaree
    Commented Aug 26, 2020 at 6:27
  • I have modified and used your code here. Well, it is better to upload things in Upload folder of WP. you can use following code also at place of $file_path = site_url() . 'uploads/2020/08/sample.csv'; You need to create a "temp" folder in "uploads" folder in wp-content .. $upload_dir = wp_upload_dir(); $file_path = $upload_dir['basedir'].'/temp/sample.csv';
    – amitdutt24
    Commented Aug 28, 2020 at 13:20
  • can i rename the attachment before sending it? something like "First_Name.csv"?
    – Yucaree
    Commented Aug 31, 2020 at 5:16
  • Off course you can keep name as per your requirement. As per my code "sample.csv" is a physical blank CSV file kept at the location mentioned. In same way you have to create a blank file and name whatever you want.
    – amitdutt24
    Commented Sep 1, 2020 at 6:38
  • what about adding the other fields on another sheet? but same file.. inside sample.csv there is sheet1 and sheet2
    – Yucaree
    Commented Sep 3, 2020 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.