PHPMailer HTML无法呈现

时间:2012-02-29 19:25:13

标签: phpmailer

我正在使用PHPMailer发送HTML电子邮件。它可以很好地发送电子邮件,但HTML不会在电子邮件客户端中呈现。我用基于网络的GMail和OSX mail.app测试了这个IMAP到同一个GMail帐户。都没有正确渲染。

我正在拔头发来解决这个问题。任何想法???

PHP代码格式化要发送的电子邮件(发送的电子邮件正常工作):

// mail format settings
// $mail->WordWrap = 50;        // set word wrap to 50 characters
$mail->IsHTML(true);            // set email format to HTML
$mail->CharSet="UTF-8";         // set the charset to UTF-8
$mail->Encoding = '7bit';
$body = '';
$body .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
$body .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">";
$body .= "<head>";
$body .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
$body .= "<meta http-equiv=\"Content-Language\" content=\"en-us\" />";
$body .= "<style>";
$body .= "body {font-family:\"trebuchet ms\", arial, \"san serif\";font-size:14px;}";
$body .= ".email-header{ margin:5px;padding:20px;background:#dddddd;border-radius:10px;color:#000000;font-size:24px;font-weight:bold;}";
$body .= ".email-body{margin:10px 20px;}";
$body .= "</style>";
$body .= "</head>";
$body .= "<body>"; 
if ($_POST["type"]=='confirmation') {

    // set the email body
    $body .= "<div class=\"email-header\">Sky Lodge HOA Reservation Confirmation</div>";
    $body .= "<div class=\"email-body\">";
    $body .= "Dear ". $_POST["name"] . ",<br/><br/>";
    $body .= "This email is confirming your reservation for unit nnn x:<br/>";
    $body .= "<strong>Arriving:</strong> date<br/>";
    $body .= "<strong>Departing:</strong> date<br/><br/>"; 
    $body .= "Please click here to view your existing reservation(s):<br/>";
    $body .= "http://www.somesite.com/index.php?token=" . $_POST["token"] . "<br/><br/>";

    // set the email subject
    $mail->Subject = "Sky Lodge HOA: Reservation Confirmation";

} else if ($_POST["type"]=='notify') {

    // set the email body
    $body .= "<div class=\"email-header\">Sky Lodge HOA Attention Required</div>";
    $body .= "<div class\"email-body\">";
    $body .= "Dear ". $_POST["name"] . ",<br/><br/>";
    $body .= "It is your turn to make a reservation for unit nnn x.<br/><br/>";
    $body .= "Please click here to make your reservation:<br/>";
    $body .= "http://www.somesite.com/index.php?token=" . $_POST["token"] . "<br/><br/>";

    // set the email subject
    $mail->Subject = "Sky Lodge HOA: Attention Required";

}
$body .= "Thank You,<br/>Patti Rea";
$body .= "</div>";
$body .= "</body>";
$body .= "</html>";
echo('$body: ' . $body);
$mail->Body = $body;
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

结果邮件:

Return-Path: <darren@theehlers.org>
Received: from skylodge (c-174-52-123-157.hsd1.ut.comcast.net. [174.52.123.157])
        by mx.google.com with ESMTPS id ko12sm7817099pbb.52.2012.02.29.11.02.07
        (version=SSLv3 cipher=OTHER);
        Wed, 29 Feb 2012 11:02:07 -0800 (PST)
Date: Wed, 29 Feb 2012 12:02:05 -0700
Return-Path: darren@theehlers.org
To: Sky Lodge <darren@theehlers.org>
From: Darren Ehlers <darren@theehlers.org>
Cc: kathi@theehlers.org
Reply-To: Darren Ehlers <darren@theehlers.org>
Subject: Sky Lodge HOA: Reservation Confirmation
Message-ID: <9941ff8576a1763200e134b5b313b1b1@skylodge>
X-Priority: 3
X-Mailer: PHPMailer 5.2.1 (http://code.google.com/a/apache-extras.org/p/phpmailer/)
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="b1_9941ff8576a1763200e134b5b313b1b1"


--b1_9941ff8576a1763200e134b5b313b1b1
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

This is the body in plain text for non-HTML mail clients


--b1_9941ff8576a1763200e134b5b313b1b1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en-us" />
<style>
body {font-family:"trebuchet ms", arial, "san serif";font-size:14px;}
.email-header{ margin:5px;padding:20px;background:#dddddd;border-radius:10px;color:#000000;font-size:24px;font-weight:bold;}
.email-body{margin:10px 20px;}</style>
</head>
<body>
<div class="email-header">Sky Lodge HOA Reservation Confirmation</div>
<div class="email-body">
Dear Sky Lodge,<br/><br/>
This email is confirming your reservation for unit nnn x:<br/>
<strong>Arriving:</strong> date<br/>
<strong>Departing:</strong> date<br/><br/>
Please click here to view your existing reservation(s):<br/>
http://www.somesite.com/index.php?token=WQ85o7OiT9<br/><br/>
Thank You,<br/>
Patti Rea
</div>
</body>
</html>

--b1_9941ff8576a1763200e134b5b313b1b1--

1 个答案:

答案 0 :(得分:0)

您是否通过删除doctype和标题标记进行了测试...在我看来,没有必要使用它们......其余的看起来很好。