I have successfully attached a generated pdf file to my email using this method but I can’t get the html part of the message to show up in the email. It comes through with the attachment and no message body. Do I need to do something differently? I am using wp_mail to send the message.
$pdf_file_name = "test.pdf";
// set the boundary
$boundary = "b_".strtoupper(md5(uniqid(time())));
// text part of message
$customer_message = '';
$customer_headers = "MIME-Version: 1.0n";
$customer_headers .= "Content-Type: multipart/mixed; boundary=$boundaryn";
$customer_headers .= 'From: Steel Structural <'.get_option('admin_email').'>' . "rn";
$customer_message .= "n--$boundaryn";
$customer_message .= "Content-Type: text/html;"."n";
$customer_message .= '<html>';
$customer_message .= '<body style="background: black; width: 100%;">';
$customer_message .= '<div style="font-family: sans-serif; text-align: center; background: white; width: 90%; max-width: 600px; margin: 0 auto; border-top: 4px solid #f1592a; border-bottom: 4px solid #f1592a;">';
$customer_message .= '<div style="width: 100%; position: relative; background: #0A0A0A;"><img style="margin: 0 auto;" src="http://thinkboxprojects.com/steel-structural/wp-content/plugins/steel-structural-override/images/email-logo.png"></div>';
$customer_message .= '<h2 style="color: black; font-style: italic; padding-top: 20px; text-transform: uppercase;">'.get_bloginfo('name').'</h2>';
$customer_message .= '<h2 style="color: black; font-style: italic; text-transform: uppercase; width: 300px; margin: 0 auto 30px auto; padding-bottom: 30px; border-bottom: 4px solid #f1592a;">Submittal Request</h2>';
$customer_message .= "<p style='padding: 10px 30px; margin: 0 auto; text-align: left; max-width: 500px;'>Thank you. We have received your submission and will contact you shortly. Below are the details of your submission.</p>";
$customer_message .= '<div style="max-width: 500px; text-align: left; margin: 30px auto;">';
if ( !empty($_SESSION['tbc']['submittal']) )
{
$customer_message .= "Cart Contents: <br /><br />";
foreach ( $_SESSION['tbc']['submittal'] as $product_id => $item )
{
$customer_message .= $item['title'].' x '.$item['qty']."<br />";
}
$customer_message .= '<hr style="background: #f1592a; width: 300px; margin: 15px auto; height: 4px;"></hr>';
}
$customer_message .= "Project Information: <br /><br />";
if ( !empty($_POST['project_name']) ) $customer_message .= 'Project Name: '.$_POST['project_name']."<br />";
if ( !empty($_POST['project_number']) ) $customer_message .= 'Project #: '.$_POST['project_number']."<br />";
if ( !empty($_POST['project_address']) ) $customer_message .= 'Project Address: '.$_POST['project_address']."<br />";
if ( !empty($_POST['project_city']) ) $customer_message .= 'Project City: '.$_POST['project_city']."<br />";
if ( !empty($_POST['project_state']) ) $customer_message .= 'Project State: '.$_POST['project_state']."<br />";
if ( !empty($_POST['project_zip']) ) $customer_message .= 'Project Zip: '.$_POST['project_zip']."<br />";
if ( !empty($_POST['project_start_date']) ) $customer_message .= 'Project Start Date: '.$_POST['project_start_date']."<br />";
if ( !empty($_POST['project_general_contractor']) ) $customer_message .= 'Project General Contractor: '.$_POST['project_general_contractor']."<br />";
if ( !empty($_POST['project_architectural_firm']) ) $customer_message .= 'Project Architectural Firm: '.$_POST['project_architectural_firm']."<br />";
$customer_message .= '<hr style="background: #f1592a; width: 300px; margin: 30px auto; height: 4px;"></hr>';
$customer_message .= "Contractor Information: <br /><br />";
if ( !empty($_POST['contractor_name']) ) $customer_message .= 'Contractor Name: '.$_POST['contractor_name']."<br />";
if ( !empty($_POST['contractor_address']) ) $customer_message .= 'Contractor Address: '.$_POST['contractor_address']."<br />";
if ( !empty($_POST['contractor_city']) ) $customer_message .= 'Contractor City: '.$_POST['contractor_city']."<br />";
if ( !empty($_POST['contractor_state']) ) $customer_message .= 'Contractor State: '.$_POST['contractor_state']."<br />";
if ( !empty($_POST['contractor_zip']) ) $customer_message .= 'Contractor Zip: '.$_POST['contractor_zip']."<br />";
if ( !empty($_POST['contractor_contact']) ) $customer_message .= 'Contractor Contact: '.$_POST['contractor_contact']."<br />";
if ( !empty($_POST['contractor_email']) ) $customer_message .= 'Contractor Email: '.$_POST['contractor_email']."<br />";
if ( !empty($_POST['contractor_phone']) ) $customer_message .= 'Contractor Phone: '.$_POST['contractor_phone']."<br />";
if ( !empty($_POST['contractor_metal_framing_distributor']) ) $customer_message .= 'Contractor Metal Framing Distributor: '.$_POST['contractor_metal_framing_distributor']."<br />";
$customer_message .= '</div>';
$customer_message .= '</div>';
$customer_message .= '</body>';
$customer_message .= '</html>';
$customer_message .= "n--$boundaryn";
$customer_to = $_POST['contractor_email'];
$pdf_files = array();
foreach ( $_SESSION['tbc']['submittal'] as $product_id => $item )
{
$product_terms = wp_get_post_terms( $product_id, 'product_category' );
$product_category = $product_terms[0]->name;
$product_category = strip_tags($product_category);
// Clean up things like &
$product_category = html_entity_decode($product_category);
// Strip out any url-encoded stuff
$product_category = urldecode($product_category);
// Replace non-AlNum characters with ''
$product_category = preg_replace('/[^A-Za-z0-9]/', '', $product_category);
// Replace Multiple spaces with single space
$product_category = preg_replace('/ +/', ' ', $product_category);
// Trim the string of leading/trailing space
$product_category = trim($product_category);
$product_title = preg_replace('/[^A-Za-z0-9]/', '-', $item['title']);
$pdf_files[] = TBC_OVERRIDE_PATH . 'pdf-files/' . $product_category . '/' . $product_title . '.pdf';
}
$pdf = new PDFMerger;
foreach ($pdf_files as $pdf_file)
{
$pdf->addPDF($pdf_file, 'all');
}
$file_path = TBC_OVERRIDE_PATH . 'pdf-files/steel-structural-'.date('Y-m-d-i-s').'.pdf';
$pdf_content = $pdf->merge('string', 'test.pdf' );
// add pdf
$customer_message .= "n--$boundaryn";
$customer_message .= "Content-Type: application/pdf; name="".$pdf_file_name."""."n";
$customer_message .= "Content-Transfer-Encoding: base64n";
$customer_message .= "Content-Disposition: inline; filename="".$pdf_file_name."""."n";
$customer_message .= "nn".base64_encode($pdf_content);
// finish off email
$customer_message .= "n--$boundary--";
wp_mail($customer_to, $subject, $customer_message, $customer_headers);
The output of the email looks like this:
<html><head><style type="text/css"></style></head><body style="background: black; width: 100%;">--b_800BAEB5A8A63D2878504261D38B8037
Content-Type: text/html; Content-Transfer-Encoding: quoted-printable;
<div style="font-family: sans-serif; text-align: center; background: white; width: 90%; max-width: 600px; margin: 0 auto; border-top: 4px solid #f1592a; border-bottom: 4px solid #f1592a;"><div style="width: 100%; position: relative; background: #0A0A0A;"><img style="margin: 0 auto;" src="http://thinkboxprojects.com/steel-structural/wp-content/plugins/steel-structural-override/images/email-logo.png"></div><h2 style="color: black; font-style: italic; padding-top: 20px; text-transform: uppercase;">Steel Structural Products</h2><h2 style="color: black; font-style: italic; text-transform: uppercase; width: 300px; margin: 0 auto 30px auto; padding-bottom: 30px; border-bottom: 4px solid #f1592a;">Submittal Request</h2><p style="padding: 10px 30px; margin: 0 auto; text-align: left; max-width: 500px;">Thank you. We have received your submission and will contact you shortly. Below are the details of your submission.</p><div style="max-width: 500px; text-align: left; margin: 30px auto;">Cart Contents: <br><br>162PDS125-15 x 1<br><hr style="background: #f1592a; width: 300px; margin: 15px auto; height: 4px;">Project Information: <br><br>Project Name: Test<br>Project City: Test<br>Project State: MI<br><hr style="background: #f1592a; width: 300px; margin: 30px auto; height: 4px;">Contractor Information: <br><br>Contractor Email: sean@thinkboxcreative.com<br></div></div>
--b_800BAEB5A8A63D2878504261D38B8037
Content-Type: application/pdf; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="test.pdf"
JVBERi0xLjMKMyAwIG9iago8PC9....OMMITED BECAUSE OF CHARACTER LENGTH (THIS IS THE PDF)...lRU9GCg==
--b_800BAEB5A8A63D2878504261D38B8037--<br>
</body></html>
The mail output is invalid, you have HTML around your whole message (headers / attachments included). This come from
wp_mail
– I didn’t noticed you were using this function in the first time.wp_mail
build the message with boundaries, content types etc internally, so you should just use it like that:With HTML only in
$customer_message
(skip the head tags, send only what’s inside<body>
).One easy solution would just be to replace
wp_mail
withmail
and that’s probably what you want to do… But now you know how to usewp_mail
🙂