通过 NodeJS 发送带附件的邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4672903/
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
Sending mails with attachment via NodeJS
提问by sdepold
Is there any library for NodeJS for sending mails with attachment?
是否有用于发送带附件邮件的 NodeJS 库?
回答by Philippe
Yes, it is pretty simple,
I use nodemailer: npm install nodemailer --save
是的,这很简单,我使用 nodemailer: npm install nodemailer --save
var mailer = require('nodemailer');
mailer.SMTP = {
host: 'host.com',
port:587,
use_authentication: true,
user: '[email protected]',
pass: 'xxxxxx'
};
Then read a file and send an email :
然后读取文件并发送电子邮件:
fs.readFile("./attachment.txt", function (err, data) {
mailer.send_mail({
sender: '[email protected]',
to: '[email protected]',
subject: 'Attachment!',
body: 'mail content...',
attachments: [{'filename': 'attachment.txt', 'content': data}]
}), function(err, success) {
if (err) {
// Handle error
}
}
});
回答by omolina
Try with nodemailer, for example try this:
尝试使用 nodemailer,例如试试这个:
var nodemailer = require('nodemailer');
nodemailer.SMTP = {
host: 'mail.yourmail.com',
port: 25,
use_authentication: true,
user: '[email protected]',
pass: 'somepasswd'
};
var message = {
sender: "[email protected]",
to:'[email protected]',
subject: '',
html: '<h1>test</h1>',
attachments: [
{
filename: "somepicture.jpg",
contents: new Buffer(data, 'base64'),
cid: cid
}
]
};
finally, send the message
最后,发送消息
nodemailer.send_mail(message,
function(err) {
if (!err) {
console.log('Email send ...');
} else console.log(sys.inspect(err));
});
回答by bpierre
Have you tried Nodemailer?
你试过Nodemailer吗?
Nodemailer supports
- Unicode to use any characters
- HTML contents as well as plain text alternative
- Attachments
- Embedded images in HTML
- SSL (but not STARTTLS)
Nodemailer 支持
- Unicode 使用任何字符
- HTML 内容以及纯文本替代
- 附件
- HTML 中的嵌入图像
- SSL(但不是 STARTTLS)
回答by kilianc
Personally i use Amazon SESrest API or Sendgridrest API which is the most consistent way to do it.
我个人使用Amazon SESrest API 或Sendgridrest API,这是最一致的方法。
If you need a low level approach use https://github.com/Marak/node_mailerand set up your own smtp server (or one you have access too)
如果您需要低级方法,请使用https://github.com/Marak/node_mailer并设置您自己的 smtp 服务器(或您也可以访问的服务器)
回答by Anatoliy
You may use nodejs-phpmailer
您可以使用nodejs-phpmailer
回答by chilts
You can also use AwsSum's Amazon SES library:
您还可以使用 AwsSum 的 Amazon SES 库:
In there, there is an operation called SendEmail and SendRawEmail, the latter of which can send attachments via the service.
在那里,有一个名为 SendEmail 和 SendRawEmail 的操作,后者可以通过该服务发送附件。
回答by David
Another alternative library to try is emailjs.
另一个可以尝试的替代库是emailjs。
I gave some of the suggestions here a try myself but running code complained that send_mail() and sendMail() is undefined (even though I simply copy & pasted code with minor tweaks). I'm using node 0.12.4 and npm 2.10.1. I had no issues with emailjs, that just worked off the shelf for me. And it has nice wrapper around attachments, so you can attach it various ways to your liking and easily, compared to the nodemailer examples here.
我在这里尝试了一些建议,但运行代码时抱怨 send_mail() 和 sendMail() 未定义(即使我只是简单地复制和粘贴代码并稍作调整)。我正在使用节点 0.12.4 和 npm 2.10.1。我对 emailjs 没有任何问题,这对我来说是现成的。并且它对附件有很好的包装,因此与此处的 nodemailer 示例相比,您可以根据自己的喜好轻松地以各种方式附加它。
回答by Krunal Goswami
use mailer package it is very flexible and easy.
使用 mailer 包非常灵活和容易。
回答by Ari Porad
I haven't used it but nodemailer(npm install nodemailer) looks like what you want.
我没有用过它,但是 nodemailer( npm install nodemailer) 看起来像你想要的。
回答by Luis Sanchez
Send With express-mailer (https://www.npmjs.com/package/express-mailer)
使用 express-mailer ( https://www.npmjs.com/package/express-mailer)发送
Send PDF -->
发送 PDF -->
var pdf="data:application/pdf;base64,JVBERi0xLjM..etc"
attachments: [ { filename: 'archive.pdf',
contents: new Buffer(pdf.replace(/^data:application\/(pdf);base64,/,''), 'base64')
}
]
Send Image -->
发送图片-->
var img = 'data:image/jpeg;base64,/9j/4AAQ...etc'
attachments: [
{
filename: 'myImage.jpg',
contents: new Buffer(img.replace(/^data:image\/(png|gif|jpeg);base64,/,''), 'base64')
}
]
Send txt -->
发送txt-->
attachments: [
{
filename: 'Hello.txt',
contents: 'hello world!'
}
]

