RESOLVED:问题是在行尾缺少"rn"
. "Contact " . $from . " for tech support."
感谢大家的帮助和建议!
我试图使一个PHP函数,将发送一封电子邮件与一些HTML的身体和附加的图像。我在这个网站和在线教程上仔细阅读了几个答案,但我看不出我错在哪里。我目前的代码附加了一个图像,并成功发送电子邮件,但HTML正文缺失,我收到一个空的电子邮件与我附加的图像。
我收到的电子邮件的原始来源似乎有我的HTML,但我的电子邮件客户端没有呈现这个HTML。请看下面的来源:
Return-path: <sidesul6@slan-550-84.anhosting.com>
Envelope-to: admin@sideapps.com
Delivery-date: Thu, 05 Jun 2014 18:20:36 -0600
Received: from sidesul6 by slan-550-84.anhosting.com with local (Exim 4.82)
(envelope-from <sidesul6@slan-550-84.anhosting.com>)
id 1Wshtc-003XAD-04
for admin@sideapps.com; Thu, 05 Jun 2014 18:20:36 -0600
To: admin@sideapps.com
Subject: attachmentTest
X-PHP-Script: sideapps.com/phpMail/mailFunc.php for 74.70.70.214
From:noreply@sideapps.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="12cbb4220ad618027879b43ba5293d9d"
Message-Id: <E1Wshtc-003XAD-04@slan-550-84.anhosting.com>
Date: Thu, 05 Jun 2014 18:20:36 -0600
Sorry, your client doesn't support MIME types.
Contact noreply@sideapps.com for tech support.--12cbb4220ad618027879b43ba5293d9d
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>
<body>
<p>Testing <i><b>HTML</b></i> right now.</p>
</body>
</html>
--12cbb4220ad618027879b43ba5293d9d
Content-Type: image/png; name="test.png"
Content-Transfer-Encoding: base64
Content-disposition: attachment; file="test.png"
<base64 image encoding>
见下面的函数:
function GetHTML()
{
return <<<HTML
<html>
<body>
<p>Testing <i><b>HTML</b></i> right now.</p>
</body>
</html>
HTML;
}
function mailAttachment($to, $subject, $body, $from, $photoPath, $photoName, $filetype)
{
$bound_text = md5(uniqid(rand(), true));;
$bound = "--" . $bound_text . "rn";
$bound_last = "--" . $bound_text . "--rn";
$headers = "From: " . $from . "rn"
. "Reply-To: " . $from . "rn"
. "Return-Path: " . $from . "rn"
. "MIME-Version: 1.0rn"
. "Content-Type: multipart/mixed; boundary="$bound_text"";
$message = "Sorry, your client doesn't support MIME types.rn"
. "Contact " . $from . " for tech support."
. $bound;
$message .= "Content-Type: text/html; charset="iso-8859-1"rn"
. "Content-Transfer-Encoding: 7bitrnrn"
. $body . "rn"
. $bound;
$file = file_get_contents($photoPath);
$message .= "Content-Type: $filetype; name="$photoName"rn"
. "Content-Transfer-Encoding: base64rn"
. "Content-disposition: attachment; file="$photoName"rn"
. "rn"
. chunk_split(base64_encode($file))
. $bound_last;
if(mail($to, $subject, $message, $headers))
{
echo 'MAIL SENT!' . '<br>';
echo 'to: ' . $to . '<br>';
echo 'from: ' . $from . '<br>';
echo 'bodyText: ' . $body . '<br>';
echo 'photoPath: ' . $photoPath . '<br>';
echo 'photoName: ' . $photoName . '<br>';
echo 'filetype: ' . $filetype . '<br>';
}
else
{
echo 'MAIL FAILED';
}
}
mailAttachment('admin@sideapps.com', 'attachmentTest', GetHTML(),
'noreply@sideapps.com', 'testImage.png', 'uploaded.png', 'image/png');
在行尾缺少"rn"
. "Contact " . $from . " for tech support."
下面是修改后的函数:
function GetHTML()
{
return <<<HTML
<html>
<body>
<p>Testing <i><b>HTML</b></i> right now.</p>
</body>
</html>
HTML;
}
function mailAttachment($to, $subject, $body, $from, $photoPath, $photoName, $filetype)
{
$bound_text = md5(uniqid(rand(), true));;
$bound = "--" . $bound_text . "rn";
$bound_last = "--" . $bound_text . "--rn";
$headers = "From: " . $from . "rn"
. "Reply-To: " . $from . "rn"
. "Return-Path: " . $from . "rn"
. "MIME-Version: 1.0rn"
. "Content-Type: multipart/mixed; boundary="$bound_text"";
$message = "Sorry, your client doesn't support MIME types.rn"
. "Contact " . $from . " for tech support.rn"
. $bound;
$message .= "Content-Type: text/html; charset="iso-8859-1"rn"
. "Content-Transfer-Encoding: 7bitrnrn"
. $body . "rn"
. $bound;
$file = file_get_contents($photoPath);
$message .= "Content-Type: $filetype; name="$photoName"rn"
. "Content-Transfer-Encoding: base64rn"
. "Content-disposition: attachment; file="$photoName"rn"
. "rn"
. chunk_split(base64_encode($file))
. $bound_last;
if(mail($to, $subject, $message, $headers))
{
echo 'MAIL SENT!' . '<br>';
echo 'to: ' . $to . '<br>';
echo 'from: ' . $from . '<br>';
echo 'bodyText: ' . $body . '<br>';
echo 'photoPath: ' . $photoPath . '<br>';
echo 'photoName: ' . $photoName . '<br>';
echo 'filetype: ' . $filetype . '<br>';
}
else
{
echo 'MAIL FAILED';
}
}
mailAttachment('admin@sideapps.com', 'attachmentTest', GetHTML(),
'noreply@sideapps.com', 'testImage.png', 'uploaded.png', 'image/png');
这并不附加文件,但您可以在HTML中嵌入图像。看起来你把附件部分整理好了。也许这将有助于html的内容。
此代码可在2007年的工作环境中运行,尚未在现代浏览器,2007年以后的Outlook版本等上进行完全测试。
IIRC,技巧是将完整的html嵌入到电子邮件正文中,并使用表格。
HTML:<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>Order</title>
</head>
<body>
<style type="text/css">
table#bodyTable {}
table#emailContainer {}
</style>
<table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0" id="bodyTable">
<tr><td align="center" valign="top">
<div style="overflow: hidden;">
<table border="0" cellpadding="0" cellspacing="0" width="800" id="emailContainer">
<tr><td align="center" valign="top">
<table style="font-family:Tahoma;font-size:13px" width="500" align="center">
<tr bgcolor="#ffffff"><td style="padding:10px;color:#000000;" align="left" bgcolor="#ffffff">
<div style="margin: 15px;float:left;">
<a href="http://image.link.redirect" target="_blank"><img alt="YourImageAltText" border="0" src="http://your.domain/img/yourImage.jpg" height="48" width="230" /></a>
</div>
</td></tr>
<tr><td style="padding:18px 30px 10px;font-size:11px" align="left">
<div style="font-size:12px">
This email is to confirm your recent job submission
</div>
</tr></td>
<tr bgcolor="#ffffff"><td style="padding:10px;color:#000000" align="center"></td></tr>
</table>
</td></tr>
</table><!-- End central content table, fixed width, aligned to center based upon master table -->
</div>
</td></tr>
</table><!-- end master containing table -->
</body>
</html>
PHP:我用动态内容和外部模板构建了$msg字符串,使用包括("。/模板/teamNotificationEmailBody.php");
function orderMail($sendTo, $subject, $msg, $errorRedirect, $successRedirect) {
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
$headers .= 'From: DoNotReply@Wherever.com';
if (isset($sendTo))
{
$email = filter_var($sendTo, FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "<hr/>$email is <strong>NOT</strong> a valid email address.<br/><br/>Please return to the reservation form and enter a valid email address.<hr/><br/><a href="$errorRedirect">Return $errorRedirect</a><br/>";
exit();
}
}
if (mail($sendTo, $subject, $msg, $headers))
{
if (isset($successRedirect))
{
echo "$successRedirect";
header("location: $successRedirect");
exit();
}
}
else
{
echo "<hr/>An error occured with the email notification system. <br/><br/><hr/><br/><a href="$errorRedirect">Click here to return</a><br/>";
exit();
}