WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form 7
Sending Attachments, Without User Upload (33 posts)

  1. Wuei
    Member
    Posted 3 years ago #

    Currently, the contact form sends me an email, as well as sends the user a confirmation email.

    Is it possible to include an attachment to the user? Specifically, if I were running a charity for an event, and they registered using Contact Form 7, would it be possible to send a pledge form and an informational pdf to them with the confirmation email?

    Note: This is unrelated to the file attachment tags, I am not trying to get people to upload an attachment to send to me, I am trying to send an attachment back to them.

    Thank you for your time, everyone! I hope to get this resolved soon.

    http://wordpress.org/extend/plugins/contact-form-7/

    Edit: Etiquette.

  2. dco22
    Member
    Posted 3 years ago #

    I have the same problem. did you find a solution?

  3. gargoyle3113
    Member
    Posted 2 years ago #

    I need a solution to this, as well. Does anyone know of another plugin we can use if this doesn't work? CForms II doesn't seem to support this, either.

  4. marshalp
    Member
    Posted 2 years ago #

    I'd really like a solution for this, can anyone help?

    Thanks
    Phil.

  5. thealchemist
    Member
    Posted 2 years ago #

    Count me in! I want to be able to use the autoresponder to send an attachment.

  6. topper007
    Member
    Posted 2 years ago #

    Count me in too ! This would be excellent !

    Did anyone found an alternative solution to this problem?

  7. thealchemist
    Member
    Posted 2 years ago #

    What's interesting, is the contact form page admin includes a "File Attachements" field in the Mail 2 area, logically leading us to believe that simply adding the URL to the attachment in the field it would automatically attach said item.

    If there are other tools available I guess I will have to use them as I can't wait.

  8. Daniele Raimondi
    Member
    Posted 2 years ago #

    I'm in the exact same situation. I have a form where a user can request a discount coupon, so I need to create a coupon (pdf file) on the fly with a unique serial number in it, attach the coupon and send it to the user via email.

    I've found a working solution, so here it is:

    1) I've created a form in the backend and setted all required field in MAIL section.

    2) In the file attachement field on MAIL I've inserted
    [coupon]
    N.B: it's not necessary to really add this field to the form, just in the attchment field.

    3) I Hooked in cf7 via 'wpcf7_before_send_mail' action

    add_action( 'wpcf7_before_send_mail', 'create_unique_coupon_and_send_it' );
    function create_unique_coupon_and_send_it( $cf7 ) {
    	//check if this is the right form
    	if ($cf7->id==741){
     		$uploads = wp_upload_dir();
    		//define some constants
    		define ('COUPON_UPLOAD_PATH',$uploads['path'].'/coupons');
    		// ...
    		// ...
    		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!
    
    		$cf7->uploaded_files = array ( 'coupon' => COUPON_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;
    	}
    }

    Note that:
    1) you have to change the form id as needed (or avoid the check if you have only one form on your site)
    2) CF7 unlink (delete) the attached file after mail is sent for security reasons, so make a copy of it if you have to send the same file over and over.
    3) make sure the key of the item in this associative array
    array ( 'coupon' => COUPON_UPLOAD_PATH.'/'.$pdf_filename );
    matches the name inserted in the form settings, in the attachment file field (see point 2) above the code)
    4) If you are using (like me) the "to-db-extension" plugin, remember to add the action with a priority level lower than 10:
    add_action( 'wpcf7_before_send_mail', 'create_unique_coupon_and_send_it', 5 );

    to execute your code before the DB plugin gets form data and save them in the db. In the code example above, I can add a field to every record in the db, just altering the $cf7->posted_data array:

    //Add coupon file name in the database
    $cf7->posted_data['coupon_file']=$pdf_filename;

    Hope it can be useful.

  9. gangesh
    Member
    Posted 2 years ago #

    @mad_max it works very well! :) Thank you!

  10. VeeBeeGlobal
    Member
    Posted 2 years ago #

    @mad_max I tried this, but it wont work.

    I put this code in my functions.php.

    Am on wp3.2.1 and the lastest version of CF7 3.x

    V

  11. Daniele Raimondi
    Member
    Posted 2 years ago #

    @VeeBeeGlobal
    1) Have you changed the $cf7->id check using YOUR form-id in code?
    2) Do you get some errors?
    3) Are you sure the file your trying to attach exists and that you are pointing to the right folder?
    2) can you paste the code you are using on pastebin or similar?

    bye

  12. Daniele Raimondi
    Member
    Posted 2 years ago #

    oh, another tip to make some debugging with cf7 forms without getting crazy: use Firefox+Firebug
    HOW
    1) add FirePHP firebug extension to firebug:
    https://addons.mozilla.org/en-US/firefox/addon/6149/
    2) install this wp-firePHP plugin
    http://inchoo.net/wordpress/wordpress-firephp-plugin/
    (download version 0.2 and unzip to your wp plugins folder then activate it)
    3) now use
    fb($whathever_var_or_string);
    to output debugging info to firebug console, even structured one like arrays or objects. For example:
    fb($cf7);
    gives you a print_r like output with all data setted ans used by cf7.

  13. marshalp
    Member
    Posted 2 years ago #

    @mad_max

    I'm a bit of a WordPress novice, I appreciate your time on this but I have a question.

    Where do I insert the code you have you have created?

    Thanks

  14. Daniele Raimondi
    Member
    Posted 2 years ago #

    You have to insert the code in the functions.php file in your theme folder. If your theme doesn't have one, create it and copy&paste the code in it (in this case, remeber to add open and close php tags to the code).

  15. gangesh
    Member
    Posted 2 years ago #

    @mad_max

    This works great overall!

    I have some issues though, like if 'CF7 unlink (delete) the attached file after sending mail' then how to retain the file by just copying it? Is there some way we can stop Cf7 from unlink the file?

    Anyways its been great help till now.
    Thanks again.

  16. Daniele Raimondi
    Member
    Posted 2 years ago #

    @gangesh
    I think there's no way to "persuade" cf7 not to unlink files after mails are sent. This is a security feature as it avoid someone can upload a scritp throug a form and then execute it on your server.
    But you can use the php copy command to make a copy of your file every time you send an email via cf7.

    Suppose you have uploaded a file in a subfolder of your WP uploads folder named master.pdf, and you wanna send a copy of it every time someone fills a form on your site.

    Just add some lines of code to my function above like this:

    add_action( 'wpcf7_before_send_mail', 'create_unique_coupon_and_send_it' );
    function create_unique_coupon_and_send_it( $cf7 ) {
    	//check if this is the right form
    	if ($cf7->id==741){
     		$uploads = wp_upload_dir();
    		//define some constants
    		define ('MY_FILE_PATH',$uploads['path'].'/myfile/');
    
    		//set filenames
    		$master_copy=MY_FILE_PATH.'master.pdf';
    		$copy_to_send=MY-FILE_PATH.'attachment.pdf';
    
    		if ($cf7->mail['use_html']==true)
    			$nl="<br/>";
    		else
    			$nl="\n";
    
    		//make a copy of the master file and attach it
    		if ( copy( $master_copy, $copy_to_send ) ){
    			//Let'go to the file attachment!
    			$cf7->uploaded_files = array ( 'coupon' => $copy_to_send );
    		}
    		//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;
    	}
    }
  17. gangesh
    Member
    Posted 2 years ago #

    @mad_max

    Thanks for such elaborate explanation. :)
    Works great!

  18. tari72
    Member
    Posted 2 years ago #

    @mad_max

    I an trying to attach a file hosted on a different site that I own.
    Can't make it work using your code.
    Can you help?

  19. Daniele Raimondi
    Member
    Posted 2 years ago #

    @tari72

    php copy should support remote files, so just change the path to your master file:

    $master_copy='http://www.mysite.it/subfolder/private/my_remote_file.pdf';

    IF IT DOESN'T WORK
    here you can find a good tutorial with 4 solutions to read a remote file and grab its content.

    Once you have putted the content of the remote file in to a variable ($content in the tutorial), just write it to a file with a line of code like this:

    file_put_contents($file, $content);

    Suppose you decide to use the second method described in the tutorial (remember to verify that your hosting service has allow_url_fopen setted to true in php.ini), you can integrate it in my code like this:

    add_action( 'wpcf7_before_send_mail', 'create_unique_coupon_and_send_it' );
    function create_unique_coupon_and_send_it( $cf7 ) {
    	//check if this is the right form
    	if ($cf7->id==741){
     		$uploads = wp_upload_dir();
    		//define some constants
    		define ('MY_FILE_PATH',$uploads['path'].'/myfile/');
    
    		//set filenames
    		$master_copy='http://www.mysite.it/subfolder/private/my_remote_file.pdf';
    		$copy_to_send=MY-FILE_PATH.'attachment.pdf';
    
    		if ($cf7->mail['use_html']==true)
    			$nl="<br/>";
    		else
    			$nl="\n";
    		// get the remote file
    		$content = file_get_contents('$master_copy');
    		if ($content !== false) {
    			//make a copy of the master file and attach it
    			if ( file_put_contents($copy_to_send, $content) !== false ){
    				//Let'go to the file attachment!
    				$cf7->uploaded_files = array ( 'coupon' => $copy_to_send );
    			}
    		}
    		//Let'go to the file attachment!
    		$cf7->uploaded_files = array ( 'coupon' => $copy_to_send );
    
    		//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;
    	}
    }
  20. tari72
    Member
    Posted 2 years ago #

    hi mad_max

    can't make it work.
    would you be kind enough to write a simplified code.
    without the upload path minimum if(s) as basic as possible.
    once i can attach a file (not upload) i guess i'll figure every thing else.

    Thanks a lot for your time.
    Really appreciate it.

  21. palgrave
    Member
    Posted 2 years ago #

    Dayum, just can't get it working. Everything works for me apart from the attachment, I get the email about the fish and the form submits nicely, but no attachment.

    I'm using the method to send the same file each time.

    Has anything changed with the latest version of cf7?

  22. Daniele Raimondi
    Member
    Posted 2 years ago #

    @tari72: Do you have some errors? What do you mean with "can't make it work."
    Can you make more clear which is the obscure part in my code, so I can help with it?

    @palgrave: I don't think so. I'm using the last CF7 version and it works. Which version of WP are you using? Have you added a field name in the file attachement field on MAIL section?

  23. palgrave
    Member
    Posted 2 years ago #

    @mad_max First of all thanks for the quick reply. That sets you apart :)

    I'm using WP 3.3.1 and I just used [coupon] in the file attachment field.

  24. Daniele Raimondi
    Member
    Posted 2 years ago #

    mmmh, I'm using this file attachment method on a 3.2.1 WP version but I think the problem is not due to the your newer WP version. Can you attach the code you are using?

  25. palgrave
    Member
    Posted 2 years ago #

    I actually got it working in the meantime using $recipient_email = $cf7->posted_data["your-email"] and then a regular mail function.

    Thank you very much for your help, and for posting the only thing anywhere on the internet that signposted it for me.

  26. Daniele Raimondi
    Member
    Posted 2 years ago #

    You are welcome! Happy to hear you have resolved your issue.

  27. tari72
    Member
    Posted 2 years ago #

    took me a while to answer but i get no errors just like palgrave.
    every thing seems to work but the attachment thing.
    mad max - thanks for the time and effort.
    palgrave - can you attach the code you used?
    what is a regular mail function.
    i got to make this work.

  28. wardstephen
    Member
    Posted 2 years ago #

    Mad Max, thanks so much for your excellent posts on this issue, I got it working thanks to you.

  29. tari72
    Member
    Posted 2 years ago #

    @mad_max,

    First of Thank you. I finally go it to work.
    I'm new to WP PHP and the web.
    problem was I tried to attach a file and gave a wrong path.

    again thanks,
    your solution makes CF7 a lot better and complete solution

    Tari72

  30. sensei888
    Member
    Posted 2 years ago #

    Can someone assist me please. I wish to use the page slug as the name of the PDF file. I tried to assign the slug as below:

    global $post
    $slug = $post->post_name;
    $master_copy=MY_FILE_PATH.$slug.'.pdf';

    But it seems this doesn't work. I'm a bit lost so any direction would be much appreciated.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic