我想附加zip文件。 但它对任何附件都不起作用。
这是我的源代码。
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 = 'test@test.com';
var mailOptions={
from : "test <test@test.com>",
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'}]
它仍然发送没有附件的邮件。当此代码无法读取文件时,会出现错误。所以我想这是行不通的,因为读取文件。我读了一些关于StackOverflow的问题,它与我有类似的错误。
我修复了路径 ->文件路径和固定流源 -> 路径我的节点邮件程序版本是4.0.1。帮我发送带有 zip 文件的邮件。
我使用的是完全相同版本的nodemailer(目前为4.0.1(,并且我已成功发送带有附件的电子邮件。
您的第一个代码片段看起来很有希望:)
但第二部分
我尝试了这些来附加文件
在此处输入代码
附件:[{ 文件名: 'test.log', streamSource: fs.createReadStream('./test.log'}]
看起来一点都不对劲...
请参考节点邮件文档
文件名和流源不是邮件选项对象的有效参数
文档中的示例
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/plainrn' +
'Content-Disposition: attachment;rn' +
'rn' +
'Hello world!'
}
]
}
如您所见,您应该将文件名更改为文件名并将流源更改为内容
// WRONG
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
// RIGHT
attachments:[{ filename: 'test.log', content: fs.createReadStream('./test.log'}]
祝你好运!我希望这对你有帮助:)