【问题标题】:How i can send image in email with cakephp 3 email template?我如何使用 cakephp 3 电子邮件模板在电子邮件中发送图像?
【发布时间】:2025-12-05 15:40:01
【问题描述】:

我正在尝试将图像发送到电子邮件中, 我的图片代码是:<?php echo $this->Html->image('image-name.jpg', ['class'=>'img-responsive']); ?>

当我在我的本地主机中调试电子邮件模板时,它工作正常,图像显示正确,但是当相同的代码使其在服务器上运行并触发电子邮件时,电子邮件内容正确显示但图像未显示(未找到)。

任何机构都会提出适当的实施方法。

谢谢, 马克

【问题讨论】:

    标签: php html email cakephp html-email


    【解决方案1】:

    先附上你要发送的文件

    $email->attachments([
        'photo.png' => [
            'file' => '/full/some_hash.png',
            'mimetype' => 'image/png',
            'contentId' => 'unique-id'
        ]
    ]);
    

    然后在您的电子邮件模板中,添加唯一 ID

     <img src="cid:unique-id">
    

    详细解释,here

    【讨论】:

    • 感谢您的重播,但我不想发送附件,我想设置徽标图像,这不适合我。
    • 注意:自 3.4.0 版起已弃用:使用 setAttachments() 而不是 attachments()。
    【解决方案2】:

    对于电子邮件或整个网站,如果您想获取内容的完整路径,请使用 fullBase 参数和 true 值,

    echo $this-&gt;Html-&gt;image("logo.png", ['fullBase' =&gt; true]);

    将输出:

    &lt;img src="http://example.com/img/logo.jpg" alt="" /&gt;

    https://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-images

    这是 CakePhp 框架建议的正确方式。

    【讨论】:

      最近更新 更多