在电子邮件正文中嵌入图像 nodemailer nodejs

Embed image in email body nodemailer nodejs

我正在遵循 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 设置静态资源还是什么,图像文件与具有上述代码的文件位于同一文件夹中,感谢您的帮助

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

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

.....

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

嘿,我刚刚将文件名从 .svg 更改为 .png,我犯的另一个错误是图像中的模板,我已将其更改为

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

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

我的 img 标签:

<img src="cid:logo">

我的附件片段:

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

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

Using Embedded Images

Attachments can be used as embedded images in the HTML body. To use this feature, you need to set additional property of the attachment – cid (unique identifier of the file) which is a reference to the attachment file. The same cid value must be used as the image URL in HTML (using cid: as the URL protocol, see example below).

NB! the cid value should be as unique as possible!

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
    }]
}

NodeMailer 的 HTML 图片附件

For HTML template images in Nodemailer, There are a couple of things to take into consideration or to pay close attention to is:

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" />

in the image tag, src should contain the relevant image's name which we gave in attachment object's cid property name.

重要

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

Ans: 那么请确保,您在附件中提供的详细信息图像:[{ ...}] array of object 都用于 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" />

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