我正在为一家企业创建一个node.js网站,希望每次有人提出申请时都可以通过电子邮件通知。每次提交工作申请表时,我都使用nodemailer和mailgun发送电子邮件。电子邮件正在发送,但是它不包含我创建的申请人对象的键值对。任何帮助将不胜感激!
这是我正在运行的nodemailer代码
const nodemailer = require('nodemailer');
const mailgun = require('nodemailer-mailgun-transport');
const debug = require('debug')('app:mail');
const auth = {
auth: {
api_key: '**************',
domain: '***************'
}
};
const transporter = nodemailer.createTransport(mailgun(auth));
function sendOrderEmail(applicant) {
let html = '<ul>';
Object.entries(applicant).forEach(([key, value]) => {
html += `<li>${key}: ${value}</li>`;
});
html += '</ul>';
const mailOptions = {
from: '*************',
to: '*********, *************',
subject: '*****************',
html
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
debug(`Error: ${err}`);
} else {
debug(`Info: ${info}`);
}
});
}
module.exports = sendOrderEmail;
这是我创建申请人对象的邮寄路线
app.post('/employment', function(req, res){
var firstName = req.body.firstName;
var middleInitial = req.body.middleInitial;
var lastName = req.body.lastName;
var address = req.body.address;
var city = req.body.city;
var state = req.body.state;
var zipCode = req.body.zipCode;
var phoneNumber = req.body.phoneNumber;
var doYouRecieveText = req.body.doYouRecieveText;
var newApplicant = {
firstName: firstName,
middleInitial: middleInitial,
lastName: lastName,
address: address,
city: city,
state: state,
zipCode: zipCode,
phoneNumber: phoneNumber,
doYouRecieveText: doYouRecieveText
};
Applicant.create(newApplicant, function(err, newlyCreated){
if(err) {
console.log(err);
} else {
console.log(newlyCreated);
sendOrderEmail(newlyCreated);
res.redirect('/');
}
});
});
答案 0 :(得分:1)
您似乎试图在HTML中插入的value
是一个对象,但是HTML期望使用String类型的值。
在将value
插入html之前,先对其进行字符串化。
html += `<li>${key}: ${ typeof value === 'string' ? value : JSON.stringify(value)}</li>`;
答案 1 :(得分:0)
当我本应通过newApplicant变量时,我正在将newCreated参数传递给sendOrderEmail函数
Applicant.create(newApplicant, function(err, newlyCreated){
if(err) {
console.log(err);
} else {
console.log(newlyCreated);
sendOrderEmail(newApplicant);
res.redirect('/');
}
});