HTML 邮件中的嵌入图像显示为附件而不是模板中的图像

发布于 2024-10-16 11:29:27 字数 2065 浏览 4 评论 0原文

我正在使用 swiftmailer 发送 HTML 邮件。一切都很好,我的 HTML 邮件在所有客户端中都显示得很好。我对构建电子邮件模板有相当的了解。自己发送的次数不多...

gmail 和 hotmail 中存在问题。图像没有显示。例如,它在 Thunderbird 中显示得很好... gmail 将我的图像留空并将其添加为附件(在邮件底部,就像我发送生日照片一样......)。

有什么想法吗?

我有以下代码:

function deliverMail($subject, $data, $from, $to, $template){
    if($_SERVER['SERVER_ADDR'] !== '::1'){

        if (!is_array($to)){
            $to = array($to);       
        }           
        $from = explode(",", $from);        

        //Create the Transport
        $transport = Swift_SmtpTransport::newInstance('mail.xxx.nl', 25)
          ->setUsername('xxx')
          ->setPassword('xxx');           

        $mailer = Swift_Mailer::newInstance($transport);
        $message = Swift_Message::newInstance($subject);
        $image = $message->embed(Swift_Image::fromPath($_SERVER['DOCUMENT_ROOT'].'/templates/emails/xxx.jpg'));


        // open the file
        $sourcefile     = $_SERVER['DOCUMENT_ROOT'].'/templates/emails/'.$template.'.html.tpl';
        $fh             = fopen($sourcefile, 'r');
        $htmltemplate       = fread($fh, filesize($sourcefile));
        fclose($fh);

        // set the content  
        if($template == 'confirm'){$replaceOld = array("[*code*]", "[*imgsrc*]");}          
        if($template == 'notify'){$replaceOld = array("[*url*]", "[*imgsrc*]");}

        $replaceNew     = array($data, $image);         
        $body           = str_replace($replaceOld, $replaceNew, $htmltemplate);         



        $message->setFrom(array($from[0] => $from[1]))
        ->setTo($to)
        ->setBody($body, 'text/html');


        if (!$mailer->send($message, $failures)){
          return "Failures:";
          return print_r($failures);
        }else{
            return 'success';
        }

    }
}

它将在我的模板中渲染图像:

<td id="head" height="80" width="640" colspan="3" valign="top" align="left">
<image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>         
</td>

I'm sending HTML mails with swiftmailer. It all works pretty great, my HTML mails are showing up just fine in all clients. I have quite the knowledge of building email templates. Sending them myself not so much...

The problem exists in gmail and hotmail. The image is not showing up. It shows just fine in Thunderbird for instance... gmail is leaving my image blank and adds it as an attachment (at the bottom of the mail, like i'm sending birthday pictures..).

Any ideas?

I've got the following code:

function deliverMail($subject, $data, $from, $to, $template){
    if($_SERVER['SERVER_ADDR'] !== '::1'){

        if (!is_array($to)){
            $to = array($to);       
        }           
        $from = explode(",", $from);        

        //Create the Transport
        $transport = Swift_SmtpTransport::newInstance('mail.xxx.nl', 25)
          ->setUsername('xxx')
          ->setPassword('xxx');           

        $mailer = Swift_Mailer::newInstance($transport);
        $message = Swift_Message::newInstance($subject);
        $image = $message->embed(Swift_Image::fromPath($_SERVER['DOCUMENT_ROOT'].'/templates/emails/xxx.jpg'));


        // open the file
        $sourcefile     = $_SERVER['DOCUMENT_ROOT'].'/templates/emails/'.$template.'.html.tpl';
        $fh             = fopen($sourcefile, 'r');
        $htmltemplate       = fread($fh, filesize($sourcefile));
        fclose($fh);

        // set the content  
        if($template == 'confirm'){$replaceOld = array("[*code*]", "[*imgsrc*]");}          
        if($template == 'notify'){$replaceOld = array("[*url*]", "[*imgsrc*]");}

        $replaceNew     = array($data, $image);         
        $body           = str_replace($replaceOld, $replaceNew, $htmltemplate);         



        $message->setFrom(array($from[0] => $from[1]))
        ->setTo($to)
        ->setBody($body, 'text/html');


        if (!$mailer->send($message, $failures)){
          return "Failures:";
          return print_r($failures);
        }else{
            return 'success';
        }

    }
}

Which will render the image in my template here:

<td id="head" height="80" width="640" colspan="3" valign="top" align="left">
<image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>         
</td>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-10-23 11:29:27

我也在 GMail 中看到了这种行为,并认为这就是 GMail 显示带有嵌入图像的邮件的方式。

请参阅此链接了解更多信息:
http://www.getelastic.com/ Gmail 电子邮件设计/

您知道 Gmail 会禁用
默认情况下 HTML 电子邮件中的图像,甚至
如果您的客户已将您添加到他的
或者她的安全名单?

I have seen this behaviour in GMail as well and think this is just how GMail displays mail with embedded images.

See this link for more information:
http://www.getelastic.com/email-design-for-gmail/

Did you know that Gmail disables
images in HTML emails by default, even
if your customer has added you to his
or her safe list?

因为看清所以看轻 2024-10-23 11:29:27

哇,终于解决了这个问题。 “image”当然不是一个有效的 HTML 标签...在尝试了 3 个不同的 php 库后我发现了这个..

  <td id="head" height="80" width="640" colspan="3" valign="top" align="left">
    <image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>         
    </td>

Wow, finally settled this. "image" is not a valid HTML tag of course... after trying 3 different php libraries i found this..

  <td id="head" height="80" width="640" colspan="3" valign="top" align="left">
    <image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>         
    </td>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文