Dan*_*alV 16 email node.js nodemailer
我有代码在nodejs中发送带有nodemailer的电子邮件,但我想将文件附加到电子邮件但是我找不到办法在网上搜索但是我找不到有用的东西.我有什么方法可以附加文件到那个或任何可以帮助我用nodemailer附加文件的资源?
var nodemailer = require('nodemailer');
var events = require('events');
var check =1;
var events = new events.EventEmitter();
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "gmail",
auth: {
user: "example@gmail.com",
pass: "pass"
}
});
function inputmail(){
///////Email
const from = 'example<example@gmail.com>';
const to = 'example@yahoo.com';
const subject = 'example';
const text = 'example email';
const html = '<b>example email</b>';
var mailOption = {
from: from,
to: to,
subject: subject,
text: text,
html: html
}
return mailOption;
}
function send(){
smtpTransport.sendMail(inputmail(),function(err,success){
if(err){
events.emit('error', err);
}
if(success){
events.emit('success', success);
}
});
}
///////////////////////////////////
send();
events.on("error", function(err){
console.log("Mail not send");
if(check<10)
send();
check++;
});
events.on("success", function(success){
console.log("Mail send");
});
Run Code Online (Sandbox Code Playgroud)
小智 51
在var mailOption中包含关键附件,如下所示:
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='
}
]
Run Code Online (Sandbox Code Playgroud)
}
选择适合您需求的选项.
链接:Nodemailer Repository GitHub
祝好运!!
我已经测试了这些附件中的每一种方法,但没有一种方法适合我.这是没有smtp传输配置的邮件程序功能代码:
function mailer(from, to, subject, attachments, body) {
// Setup email
var mailOptions = {
from: from,
to: to,
subject: subject,
attachments: attachments,
html: body
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
if(error) console.log(error);
else console.log("Message sent: " + response.message);
// shut down the connection pool, no more messages
smtpTransport.close();
});
}
Run Code Online (Sandbox Code Playgroud)
然后电话:
var attachments = [{ filename: 'test.pdf', path: __dirname + '/pdf/test.pdf', contentType: 'application/pdf' }];
mailer("exped@gmail.com", "mymail@gmail.com", "Test", attachments, "<h1>Hello</h1>");
Run Code Online (Sandbox Code Playgroud)
邮件成功但没有附件.即使我设置了字符串或缓冲区附件,也会产生相同的结果.
您的代码几乎是正确的,只需添加“附件”属性即可将文件附加到邮件中,
您的邮件选项:
var mailOption = {
from: from,
to: to,
subject: subject,
text: text,
html: html
}
Run Code Online (Sandbox Code Playgroud)
只需添加附件即可
var mailOption = {
from: from,
to: to,
subject: subject,
text: text,
html: html,
attachments: [{
filename: change with filename,
path: change with file path
}]
}
Run Code Online (Sandbox Code Playgroud)
附件还提供了一些其他方式来附加文件以获取更多信息,请在此处查看 NodeMailer 社区的文档
| 归档时间: |
|
| 查看次数: |
35788 次 |
| 最近记录: |