【问题标题】:What is wrong with this mail header text?此邮件标题文本有什么问题?
【发布时间】:2011-01-28 15:05:00
【问题描述】:

以下 $header 正在通过 PHP 的 mail($to,$subject,$content,$header) 命令发送。邮件到达并且似乎有一个附件。但是邮件文本和文件一样是空的。我认为这与行距有关,但我看不到问题所在。我尝试将内容(在边界之间)放入$contents,而不是将其附加到$header。这没什么区别。有什么想法吗?

From: dnagirl@someplace.com 
Reply-To: dnagirl@someplace.com 
X-Mailer: PHP 5.3.1
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="7425195ade9d89bc7492cf520bf9f33a"

--7425195ade9d89bc7492cf520bf9f33a
Content-type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

this is a test message.

--7425195ade9d89bc7492cf520bf9f33a
Content-Type: application/pdf; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.pdf"

JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k
ZT4+CnN0cmVhbQp4nE2PT0vEMBDFadVdO4p/v8AcUyFjMmma5CqIIF5cctt6WnFBqLD1+4Np1nY3
c3lvfm+GyQ4VaUY11iQ2PTyuHG5/Ibdx9fIvhi3swJMZX24c602PTzENegwUbNAO4xcoCsG5xuWE
.
... the rest of the file
.
MDAwMDA2MDYgMDAwMDAgbiAKMDAwMDAwMDcwNyAwMDAwMCBuIAowMDAwMDAxMDY4IDAwMDAwIG4g
CjAwMDAwMDA2NDcgMDAwMDAgbiAKMDAwMDAwMDY3NyAwMDAwMCBuIAowMDAwMDAxMjg2IDAwMDAw
IG4gCjAwMDAwMDA5MzIgMDAwMDAgbiAKdHJhaWxlcgo8PCAvU2l6ZSAxNCAvUm9vdCAxIDAgUiAv
SW5mbyAyIDAgUgovSUQgWzxEMURDN0E2OUUzN0QzNjI1MDUyMEFFMjU0MTMxNTQwQz48RDFEQzdB
NjlFMzdEMzYyNTA1MjBBRTI1NDEzMTU0MEM+XQo+PgpzdGFydHhyZWYKNDY5MwolJUVPRgo=


--7425195ade9d89bc7492cf520bf9f33a--

$header 没有换行符结束

【问题讨论】:

    标签: php email mime email-attachments


    【解决方案1】:

    我使用下面的代码发送带有附件以及 html 和文本消息部分的消息。我记得与它斗争了很长时间以使其正确,但不幸的是不记得哪些部分是我的问题。也许看看它会帮助你。我已经更改了一些不相关的变量,并希望使其更易于阅读。如果有什么奇怪或不清楚的地方,请随时询问,我会尽力解释。

    public function sendEmail($htmlString)
    {
        $random_hash = md5(date('r', time()));
    
        $message = "--mixed-$random_hash\n";
        $message .= 'Content-Type: multipart/alternative; boundary="alt-' . $random_hash . '"' . "\n\n";
        $message .= 'MIME-Version: 1.0' . "\n";
        $message .= '--alt-' . $random_hash . "\n";
        $message .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
        $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
    
        // text message
        $message .= strip_tags($htmlString) . "\n\n";
    
        $message .= '--alt-' . $random_hash . "\n";
        $message .= 'Content-Type: text/html; charset="iso-8859-1"' . "\n";
        $message .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
    
        // html message
        $message .= $htmlString . "\n\n";
    
        $message .= '--alt-' . $random_hash . '--' . "\n";
    
        // graph image
        if($this->selectedGraph != 0)
        {
            $graphString = $this->getGraph(); // image attachment
            $graphString = chunk_split(base64_encode($graphString));
    
            $linkID = 'graph-' . $userInfo['FirmID'] . $random_hash . '-image';
    
            $message .= '--mixed-' . $random_hash . "\n";
            $message .= 'MIME-Version: 1.0' . "\n";
            $message .= 'Content-Transfer-Encoding: base64' . "\n";
            $message .= 'Content-ID: ' . $linkID . "\n";
            $message .= 'Content-Type: image/gif; name="graph.gif"' . "\n";
            $message .= 'Content-Disposition: attachment' . "\n\n";
    
            $message .= $graphString;
            $message .= '--mixed-' . $random_hash . '--' . "\n";
    
        }
        else
        {
            $message .= '--mixed-' . $random_hash . '--' . "\n";
        }
    
    
        $headers = 'From: ' . $this->from. "\r\nReply-To: " . $this->replyto;
        $headers .= "\r\nContent-Type: multipart/related; boundary=\"mixed-" . $random_hash . "\"\r\nMIME-Version: 1.0";
    
        $flags = '-f ' . BOUNCED_EMAIL_ADDRESS;
    
        return mail($userInfo['Email'], $this->subject, $message, $headers, $flags);
    
    }
    

    【讨论】:

    • 根据您的示例,我将行尾从 \r\n 更改为 \n。那解决了它。发送!
    • 我的建议是换行,但我迟到了 :) 正确的格式是 \r\n 但根据我的经验,有一些电子邮件服务器程序试图修复它自己并最终破坏消息。
    【解决方案2】:

    这些事情发生在从头开始构建 mime 邮件时。有几个 php 模块可用于生成酷炫且兼容的 mime 消息。 Mail_Mime 应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2015-01-04
      相关资源
      最近更新 更多