【问题标题】:MIME Multipart Message in PHP not sendingPHP中的MIME多部分消息未发送
【发布时间】:2014-09-07 07:08:42
【问题描述】:

我正在尝试在 PHP 中发送 MIME 多部分消息,但它没有发送。我得出的结论是,这可能是因为某处的 MIME 语法有问题,但我不知道在哪里。

代码如下:

$Subject = "****** Contact Form";
$Name = trim(stripslashes($_POST['Name'])); 
$Email = trim(stripslashes($_POST['Email'])); 
$Message = nl2br(trim(stripslashes($_POST['Message'])));
$MessagePlain = trim(stripslashes($_POST['Message']));
$ReferredBy = $_POST['referredby']; 
$Other = trim(stripslashes($_POST['othertext'])); 
$Day = $_POST['Day']; 
$Month = $_POST['month']; 
$Year = $_POST['year']; 

if ($Month = 1) {
        $Month = 'January';
    } elseif ($Month = 2) {
        $Month = 'February';
    } elseif ($Month = 3) {
        $Month = 'March';
    } elseif ($Month = 4) {
        $Month = 'April';
    } elseif ($Month = 5) {
        $Month = 'May';
    } elseif ($Month = 6) {
        $Month = 'June';
    } elseif ($Month = 7) {
        $Month = 'July';
    } elseif ($Month = 8) {
        $Month = 'August';
    } elseif ($Month = 9) {
        $Month = 'September';
    } elseif ($Month = 10) {
        $Month = 'October';
    } elseif ($Month = 11) {
        $Month = 'November';
    } elseif ($Month = 12) {
        $Month = 'December';
}

$MIMEBoundary = md5(uniqid(rand()));

// prepare email headers
$Headers  = "MIME-Version: 1.0\r\n";
$Headers .= "From: ******\r\n";
$Headers .= "Reply-To: ****** <******@hotmail.com>\r\n";
$Headers .= "Content-type: multipart/alternative; boundary=$MIMEBoundary\r\n";

// prepare email body text
$Body = "--" . $MIMEBoundary . "\r\n";
$Body .= "Content-type: text/html;charset=\"ISO-8859-1\"\r\n";
$Body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$Body .= '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Thank you for your message!</title><style type="text/css"> @media only screen and (max-width: 647px){#container{padding:0 !important;}#header, #header-content, #body, #content, #info, #body-message{width:100% !important;}#body-message, #quote{padding:20px !important;}} </style></head><body style="margin:0;"><table width="100%" height="100%" style="background-color:#fafafa;font-family:Helvetica Neue, Helvetica, sans-serif;padding:45px;" cellpadding="0" cellspacing="0" id="container"><tr><td><table align="center" cellpadding="0" cellspacing="0" border="0" id="rounded-border-top"><tr><td><img src="http://******"/></td></tr></table><table align="center" cellpadding="0" cellspacing="0" border="0" style="border-left:solid 1px #e6e6e6;border-right:solid 1px #e6e6e6;" id="content"><tr><td style="width:600px;background-color:#ffffff;" id="body"><table cellpadding="0" cellspacing="0" border="0" style="padding:45px;color:#303030;line-height:140%;" id="body-message"><tr><td>Hi '.$Name.',<br/><br/> Thank you for your message! I will endeavour to reply to your message within 24 hours. If you have any queries, please feel free to contact me on 07783 962 578.<br/><br/> As a first thanks, here is a free copy of the \'10 top wedding-planning tips\'. It covers the 10 essentials to wedding planning and common mistakes that can be made. Wedding planning is often very stressful, with so many things to think about. So hopefully this little companion will help lessen the load!<br/><br/><a href="downloadfreecopy.html"><img src="http://******" alt="Download Free Copy" style="border:0;"/></a><br/><br/> Best Wishes,<br/><br/><img src="http://******" alt="******"></td></tr></table></td></tr></table><table align="center" cellpadding="0" cellspacing="0" border="0" id="rounded-border-bottom"><tr><td><img src="http://******" align="left"></td></tr></table></td></tr></table></body></html>';
$Body .= "\r\n\r\n--' . $MIMEBoundary . '\r\n";
$Body .= "Content-type: text/plain;charset=\"ISO-8859-1\"\r\n";
$Body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$Body .= 'Hi '.$Name.',

Thank you for your message! I will endeavour to reply to your message within 24 hours. If you have any queries, please feel free to contact me on 07783 962 578.\r\n\r\nAs a first thanks, here is a free copy of the \'10 top wedding-planning tips\'. It covers the 10 essentials to wedding planning and common mistakes that can be made. Wedding planning is often very stressful, with so many things to think about. So hopefully this little companion will help lessen the load!

http://******

Best Wishes,
******';
$Body .= "\r\n\r\n--" . $MIMEBoundary;

