Dynamic File Attachments
-
Hello,
I am trying to figure out how to dynamically attach files to mail2 based on checkboxes that are selected by the user when filling out the contact form.
Essentially if I have 2 checkboxes “File 1” and “File 2” if the user only checks “File 1” I want to email them and attach only File 1 to the email.
Been banging my head for hours trying to figure out hooks available in Conact Form 7 using filters like wpcf7_mail_components and wpcf7_before_send_mail
Example 1:
add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components'); function mycustom_wpcf7_mail_components($components){ define ('ATTACHMENT_PATH',$uploads['path'].'/attachments'); $pdf_filename = 'file1.pdf'; $components['attachments'] = array(ATTACHMENT_PATH.'/'.$pdf_filename); return $components; }
Example 2:
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='https://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; } }
Any help or direction is appreciated.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Dynamic File Attachments’ is closed to new replies.