javascript 在电子邮件正文中嵌入图像 nodemailer nodejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48449379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embed image in email body nodemailer nodejs
提问by Teebo
I am following the approach used in the nodemailer community sitebut I cannot seem to get it to work as I am getting the error
我正在遵循 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' }
The nodemailer options are as follows
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
};
And in the emailBodyvariable I have a template string with an image tag line like so
在emailBody变量中,我有一个带有图像标记行的模板字符串,如下所示
<img style="width:250px;" cid:unique@cid>
Do I maybe need to set the static assets for express or what, the image file is in the same folder as the file that has the above code, any help is appreciated
我可能需要为express设置静态资产还是什么,图像文件与具有上述代码的文件位于同一文件夹中,任何帮助表示赞赏
采纳答案by Teebo
So I got it to work by using ...
所以我通过使用它来工作......
path: __dirname + '/rello-logo-full-svg.svg',
....
....
But funny this is not what I was trying to achieve because I wanted the image to be in the email body, bu hope this'll help someone else.
但有趣的是,这不是我想要实现的目标,因为我希望图像出现在电子邮件正文中,但希望这会对其他人有所帮助。
Hey, I just changed the file name from .svgto .png, another mistake I made was with the image in the template, I have changed it to
嘿,我刚刚将文件名从.svg更改为.png,我犯的另一个错误是模板中的图像,我已将其更改为
<img style="width:250px;" src="cid:unique@cid">
回答by RatKing
Responding here in case anyone else encounters this! The __dirnameabove was really helpful, but this is my code to actually see the image embedded in the email
在这里回复以防其他人遇到这个!在__dirname上面的是真正有用的,但是这是我的代码真正看到嵌入在电子邮件中的图像
My img tag:
我的 img 标签:
<img src="cid:logo">
My attachments snippet:
我的附件片段:
attachments: [{
filename: 'Logo.png',
path: __dirname +'/folder/Logo.png',
cid: 'logo' //my mistake was putting "cid:logo@cid" here!
}]
回答by ratskin
I found a great example of this at https://community.nodemailer.com/using-embedded-images/. Here is the post
我在https://community.nodemailer.com/using-embedded-images/找到了一个很好的例子。这是帖子
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:[email protected]"/>', attachments: [{ filename: 'image.png', path: '/path/to/file', cid: '[email protected]' //same cid value as in the html img src }] }
使用嵌入图像
附件可用作 HTML 正文中的嵌入图像。要使用此功能,您需要设置附件的附加属性——cid(文件的唯一标识符),它是对附件文件的引用。必须使用相同的 cid 值作为 HTML 中的图像 URL(使用 cid: 作为 URL 协议,请参见下面的示例)。
注意!cid 值应该尽可能唯一!
var mailOptions = { ... html: 'Embedded image: <img src="cid:[email protected]"/>', attachments: [{ filename: 'image.png', path: '/path/to/file', cid: '[email protected]' //same cid value as in the html img src }] }

