![](/img/trans.png)
[英]Error when sending email using nodemailer / smtp.gmail.com
[英]NodeMailer attachments not sending with smtp/gmail
我使用 NodeMailer 创建了以下功能,它似乎可以毫无问题地发送电子邮件(控制台中的“已发送消息”通知和收到的电子邮件),除了没有发送任何电子邮件的附件!
用一堆电子邮件地址(gmail、谷歌应用程序、hotmail)尝试过,但都在做同样的事情。 请帮忙!
var sendWithAttachment = function(userMail, subject, html, attachment_path, cb){
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "labs@domain.com",
pass: "password"
}
});
var mailOptions = {
from: "Labs <labs@domain.com>",
to: userMail,
subject: subject || "[blank]"
html: html || "[none]"
generateTextFromHTML: true,
attachments: [
{ // filename and content type is derived from path
path: attachment_path
},
{ // utf-8 string as an attachment
filename: 'check.txt',
content: 'checking that some attachments work...'
},
],
};
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
cb(error, null);
}else{
console.log("Message sent: " + response.message);
cb(null, response);
}
smtpTransport.close();
});
};
这是 nodemailer 文档中的一个问题。 使用 'filePath' 更改 'path' 以定义路径并将文本的 'content' 更改为 'contents'。 为我工作。
var mailOptions = {
...
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
contents: 'hello world!'
},
{ // utf-8 string as an attachment
filename: 'text1.txt',
filePath: 'text.txt'
},
]
}
我通过将content
重命名为contents
解决了这个问题。 我正在阅读最新版本的nodemailer
的最新文档。 您可以在此处阅读1.0以下版本的文档: https : //github.com/andris9/Nodemailer/blob/0.7/README.md
var mailOptions = {
...
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
},
{ // binary buffer as an attachment
filename: 'text2.txt',
content: new Buffer('hello world!','utf-8')
},
{ // file on disk as an attachment
filename: 'text3.txt',
path: '/path/to/file.txt' // stream this file
},
{ // filename and content type is derived from path
path: '/path/to/file.txt'
},
{ // stream as an attachment
filename: 'text4.txt',
content: fs.createReadStream('file.txt')
},
{ // define custom content type for the attachment
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
},
{ // use URL as an attachment
filename: 'license.txt',
path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE'
},
{ // encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
{ // data uri as an attachment
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
}
]
}
// nodemailer 发送带附件的电子邮件的简单代码
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "test@gmail.com",
pass: "passport"
}
});
fs.readFile("path/logfile.txt", function (err, data) {
//creating simple built-in templating
var templateSender = {
from: 'MK <test@gmail.com>', // sender address
to: 'reciever@gmail.com', // list of receivers
subject: "Attachment", // Subject line
body: 'mail content...',
attachments: [{ filepath: 'path/logfile.txt', filename: 'logfile.txt', contents: data}]
};
// send mail with defined transport object
smtpTransport.sendMail(templateSender, function(error, success){
if(error){
return console.log(error);
}
smtpTransport.close();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.