简体   繁体   中英

Attaching a pdf with wp_mail function in wordpress

I am trying to attach a pdf file created using tcpdf library in order to process a reservation form data. The mail can be sent but, the dynamically created pdf file can't be attached. Any help in this regard is highly appreciated.

$pdfname ='pdfname';
$PdfName = $_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/themename/folder/'.$pdfname.'.pdf';
echo $pdf->Output($PdfName, 'F');
echo $pdf->Output($pdfname.'.pdf', 'D');


/***** After creating pdf you will use below code****/
$email       = $ToMailAdrs;
$to          = "<$email>";

$subject     = "PDF Attachment";

$separator = md5(time());
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = 'From: Name <no-reply@test.in>' . "\r\n";
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; // see below 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
// message
$msg .= "<p style='white-space: pre-wrap;'>".$email_message."</p>".$eol.$eol;
$attachment = array($PdfFileUrl);
// send message
wp_mail($to, $subject, $msg, $headers,$attachment); 
header("Location:?showpage=invoice");
exit;

This code comes from one of my custom plugin for WooCommerce.

$pdf_folder = WP_PLUGIN_DIR .'/my-plugin/pdf/';
$attachment = $pdf_folder . 'my_filename.pdf';
$headers[] = 'Cc:  <'.get_option('recipient_cc').'>'.',<'.get_option('admin_email').'>';

$success = wp_mail( get_option( 'recipient' ), get_option( 'email_title' ), get_option('email_content'), $headers, $attachment );

if ( $success ) {
  //do something and return success
} else {
  //do something return false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM