skip to Main Content

Trying to add attachments to the letter.

add_action('wpcf7_before_send_mail', 'custom_attach_files_to_email');
function custom_attach_files_to_email($contact_form) {
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
        $uploaded_files = $submission->uploaded_files();
        $attachments = array();
        
        foreach ($uploaded_files as $uploaded_file) {
            $attachments[] = $uploaded_file;
        }
        
        $attachments[] = WP_CONTENT_DIR . '/uploads/files/file.txt';
        //$attachments[] = WP_CONTENT_DIR . '/uploads/files/file_2.txt';
        
        $mail = $contact_form->prop('mail');
        $mail['attachments'] = implode(',', $attachments);
        $contact_form->set_properties(array('mail' => $mail));
    }
}

This works only for ONE file. When i try to send two files (file.txt and file_2.txt) – the letter has no files at all

I need to send several files using code.

2

Answers


  1. Chosen as BEST ANSWER

    so, this code sends a letter with files. it's very important to set the correct path to file and files shouldn't be empty. also this code removes all attached files before (i didn't find no more ways)

    add_action('wpcf7_before_send_mail', 'custom_attach_files_to_email');
    function custom_attach_files_to_email($contact_form) {
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
        $mail = $contact_form->prop('mail');
        
        $f_1 = '/CORRECT_path_to_file/';
        $f_2 = '/CORRECT_path_to_file/';
    
        $mail['attachments'] = "
            $f_1
            $f_2
        ";
            
        $contact_form->set_properties(array('mail' => $mail));
    }
    

    }


  2. To properly attach multiple files in Contact Form 7, ensure that the attachments are set as an array in the mail properties. Try this code:

    add_action('wpcf7_before_send_mail', 'custom_attach_files_to_email');
    function custom_attach_files_to_email($contact_form) {
        $submission = WPCF7_Submission::get_instance();
        if ($submission) {
            $uploaded_files = $submission->uploaded_files();
            $attachments = array();
            
            foreach ($uploaded_files as $file) {
                $attachments[] = $file;
            }
            
            // Add additional file attachments
            $attachments[] = WP_CONTENT_DIR . '/uploads/files/file.txt';
            $attachments[] = WP_CONTENT_DIR . '/uploads/files/file_2.txt';
            
            $mail = $contact_form->prop('mail');
            if (isset($mail['attachments']) && is_array($mail['attachments'])) {
                $mail['attachments'] = array_merge($mail['attachments'], $attachments);
            } else {
                $mail['attachments'] = $attachments;
            }
            
            $contact_form->set_properties(array('mail' => $mail));
        }
    }

    Make sure the file paths are correct and that the files exist

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search