我想附加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上读了一些与我有类似错误的问题。
我固定路径 - &gt;文件路径 和固定的streamSource - &gt;路径 我的nodemailer版本是4.0.1。 帮我发送带zip文件的邮件。
答案 0 :(得分:1)
我使用完全相同版本的nodemailer(此时 4.0.1 )并且我正在发送带有附件的电子邮件。
您的第一个代码段看起来很有前途:)
但第二部分
我试过这些来附加文件
在这里输入代码
附件:[{ fileName :'test.log', streamSource :fs.createReadStream('。/ test.log'}]
看起来并不正确......
fileName 和 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!'
}
]
}
您可以看到应将 fileName 更改为文件名,将 streamSource 更改为内容
// WRONG
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}]
// RIGHT
attachments:[{ filename: 'test.log', content: fs.createReadStream('./test.log'}]
祝你好运!我希望这对你有帮助:))