nodemailer附件不起作用
问题描述:
我想附上zip文件。但它不起作用任何附件。nodemailer附件不起作用
这是我的源代码。
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var fs = require('fs');
var mailinfo = require('../config/mail_info').info;
var smtpTransport = nodemailer.createTransport({
host: mailinfo.host,
port: mailinfo.port,
auth: mailinfo.auth,
tls: mailinfo.tls,
debug: true,
});
router.post('/',function(req,res){
var emailsendee = req.body.emailAddress;
console.log(emailsendee);
var emailsubject = "Requested File";
var emailText = "test";
var emailFrom = '[email protected]';
var mailOptions={
from : "test <[email protected]>",
to : emailsendee,
subject : emailsubject,
html : '<h1>' + emailText+ '</h1>';
attachments : [
{
filename : '',//i just put black make you understand esaily
path : ''//what i did is under this code
}
]
};
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end();
}else{
console.log(response);
res.end();
}
});
});
module.exports = router;
我试图为这些附加文件
enter code here
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
它仍然发送邮件,而附接。 当此代码无法读取文件时出现错误。 所以我想这是不工作,因为阅读文件。 和我读了一些关于*的问题,它和我有类似的错误。
我固定路径 - >文件路径 和固定streamSource - >路径 我的nodemailer版本是4.0.1。 帮我发邮件给zip文件。
答
我使用完全相同版本的nodemailer(4.0.1此刻),并且我正在成功发送带有附件的电子邮件。
你的第一个代码片段看起来很有希望:)
但第二部分
我想这些对于附加文件
输入代码在这里
附件:[{文件名:'test.log',streamSource:fs.createReadStream('./ test.log'}]
不看的权利在所有...
文件名和StreamSource的没有的mailOptions对象
一个有效参数DOCS示例
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/nodemailer/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='
},
{
// use pregenerated MIME node
raw: 'Content-Type: text/plain\r\n' +
'Content-Disposition: attachment;\r\n' +
'\r\n' +
'Hello world!'
}
]
}
,你可以看到你应该改变文件名到名和StreamSource的到内容
// WRONG
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
// RIGHT
attachments:[{ filename: 'test.log', content: fs.createReadStream('./test.log'}]
祝您好运!我希望这对你有所帮助:)