已解决:问题是在行尾读取了“\ r \ n”:
. "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 . "\r\n";
$bound_last = "--" . $bound_text . "--\r\n";
$headers = "From: " . $from . "\r\n"
. "Reply-To: " . $from . "\r\n"
. "Return-Path: " . $from . "\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$message = "Sorry, your client doesn't support MIME types.\r\n"
. "Contact " . $from . " for tech support."
. $bound;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. $body . "\r\n"
. $bound;
$file = file_get_contents($photoPath);
$message .= "Content-Type: $filetype; name=\"$photoName\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-disposition: attachment; file=\"$photoName\"\r\n"
. "\r\n"
. 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');
答案 0 :(得分:1)
在行的尾端读取“\ r \ n”:
. "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 . "\r\n";
$bound_last = "--" . $bound_text . "--\r\n";
$headers = "From: " . $from . "\r\n"
. "Reply-To: " . $from . "\r\n"
. "Return-Path: " . $from . "\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$message = "Sorry, your client doesn't support MIME types.\r\n"
. "Contact " . $from . " for tech support.\r\n"
. $bound;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. $body . "\r\n"
. $bound;
$file = file_get_contents($photoPath);
$message .= "Content-Type: $filetype; name=\"$photoName\"\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-disposition: attachment; file=\"$photoName\"\r\n"
. "\r\n"
. 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');
答案 1 :(得分:0)
这不会附加文件,但您可以在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字符串 包括(&#39; ./模板/ teamNotificationEmailBody.php&#39);
function orderMail($sendTo, $subject, $msg, $errorRedirect, $successRedirect) {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$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();
}