Contact form 7 send email with auto attachment

I wanted to make a contact form in wordpress, that user fill with simple data like the username and email address so the email send to him with our price list in pdf format

I used contact 7 plugin for that with adjustment in the code so that instead you must make the user send the attachment file in the form .. you will have the pdf file in your ( uploads ) directory to send it to the user that want it

first make the form like the following

1-15-2013 10-07-29 PM

then go to the functions.php in your theme folder and write the following code


add_action( 'wpcf7_before_send_mail', 'send_attachment_file' );
 function send_attachment_file($cf7)
 {
 //check if this is the right form

if ($cf7->id==872)        // This is the form ID
 {
 $uploads = wp_upload_dir();     // will output the array of path,url,subdir,basedir,baseurl,error -- what we need is the path = '/home/euroling/public_html/beta/wp-content/uploads/2013/01'
 //define some constants
 define ('PRICE_UPLOAD_PATH',$uploads['path'].'/price');     // create price folder in the path
 // ...
 // ...
 if ($cf7->mail['use_html']==true)
 $nl="<br/>";
 else
 $nl="\n";
 //I omitted all the stuff used to create
 //the pdf file, you have just to know that
 //$pdf_filename contains the filename to attach
 //Let'go to the file attachment!

// $pdf_filename with the extenstion not just the filename
 $pdf_filename = "testnew.pdf";
 $cf7->uploaded_files = array( 'file_upload' => PRICE_UPLOAD_PATH .'/'.$pdf_filename );

//append some text to the outgoing email
 $message=$nl.$nl.'Blah blah blah.....'.$nl;
 $message.='So Long, and Thanks for All the Fish!'.$nl;
 $cf7->mail_2['body'].=$message;
 }
}

AND ..

1-15-2013 10-19-51 PM

for reference :

http://wordpress.org/support/topic/contact-form-7-sending-attachments-without-user-upload?replies=33

Advertisements
Leave a comment

4 Comments

  1. Hi, nice example, Now i have a slightly complex situation here, I have 7 attachments and one will go in autoresponder based on a selection of a field in the form. I need to send pdf for the program visitor will select from a combo field [program] of the form. I guess if we can somehow get [program] field into a variable we can use switch case or if else to select attachments but the question is how to get [program] into a variable.

    Reply
  2. tiku

     /  25/03/2015

    Not Work For Me

    Reply
  3. tiku

     /  25/03/2015

    good its help me

    Reply
  4. tiku

     /  10/03/2015

    add_action( ‘wpcf7_before_send_mail’, ‘send_attachment_file’ );
    function send_attachment_file($cf7)
    {
    //check if this is the right form

    if ($cf7->id==145) // This is the form ID
    {
    $uploads = wp_upload_dir(); // will output the array of path,url,subdir,basedir,baseurl,error — what we need is the path = ‘/home/euroling/public_html/beta/wp-content/uploads/2013/01’
    //define some constants
    define (‘PRICE_UPLOAD_PATH’,$uploads[‘baseurl’].’/2015/02/Voorbeeld_Word_Losgesneden.pdf’); // create price folder in the path

    // …
    // …
    if ($cf7->mail[‘use_html’]==true)
    $nl=””;
    else
    $nl=”\n”;
    //I omitted all the stuff used to create
    //the pdf file, you have just to know that
    //$pdf_filename contains the filename to attach
    //Let’go to the file attachment!

    // $pdf_filename with the extenstion not just the filename
    $pdf_filename = “testnew.pdf”;
    $cf7->uploaded_files = array( ‘file_upload’ => PRICE_UPLOAD_PATH .’/’.$pdf_filename );

    //append some text to the outgoing email
    $message=$nl.$nl.’Blah blah blah…..’.$nl;
    $message.=’So Long, and Thanks for All the Fish!’.$nl;
    $cf7->mail_2[‘body’].=$message;
    }
    }

    this is my code i dont know where i make mistake.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: