gpt4 book ai didi

Node.js 使用 nodemailer 发送带有图像附件的电子邮件

转载 作者:搜寻专家 更新时间:2023-11-01 00:15:45 34 4
gpt4 key购买 nike

我正在尝试从发布请求发送电子邮件。我正在使用 Express 和 nodemailer。我被“fs”弄糊涂了 我的电子邮件正在发送,但图像未作为附件包含在内。我检查了文档,但它们似乎都发送了静态文件,而不是从表单请求中流式传输的文件。

var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "gmail_address@gmail.com",
pass: "password_for_gmail_address"
}
});

app.get('/', function(req, res){
res.send('<form method="post" enctype="multipart/form-data">'
+ '<p>Post Title: <input type="text" name="title"/></p>'
+ '<p>Post Content: <input type="text" name="content"/></p>'
+ '<p>Image: <input type="file" name="image"/></p>'
+ '<p><input type="submit" value="Upload"/></p>'
+ '</form>');
})

app.post('/', function(req, res, next){
var mailOptions = {
from: "gmail_address@gmail.com", // sender address
to: "somebodyelse@example.com", // list of receivers
subject: req.body.title, // Subject line
text: req.body.content, // plaintext body
attachments:[
{
fileName: req.body.title,
streamSource: req.files.image
}
]
}

smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.send('Failed');
}else{
console.log("Message sent: " + response.message);
res.send('Worked');
}
});
});

最佳答案

这对我有用:

attachments: [{   // stream as an attachment
filename: 'image.jpg',
content: fs.createReadStream('/complete-path/image.jpg')
}]

关于Node.js 使用 nodemailer 发送带有图像附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12888817/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com