$Body2 = "--" . $MIMEBoundary . "\r\n";
$Body2 .= "Content-type: text/html;charset=\"ISO-8859-1\"\r\n";
$Body2 .= "Content-Transfer-Encoding: base64\r\n\r\n";
$Body2 .= '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Thank you for your message!</title><style type="text/css"> @media only screen and (max-width: 647px){#container{padding:0 !important;}#header, #header-content, #body, #content, #info, #body-message{width:100% !important;}#body-message, #quote{padding:20px !important;}} </style></head><body style="margin:0;"><table width="100%" height="100%" style="background-color:#fafafa;font-family:Helvetica Neue, Helvetica, sans-serif;padding:45px;" cellpadding="0" cellspacing="0" id="container"><tr><td><table align="center" cellpadding="0" cellspacing="0" border="0" id="rounded-border-top"><tr><td><img src="http://******"/></td></tr></table><table align="center" cellpadding="0" cellspacing="0" border="0" style="border-left:solid 1px #e6e6e6;border-right:solid 1px #e6e6e6;" id="content"><tr><td style="width:600px;background-color:#ffffff;" id="body"><table cellpadding="0" cellspacing="0" border="0" style="padding:45px;color:#303030;line-height:140%;" id="body-message"><tr><td>New Message<br/><br/><table width="100%" cellpadding="0" cellspacing="10" border="0" style="width:510px;" id="info"><tr><td width="50%"><strong>Question</strong></td><td width="50%"><strong>Answer</strong></td></tr><tr><td>Name:</td><td>'.$Name.'</td></tr><tr><td>Email:</td><td>'.$Email.'</td><tr><td>Phone:</td><td>'.$Telephone.'</td></tr><tr><td>Referred By:</td><td>'.$ReferredBy.'</td></tr><tr><td>Date:</td><td>'.$Day.' '.$Month.' '.$Year.'</td></tr><tr><td colspan="2">Message:<br/><br/><table width="100%" cellpadding="45" cellspacing="0" border="0" style="background-color:#fafafa;color:#505050;font-size:14px;line-height:140%;"><tr><td>'.$MessagePlain.'</td></tr></table></td></tr></table></td></tr></table></td></tr></table><table align="center" cellpadding="0" cellspacing="0" border="0" id="rounded-border-bottom"><tr><td><img src="http://******" align="left"></td></tr></table></td></tr></table></body></html>';
$Body2 .= "Content-type: text/plain;charset=\"ISO-8859-1\"\r\n";
$Body2 .= "Content-Transfer-Encoding: base64\r\n\r\n";
$Body2 .= 'New Message

Question Answer
Name: '.$Name.'
Email: '.$Email.'
Phone: '.$Telephone.'
Referred By: '.$ReferredBy.'
Date: '.$Day.' '.$Month.' '.$Year.'
Message:
' . $MessageAdmin;
$Body2 .= "\r\n\r\n--" . $MIMEBoundary;

//Send email to user
mail($Email, $Subject, $Body, $Headers);

// send email to admin
mail('******@hotmail.com', $Subject, $Body2, $Headers);


?>

【问题讨论】:

    标签: php email mime


    【解决方案1】:

    问题

    你的代码有几个问题:

    • 第一个和第二个正文部分之间的分隔符不正确

      $Body .= "\r\n\r\n--' . $MIMEBoundary . '\r\n";
      

      上面的行将输出单引号和点。它应该是:

        $Body .= "\r\n\r\n--" . $MIMEBoundary . "\r\n";
      

        $Body .= "\r\n\r\n--$MIMEBoundary\r\n";
      
    • 两封电子邮件中的关闭分隔符都不正确

      $Body .= "\r\n\r\n--" . $MIMEBoundary;
      $Body2 .= "\r\n\r\n--" . $MIMEBoundary;
      

      关闭分隔符必须以两个破折号结尾,因此上面的行应为:

        $Body .= "\r\n\r\n--" . $MIMEBoundary . "--";
        $Body2 .= "\r\n\r\n--" . $MIMEBoundary . "--";
      

        $Body .= "\r\n\r\n--$MIMEBoundary--";
        $Body2 .= "\r\n\r\n--$MIMEBoundary--";
      
    • 内容编码不正确

      如果您指定Content-Transfer-Encoding: base64,您还必须指定base64_encode()(和chunk_split())该正文部分。

    另请参阅下面的最后说明

    MIME 语法

    这是根据 RFC 2046 的多部分消息正文结构的摘录。(BNF 语法,有些简化。)

    multipart-body := [序言 CRLF] 虚线边界CRLF 身体部分 *封装 闭合分隔符 [CRLF 结语] dash-boundary := "--" 边界 body-part := MIME-part-headers [CRLF *OCTET] 封装 := 分隔符 CRLF 身体部位 delimiter := CRLF 破折号边界 close-delimiter := 分隔符“--”

    参考文献

    最后说明

    • 在 HTML 电子邮件正文中展开的变量(例如 $Name 等)应使用 htmlspecialchars() 进行转义。

    • 如果将每个正文部分存储在变量中,则更容易理解电子邮件的结构:

        $Body  = "--$MIMEBoundary\r\n"; /* First dash-boundary */
        $Body .= "Content-type: text/html;charset=\"ISO-8859-1\"\r\n";
        $Body .= "Content-Transfer-Encoding: base64\r\n";
        $Body .= "\r\n";
        $Body .= $Body_part1;
        $Body .= "\r\n--$MIMEBoundary\r\n"; /* delimiter */
        $Body .= "Content-type: text/plain;charset=\"ISO-8859-1\"\r\n";
        $Body .= "Content-Transfer-Encoding: base64\r\n";
        $Body .= "\r\n";
        $Body .= $Body_part2;
        $Body .= "\r\n--$MIMEBoundary--"; /* close-delimiter */
        /* You only need a single CRLF between body part and delimiter. */
      
    • 就个人而言,我会在使用之前测试 MIME 边界字符串是否出现在任何身体部位,但我也会使用 much 更短的边界字符串。但这只是我。普通人不会那样做。

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 1970-01-01
      • 2017-05-12
      • 2023-03-22
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2022-07-12
      相关资源
      最近更新 更多