在wordpress中使用wp_mail功能附加pdf


Attaching a pdf with wp_mail function in wordpress

我试图附加使用tcpdf库创建的pdf文件,以便处理预订表单数据。邮件可以发送,但是动态创建的pdf文件不能附加。

$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;

这段代码来自我的一个自定义插件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
}