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.

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 )