Home » Wordpress » How to send pdf file attachment to user using contact form 7

Here is code to send pdf file attachment to user using contact form 7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function send_pdf( $cf7 ) {
    $id = $cf7->id();
    if ($id==1076){
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();
        $tdsfileurl = $posted_data['tdsfile'];
        $tdsproductname = $posted_data['productname'];
        $explode = explode("/", $tdsfileurl);
        $end = '';
        $begin = '';
        if(count($explode) > 0){
            $end = array_pop($explode); // removes the last element, and returns it
            if(count($explode) > 0){
                $begin = implode('/', $explode); // glue the remaining pieces back together
            }
        }
        $t=time();
        $hostname = $_SERVER['HTTP_HOST'];
        $newfile2 = explode($hostname, $begin);
        $newfile = $_SERVER['DOCUMENT_ROOT'] . '' .$newfile2['1'].'/' .$tdsproductname.'_TDS_'.$t.'.pdf';
        error_log('$tdsfileurl: ' . $tdsfileurl);
        error_log('$newfile: ' . $newfile);
        if ( copy($tdsfileurl, $newfile) ) {
            error_log('File copied');
            $submission->add_uploaded_file('pdf', $newfile);
        }else{
            error_log('Not able to attche file');
        }
 
 
    }
}
add_action('wpcf7_before_send_mail','send_pdf');

1076 is contact form 7 ID and tdsfile form field is having PDF url form local server (http://localhost:90/pidi/wp-content/uploads/2015/09/test.pdf) which i want to send as attachment.

Array ( [0] => Wordpress, wordpress hacks )