如何使用PHP邮件组合HTML和附件?

如何使用PHP邮件组合HTML和附件?,php,html,email,attachment,Php,Html,Email,Attachment,已解决:问题是行尾缺少一个“\r\n”读数: . "Contact " . $from . " for tech support." . "Contact " . $from . " for tech support." 谢谢大家的帮助和建议 我正在尝试制作一个PHP函数,它将发送一封带有HTML正文和附加图像的电子邮件。我在这个网站和在线教程上仔细阅读了一些答案,但我看不出哪里出了问题。我当前的代码附加了一个图像并成功发送了电子邮件,但HTML正文丢失,我收到了一封带有附加图像的空电子邮件

已解决:问题是行尾缺少一个“\r\n”读数:

. "Contact " . $from . " for tech support."
. "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>
返回路径:
信封收件人:admin@sideapps.com
交货日期:2014年6月5日星期四18:20:36-0600
收到:slan-550-84.anhosting.com从sidesul6与本地(Exim 4.82)
(信封来自)
id 1Wshtc-003XAD-04
对于admin@sideapps.com; 2014年6月5日星期四18:20:36-0600
致:admin@sideapps.com
主题:附件测试
X-PHP-Script:sideapps.com/phpMail/mailFunc.PHP for 74.70.70.214
发件人:noreply@sideapps.com
MIME版本:1.0
内容类型:多部分/混合;边界=“12CBB420AD618027879B43BA5293D9D”
消息Id:
日期:2014年6月5日星期四18:20:36-0600
很抱歉,您的客户端不支持MIME类型。
接触noreply@sideapps.com用于技术支持。--12cbb4220ad618027879b43ba5293d9d
内容类型:text/html;charset=“iso-8859-1”
内容传输编码:7bit
正在测试HTML

--12CBB420AD618027879B43BA5293D9D 内容类型:图片/png;name=“test.png” 内容传输编码:base64 内容处置:附件;file=“test.png”
请参见下面的我的功能:

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');
函数GetHTML() {
return这不会附加文件,但您可以在HTML中嵌入图像。看起来您已经对附件部分进行了排序。这可能对HTML内容有所帮助

这段代码在2007年的工作环境中工作,还没有用现代浏览器、Outlook 2007年版等对其进行全面测试

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>

命令
表#bodyTable{}
表#emailContainer{}
此电子邮件用于确认您最近提交的工作
PHP: 我使用动态内容和外部模板构建$msg字符串 包括('./templates/teamNotificationEmailBody.php')

函数orderMail($sendTo、$subject、$msg、$errorRedirect、$successRedirect){
$headers='MIME版本:1.0'。“\r\n”;
$headers.=“内容类型:text/html;字符集=iso-8859-1”。“\r\n”;
$headers.='来自:DoNotReply@Wherever.com';
如果(isset($sendTo))
{  
$email=filter\u var($sendTo,filter\u SANITIZE\u email);
如果(!filter_var($email,filter_VALIDATE_email))
{    
echo“
$email不是有效的电子邮件地址。

请返回预订表并输入有效的电子邮件地址。


”; 退出(); } } if(邮件($sendTo,$subject,$msg,$headers)) { 如果(isset($successRedirect)) { 回显“$successRedirect”; 标题(“位置:$successRedirect”); 退出(); } } 其他的 { echo“
电子邮件通知系统出现错误。



; 退出(); }
行尾缺少一个“\r\n”读数:

. "Contact " . $from . " for tech support."
. "Contact " . $from . " for tech support."
以下是AMMEND功能:

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');
函数GetHTML() {
return为自己省去了一个大麻烦:var_dump$body,看看当它被传递到函数中时,你会得到什么。谢谢@universe,但我已经为此付出了几个小时的努力,我很想完成这个函数。@Vikingbreaded这里是var_dump:string(37)“立即测试HTML”的输出。这只是一个简单的字符串(HTML一词嵌入在和标记中)。向我们展示构建$body的代码感谢您的回复!我实际上已经开始将图像嵌入我发送的HTML中,但这将更方便我的雇主处理附件(我知道,我知道-右键单击另存为-不要让我开始)我的目标是取悦您。我将仔细研究您提供的一些代码,看看这些代码片段是否有帮助。这些代码对我来说很有用,但我认为我的问题与我的多部分MIME格式有关,这是包含附加图像(而不是嵌入图像)所必需的。再次感谢!您使用的是哪种服务器环境,以及哪种版本的PHP?您是否尝试过boundary=\“PHP mixed-”$your\u hash。“\”?我四处查看了一下,这对.zip文件很有效。可能需要更改内容类型:application/zip;name=“attachment.zip”以匹配您的附件类型,如内容类型:image/png等。祝您好运!