我有一个在nodemailer的NodeJS发送电子邮件的代码,但我想文件附加到电子邮件,但我不能找到办法做到这一点我搜索的净,但我便无法找到useful.Is那里,我可以附加任何方式文件与或能帮助我附上文件,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: "[email protected]",
pass: "pass"
}
});
function inputmail(){
///////Email
const from = 'example<[email protected]>';
const to = '[email protected]';
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");
});
包括在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='
}
]
}
选择调整到您需要的选项。
链接: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();
});
}
然后调用:
var attachments = [{ filename: 'test.pdf', path: __dirname + '/pdf/test.pdf', contentType: 'application/pdf' }];
mailer("[email protected]", "[email protected]", "Test", attachments, "<h1>Hello</h1>");
邮件来自成功,但没有附着物。即使我设置一个字符串或缓冲区附件是同样的结果。
如果您是通过选择在邮件撰写Constructor对象和依恋是HTTP服务器上,那么它应该是这样的:
const options = {
attachments = [
{ // use URL as an attachment
filename: 'xxx.jpg',
path: 'http:something.com/xxx.jpg'
}
]
}
该替代解决方案是在线托管图片使用CDN并链接到你的HTML,例如在线图像源。 <img src="list_image_url_here">
。
(我不得不使用nodemailer 2.6.0版,这就是为什么我想通了,这种解决方法nodemailer的形象嵌入的问题。)
这种方案的好处是,你不发送附件nodemailer,所以发送过程更加简化。
var express = require('express');
var router = express(),
multer = require('multer'),
upload = multer(),
fs = require('fs'),
path = require('path');
nodemailer = require('nodemailer'),
directory = path.dirname("");
var parent = path.resolve(directory, '..');
// your path to store the files
var uploaddir = parent + (path.sep) + 'emailprj' + (path.sep) + 'public' + (path.sep) + 'images' + (path.sep);
/* GET home page. */
router.get('/', function(req, res) {
res.render('index.ejs', {
title: 'Express'
});
});
router.post('/sendemail', upload.any(), function(req, res) {
var file = req.files;
console.log(file[0].originalname)
fs.writeFile(uploaddir + file[0].originalname, file[0].buffer, function(err) {
//console.log("filewrited")
//console.log(err)
})
var filepath = path.join(uploaddir, file[0].originalname);
console.log(filepath)
//return false;
nodemailer.mail({
from: "yourgmail.com",
to: req.body.emailId, // list of receivers
subject: req.body.subject + " ✔", // Subject line
html: "<b>" + req.body.description + "</b>", // html body
attachments: [{
filename: file[0].originalname,
streamSource: fs.createReadStream(filepath)
}]
});
res.send("Email has been sent successfully");
})
module.exports = router;
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
}
}
});
您的代码几乎是正确的,只需要添加,“附件”属性为您的邮件文件附加,
您的邮件选项:
var mailOption = {
from: from,
to: to,
subject: subject,
text: text,
html: html
}
只需添加附件一样
var mailOption = {
from: from,
to: to,
subject: subject,
text: text,
html: html,
attachments: [{
filename: change with filename,
path: change with file path
}]
}
附件还提供了一些其他的方式来连接文件的详细信息检查nodemailer社会的文档HERE