• Resolved Pierre_02

    (@pierre_02)


    Hi.
    I use this function found somewhere on the web and it works fine.

    function send_pdf( $cf7 ) {
        $id = $cf7->id();
    	if ($id==742){
            $submission = WPCF7_Submission::get_instance();
            $submission->add_uploaded_file('pdf', '/path_to_my_file.pdf');
    	}
    }
    add_action('wpcf7_before_send_mail','send_pdf', 100);

    The problem is that after form submition you call $this->remove_uploaded_files();in includes/submission.php that delete my file :(.

    Is there any other possiblity to send dynamically a file to a user without delete it after sending it?

    TIA.

    Amicably,

    Pierre.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Thread Starter Pierre_02

    (@pierre_02)

    Hi Takayuki :).

    Thanks for the quick reply, but I’d like to be able to attach a file dynamically, not directly in my form (I know how to do it with your cool plugin ;)).
    Forget, the id of the form in my function it was just for test purposes ;).

    TIA,

    Amicably,

    Pierre.

    This works fine for you?
    $submission->add_uploaded_file('pdf', '/path_to_my_file.pdf');

    I cant get any attachments to add to my email.

    About the deletion, I used this to copy it to a tmp folder. The tmp folder is also deleted hence the mkdir!

    // get attachment from attachment-id in form
    $attachment = get_attached_file($posted_data['attachment-id']);
    $attachment_filename = basename($attachment);
    // Create a temp copy of the file
    $upload_dir = wp_upload_dir();
    $temp_file = $upload_dir['basedir'].'/tmp/'.$attachment_filename;
    
    // Copy file to temp location (because Contact Form 7 deletes it later!)
    mkdir($upload_dir['basedir'].'/tmp/');
    copy($attachment,$temp_file);
    
    // Attach to email
    $submission->add_uploaded_file('pdf', $temp_file);

    My problem is its not attached to the email

    Ah I solved my issue:
    https://gist.github.com/ianhampton/eee8f8ea1a6280db8bbae3944c07a451

    The only issue with this is its an inline attachment in the body, so my mail client is not showing there are any attachments in it. Hope this is not a problem for any email clients.

    So we dont need to copy a temp file now as we dont need to use add_uploaded_file()

    // Get attachment path
    $attachment = get_attached_file($posted_data['attachment-id']);
    
    // do some replacements in the cf7 email body
    $mail = $wpcf7->prop('mail');
    
    $mail['attachments'] = $attachment;
    		
     $wpcf7->set_properties(array(
    	    "mail" => $mail
    ));
    • This reply was modified 7 years, 9 months ago by amityweb.
    • This reply was modified 7 years, 9 months ago by amityweb.
    Thread Starter Pierre_02

    (@pierre_02)

    Thanks for the help amityweb ?? !

    I have adapted a little your code and now I’ve got something that works fine ?? !!

    Have a great day amityweb ?? !!

    Amicably,

    Pierre.

    God afternoon,

    I hade added a “file attachment” field in my form, works good, but only for once. Than the file stays in “tmp” and I can’t send another file.

    The “tmp_file” cleaner isn’t working I suppose.

    I need some help…

    Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to avoid file deletion after sending with add_uploaded_file’ is closed to new replies.