I'm trying to attach pdf files to an e-mail manually and I did some research on the topic and found this code:


function send_pdf( $cf7 ) {
    $id = $cf7->id();
    if ($id==2399){
        $submission = WPCF7_Submission::get_instance();
        $submission->add_uploaded_file('pdf', get_template_directory().'/pdf/test.pdf');

The strange thing is that after the email is sent, the file and the folder are deleted but nothing is attached. I'm using a theme and a child theme, and the folder is put in the main theme directory. Anyone ideas?


I've found what's been missing in the code. You have to add this also:

add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );

function mycustom_wpcf7_mail_components( $components ) {
    $components['attachments'][] = get_template_directory().'/pdf/test.pdf';

    return $components;

Now everything is working fine and the file is attached to email without the need to add fields in the contact form.

  • I tried your code and didnt work for me. I placed it in Functions.php – Gangesh Apr 27 '18 at 16:54
  • I wnats to send this attchment only in user email but not idea how to manage condition here? – ravi patel Feb 8 '19 at 6:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.