php - 为什么在 Thunderbird 中带有图像附件的 PHP HTML-Mail 渲染不正确?

标签 php html email attachment

在这个问题 Send attachments with PHP Mail()? 的帮助下 我用我的电子邮件解决了很多问题。 Gmail 显示带有在 HTML 中查看的图像的正确邮件,但在 Thunderbird 中它只显示图像而不显示 html。如果我检查邮件的源代码,它看起来也是正确的。

我能做什么?

$img_src = "km.png"; //about 79KB
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);

send("name@example.com", "Test Subject", $img_str);

function send($receiver, $subject, $content){

    $header  = "MIME-Version: 1.0" . "\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"aboundary\"" . "\n";

    $header .= "--aboundary". "\n";
    $header .= "Content-Type: multipart/alternative; boundary=\"xboundary\"". "\n\n";

    $header .= "--xboundary". "\n".
                "Content-Type: text/html; charset=utf-8". "\n".
                "Content-Transfer-Encoding: 8bit". "\n";

    $header .= "\n".    
    "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title></title></head><body>"."\n".
//  "<h1>Hello World</h1><img src=\"cid:0123456789\" alt=\"no img\" /><p>Some Testcontent with umlauts ÖÄÜß</p>". "\n". //this shell work in step 2 as well
    "<h1>Hello World</h1><img src=\"cid:0123456789\" alt=\"no img\" /><p>Some Testcontent without umlauts.</p>". "\n".
    "</body></html>"."\n\n";
    $header .= "--xboundary". "\n".
                "Content-Type: image/png; name=\"att.png\"". "\n".
                "Content-Disposition: inline; filename=\"att.png\"". "\n".
                "Content-Transfer-Encoding: base64". "\n".
                "Content-ID: <0123456789>". "\n".
                "Content-Location: att.png". "\n".
                "\n".
                chunk_split($content, 64).
                "\n".
                "--xboundary--";
    mail($receiver, $subject, "" , $header);
}

最佳答案

    $img_src = "km.png"; //about 79KB
    //$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
    //$img_str = base64_encode($imgbinary);

    $to= "name@example.com";
    $subject = "Test Subject";



        $headers = "From:  youremail@somewhere.com \r\n";
        $headers .= "Reply-To:  youremail@somewhere.com \r\n";
        $headers .= "MIME-Version: 1.0\r\n";

        $headers .= "Content-Type: text/html" . "\r\n";
        $headers .= "Content-Transfer-Encoding: base64" . "\r\n";

        $message ="<html><head><title></title></head><body><h1>Hello World</h1><img src='".$img_src."' alt='no img' /><p>Some Testcontent with umlauts ÖÄÜß</p>
        <h1>Hello World</h1><img src='".$img_src."' alt='no img' /><p>Some Testcontent without umlauts.</p>
        </body></html>";

        $base64contents = rtrim(chunk_split(base64_encode($message)));             
                     $success = mail($to, $subject, $base64contents, $headers);
                    if (!$success){
                        echo "Something Went Wrong";} 

关于php - 为什么在 Thunderbird 中带有图像附件的 PHP HTML-Mail 渲染不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17406685/

相关文章:

javascript - 如何做 HTML 谷歌多级条目?

php - 如何在 PHP 中将电子邮件附件保存到服务器?

php - 使用mysql获取最旧的日期时间值

php 错误 - 额外循环运行一次?

javascript - 单击时保持 jquery 菜单打开

php - Href 链接和链接

php - 在 php 中,我使用 mail() 发送邮件,它不能保存在我的已发送邮件文件夹中

python - 在 Django 1.7 中使用 html 发送电子邮件

php - 推特 API 推文

php - 在模式框中显示表单(简单的 php 文件)?