Reputation: 97
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.
Upvotes: 6
Views: 11322
Reputation: 9257
$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 <[email protected]>' . "\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;
Upvotes: 4
Reputation: 1164
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
}
Upvotes: 1