PHP pdf 电子邮件附件,内容类型:多部分/混合,html 消息未显示

Posted

技术标签:

【中文标题】PHP pdf 电子邮件附件,内容类型:多部分/混合,html 消息未显示【英文标题】:PHP pdf email attachment with content-type: multipart/mixed, html message not showing up 【发布时间】:2016-01-03 12:37:33 【问题描述】:

我已使用此方法成功地将生成的 pdf 文件附加到我的电子邮件中,但我无法让邮件的 html 部分显示在电子邮件中。它带有附件,没有消息正文。我需要做一些不同的事情吗?我正在使用 wp_mail 发送消息。

$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.0\n";
$customer_headers .= "Content-Type: multipart/mixed; boundary=$boundary\n";
$customer_headers .= 'From: Steel Structural <'.get_option('admin_email').'>' . "\r\n";

$customer_message .= "\n--$boundary\n";
$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--$boundary\n";

$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 &amp;
    $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--$boundary\n";
$customer_message .= "Content-Type: application/pdf; name=\"".$pdf_file_name."\""."\n";
$customer_message .= "Content-Transfer-Encoding: base64\n";
$customer_message .= "Content-Disposition: inline; filename=\"".$pdf_file_name."\""."\n";
$customer_message .= "\n\n".base64_encode($pdf_content);

// finish off email
$customer_message .= "\n--$boundary--";

wp_mail($customer_to, $subject, $customer_message, $customer_headers);

电子邮件的输出如下所示:

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

【问题讨论】:

您可以尝试在Content-Type: text/html; 之后添加Content-Transfer-Encoding: quoted-printable 吗? 是的,同样的事情。电子邮件附件显示正常,但邮件正文却没有。感谢您的回复。 你能把生成的邮件源也粘贴一下吗? 已更新电子邮件输出 【参考方案1】:

邮件输出无效,整个邮件(包括标题/附件)周围都有 HTML。这来自wp_mail - 我没有注意到你第一次使用这个功能。 wp_mail 在内部构建带有边界、内容类型等的消息,所以你应该像这样使用它:

function my_email_content_type() 
  return 'text/html';

$attachmments = array($file_path);
$headers = array(
   'From: Steel Structural <'.get_option('admin_email').'>'
);
add_filter('wp_mail_content_type', 'my_email_content_type');
wp_mail($customer_to, $subject, $customer_message, $headers, $attachments);

仅在 $customer_message 中使用 HTML(跳过头部标签,仅发送 &lt;body&gt; 中的内容)。

一个简单的解决方案就是将wp_mail 替换为mail,这可能就是您想要做的......但是现在您知道如何使用wp_mail :)

【讨论】:

以上是关于PHP pdf 电子邮件附件,内容类型:多部分/混合,html 消息未显示的主要内容,如果未能解决你的问题,请参考以下文章

解析并对其进行一些更改后,为多部分电子邮件设置的内容类型应该是啥?

多部分电子邮件 PDF 附件空白

带有pdf附件和html消息的php邮件功能

在 python 中解析多部分电子邮件并保存附件

PHP 发送带附件的多部分编码邮件。

如何在 PHP 中提供多部分/相关内容?