在 node.js 中发送带有图片附件的电子邮件
Send email with image attachment in node.js
目前我可以使用以下代码在 node.js 中发送电子邮件:
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "gmailpass"
}
});
smtpTransport.sendMail({
from: "My Name <me@example.com>", // sender address
to: "Your Name <you@example.com>", // comma separated list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔" // plaintext body
}, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
如何在此电子邮件中发送从 html 表单 上传的图片的附件?另外,我可以在不上传到服务器的情况下发送电子邮件中的图像吗?如果没有,没关系。这是我的 html 表格:
<form id="mainForm">
<input type="file" id="fileUpload">
<input type="submit" id="submit" name="submit">
</form>
如何获取文件并将其包含在我使用 node.js 发送的电子邮件中?
你可以尝试这样的事情。使用 busboy 获取文件,然后在获取文件后将其转换为 base64 并将其添加到附件 属性 以供您选择邮件选项。唯一的问题是我不知道文件参数是否作为缓冲区返回。如果不是,您只需要将该文件转换为 base 64 即可将其作为附件发送
var app = express();
var Busboy = require('busboy');
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "gmailpass"
}
});
app.post('/email', function(req, res){
var busboy = new Busboy({ headers: req.headers });
var attachments = [];
var mailOptions = {
from: "My Name <me@example.com>", // sender address
to: "Your Name <you@example.com>", // comma separated list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔" // plaintext body
};
busboy
.on('file', function(fieldname, file, filename, encoding, mimetype){
attachments.push({
filename: filename,
content: file.toString('base64'),
encoding: 'base64'
});
})
.on('finish', function() {
mailOptions.attachments = attachments;
smtpTransport.sendMail(mailOptions, function (err, info) {
if (err) {
//handle error
}
// email sent
});
});
});
目前我可以使用以下代码在 node.js 中发送电子邮件:
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "gmailpass"
}
});
smtpTransport.sendMail({
from: "My Name <me@example.com>", // sender address
to: "Your Name <you@example.com>", // comma separated list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔" // plaintext body
}, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
如何在此电子邮件中发送从 html 表单 上传的图片的附件?另外,我可以在不上传到服务器的情况下发送电子邮件中的图像吗?如果没有,没关系。这是我的 html 表格:
<form id="mainForm">
<input type="file" id="fileUpload">
<input type="submit" id="submit" name="submit">
</form>
如何获取文件并将其包含在我使用 node.js 发送的电子邮件中?
你可以尝试这样的事情。使用 busboy 获取文件,然后在获取文件后将其转换为 base64 并将其添加到附件 属性 以供您选择邮件选项。唯一的问题是我不知道文件参数是否作为缓冲区返回。如果不是,您只需要将该文件转换为 base 64 即可将其作为附件发送
var app = express();
var Busboy = require('busboy');
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "gmailpass"
}
});
app.post('/email', function(req, res){
var busboy = new Busboy({ headers: req.headers });
var attachments = [];
var mailOptions = {
from: "My Name <me@example.com>", // sender address
to: "Your Name <you@example.com>", // comma separated list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔" // plaintext body
};
busboy
.on('file', function(fieldname, file, filename, encoding, mimetype){
attachments.push({
filename: filename,
content: file.toString('base64'),
encoding: 'base64'
});
})
.on('finish', function() {
mailOptions.attachments = attachments;
smtpTransport.sendMail(mailOptions, function (err, info) {
if (err) {
//handle error
}
// email sent
});
});
});