【问题标题】:Embedded image in HTML mail shows attached in stead of in templateHTML 邮件中的嵌入图像显示附件而不是模板
【发布时间】:2011-06-23 02:25:28
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: php email mime swiftmailer


    【解决方案1】:

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

    查看此链接了解更多信息:
    http://www.getelastic.com/email-design-for-gmail/

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

    【讨论】:

    • 一种解决方案是链接到外部服务器。收件人必须允许图像显示。这就是我认为上面的文章的内容。我想附上图像以逃避这种行为。使用 PHPmailer 我已经能够做到这一点。我不确定为什么它不适用于 SwiftMailer。如果我错了,请纠正我..
    【解决方案2】:

    哇,终于解决了。 “图像”当然不是一个有效的 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>
    

    【讨论】:

    • 所以你使用了&lt;img src="" ...&gt; 而不是&lt;image src="" ...&gt;
    • 是的,我就是这么做的
    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2017-08-31
    • 2010-10-29
    • 2011-04-16
    相关资源
    最近更新 更多