【问题标题】:Embed image in email body nodemailer nodejs在电子邮件正文中嵌入图像 nodemailer nodejs
【发布时间】:2018-07-05 02:04:01
【问题描述】:

我正在遵循 nodemailer 社区中使用的方法site 但我似乎无法让它工作,因为我收到了错误

   Failed to send email { Error: ENOENT: no such file or directory, open 
'./rello-logo-full-svg.svg'
 errno: -2,
  code: 'ESTREAM',
  syscall: 'open',
  path: './rello-logo-full-svg.svg',
  command: 'API' }

nodemailer选项如下

    let mailOptions = {
        from: '<from_email>', 
        to: to_email_address.toString(),
        subject: 'Subject',
        text: 'Hello world', // plain text body
        attachments: [{
          filename: 'rello-logo-full-svg.svg',
          path: './rello-logo-full-svg.svg',
          cid: 'unique@cid'
      }],
        html: emailBody
    };

emailBody 变量中,我有一个带有图像标签行的模板字符串,如下所示

<img style="width:250px;" cid:unique@cid>

我可能需要为 express 设置静态资产还是什么,图像文件与具有上述代码的文件位于同一文件夹中,感谢任何帮助

【问题讨论】:

  • 错误很明显,找不到图像的路径。查看图片的完整路径。
  • @Fals 当然可以,但是引用图像文件的文件在同一个目录中,我还缺少什么,比如用 express 设置它吗?

标签: javascript node.js express nodemailer


【解决方案1】:

NodeMailer 的 HTML 图片附件

对于 Nodemailer 中的 HTML 模板图像,有几件事 考虑或密切关注的是:

attachments: [{
 filename: 'img.png',
 path: __dirname +'/folder/img.png', // path contains the filename, do not just give path of folder where images are reciding.
 cid: 'img' // give any unique name to the image and make sure, you do not repeat the same string in given attachment array of object.
}]

<img src="cid:img" />

在图片标签中,src 应该包含相关图片的名称 我们提供了附件对象的 cid 属性名称。

重要

如果您看到,很少有图片被视为电子邮件线程的附件,您不希望这些图片作为“附件”发送,

Ans:那么请确保您在附件中提供的详细信息:[{ ...}] 对象数组中的所有图像都用于 HTML 的 img src。

例如:

   attachment: [{
         filename: 'logo1.png',
         path: '/path/logo1.png',
         cid: 'logo1'
     },
     {
         filename: 'logo2.png',
         path: '/path/logo2.png',
         cid: 'logo2'
    }]



<img src="cid:logo2" />

在这种情况下,logo2 将绑定到 HTML 模板,而 logo1.png 将附加到电子邮件线程。

【讨论】:

    【解决方案2】:

    我在https://community.nodemailer.com/using-embedded-images/ 找到了一个很好的例子。这是帖子

    使用嵌入图像

    附件可以用作嵌入的图像 HTML 正文。要使用此功能,您需要设置的附加属性 附件 – cid(文件的唯一标识符),它是 参考附件文件。必须使用相同的 cid 值作为 HTML 中的图像 URL(使用 cid: 作为 URL 协议,参见示例 下面)。

    注意! cid 值应尽可能唯一!

    var mailOptions = {
        ...
        html: 'Embedded image: <img src="cid:unique@kreata.ee"/>',
        attachments: [{
            filename: 'image.png',
            path: '/path/to/file',
            cid: 'unique@kreata.ee' //same cid value as in the html img src
        }]
    }
    

    【讨论】:

    • 在 Outlook 中它也是附件。这样任何人都可以下载图像。有什么办法可以阻止
    • 很确定这与 Outlook 客户端有关,而不是实际的电子邮件。检查这些:msoutlook.info/question/72msoutlook.info/question/500
    • 其实我之前没明白你的意思。是的,所以如果你想嵌入它,你可以将图像转换为 base64 并包含 stackoverflow.com/questions/9110091/…。虽然我认为一些电子邮件客户端仍然会自动拉出任何图像。发件人方面对此您无能为力
    【解决方案3】:

    在这里回复以防其他人遇到此问题!上面的__dirname 确实很有帮助,但这是我实际查看电子邮件中嵌入图像的代码

    我的img标签:

    <img src="cid:logo">
    

    我的附件sn-p:

    attachments: [{
         filename: 'Logo.png',
         path: __dirname +'/folder/Logo.png',
         cid: 'logo' //my mistake was putting "cid:logo@cid" here! 
    }]
    

    【讨论】:

      【解决方案4】:

      所以我通过使用它来工作 ...

       path: __dirname + '/rello-logo-full-svg.svg',
      

      ....

      但有趣的是,这不是我想要实现的目标,因为我希望图像出现在电子邮件正文中,但希望这对其他人有所帮助。

      嘿,我刚刚把文件名从.svg改成了.png,我犯的另一个错误是模板中的图片,我把它改成了

       <img style="width:250px;" src="cid:unique@cid">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-16
        • 2016-09-07
        • 1970-01-01
        • 2019-12-12
        • 2013-08-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-06
        相关资源
        最近更新 更多