问题是我的代码在本地可以正常运行,但是在heroku中,我没有改变我调用sendmail函数的方式,它仍然可以完成
试图以不同的方式调用我的函数,没有任何作用
我希望让控制台发送日志,然后知道外发邮件已成功发送
app.post('/', emailUtility.sendMail, function(req, res) {
res.send("recieved your request!");
});
var nodemailer = require('nodemailer');
exports.sendMail = function(req, res, next) {
var input = JSON.parse(JSON.stringify(req.body));
var data = {
persons_name: input.name,
personal_message: input.message
};
console.log("name :" + data.persons_name);
console.log("message :" + data.personal_message);
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: process.env.USERNAME, // generated ethereal user
pass: process.env.PASS // generated ethereal password
}
});
// setup email data with unicode symbols
let mailOptions = {
from: data.persons_name + '" " <example@gmail.com>', // sender address
to: process.env.MAILTOSENDTO, // list of receivers
subject: 'Art Life Clothing ✔', // Subject line
text: data.personal_message, // plain text body
html: '<b>' + data.personal_message + '</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
next()
}
at=info method=POST path="/" host=calm-island-58460.herokuapp.com request_id=ca397146-1296-42a8-a505-b2c6ebb1543a fwd="197.229.3.109" dyno=web.1 connect=0ms service=152ms status=200 bytes=222 protocol=https
2018-12-30T17:06:32.673329+00:00 app[web.1]: dns.js:229
2018-12-30T17:06:32.673336+00:00 app[web.1]: throw new Error('"callback" argument must be a function');
2018-12-30T17:06:32.673338+00:00 app[web.1]: ^
2018-12-30T17:06:32.673340+00:00 app[web.1]:
2018-12-30T17:06:32.673344+00:00 app[web.1]: Error: "callback" argument must be a function
2018-12-30T17:06:32.673346+00:00 app[web.1]: at Object.query [as resolve4] (dns.js:229:13)
2018-12-30T17:06:32.673348+00:00 app[web.1]: at resolver (/app/node_modules/nodemailer/lib/shared/index.js:15:28)
2018-12-30T17:06:32.673350+00:00 app[web.1]: at Object.module.exports.resolveHostname (/app/node_modules/nodemailer/lib/shared/index.js:55:5)
2018-12-30T17:06:32.673351+00:00 app[web.1]: at SMTPConnection.connect (/app/node_modules/nodemailer/lib/smtp-connection/index.js:314:27)
2018-12-30T17:06:32.673353+00:00 app[web.1]: at getSocket (/app/node_modules/nodemailer/lib/smtp-transport/index.js:262:24)
2018-12-30T17:06:32.673355+00:00 app[web.1]: at Immediate.setImmediate (/app/node_modules/nodemailer/lib/smtp-transport/index.js:70:35)
2018-12-30T17:06:32.673357+00:00 app[web.1]: at runCallback (timers.js:570:20)
2018-12-30T17:06:32.673358+00:00 app[web.1]: at tryOnImmediate (timers.js:550:5)
2018-12-30T17:06:32.673360+00:00 app[web.1]: at processImmediate [as _immediateCallback] (timers.js:529:5)
2018-12-30T17:06:32.767524+00:00 heroku[web.1]: State changed from up to crashed
2018-12-30T17:06:32.749958+00:00 heroku[web.1]: Process exited with status 1
答案 0 :(得分:0)
next()调用不应该在transporter.sendMai()内部吗?因为这是异步代码,并且花费时间来获取响应,所以在localhost中更快。
我认为使用这段代码可能会起作用。
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
next(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
next()
});