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

Tee*_*ebo 11 javascript node.js express nodemailer

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

   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' }
Run Code Online (Sandbox Code Playgroud)

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
    };
Run Code Online (Sandbox Code Playgroud)

emailBody变量中,我有一个带有图像标记行的模板字符串

<img style="width:250px;" cid:unique@cid>
Run Code Online (Sandbox Code Playgroud)

我是否可能需要为express设置静态资产或者什么,图像文件与具有上述代码的文件位于同一文件夹中,任何帮助表示赞赏

Rat*_*ing 15

如果其他人遇到这个,请回复此处!在__dirname上面的是真正有用的,但是这是我的代码真正看到嵌入在电子邮件中的图像

我的img标签:

<img src="cid:logo">
Run Code Online (Sandbox Code Playgroud)

我的附件片段:

attachments: [{
     filename: 'Logo.png',
     path: __dirname +'/folder/Logo.png',
     cid: 'logo' //my mistake was putting "cid:logo@cid" here! 
}]
Run Code Online (Sandbox Code Playgroud)

  • *至关重要*是路径_必须包含文件名_!文档没有显示这一点。 (2认同)

Tee*_*ebo 5

所以我通过使用...使其工作

 path: __dirname + '/rello-logo-full-svg.svg',
Run Code Online (Sandbox Code Playgroud)

....

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

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

 <img style="width:250px;" src="cid:unique@cid">
Run Code Online (Sandbox Code Playgroud)


rat*_*kin 5

我在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
    }]
}
Run Code Online (Sandbox Code Playgroud)


小智 5

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" />
Run Code Online (Sandbox Code Playgroud)

在图像标签中,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" />
Run Code Online (Sandbox Code Playgroud)

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