I got this node.js code as an IBM Cloud function to let a user of my Watson Assistant send me a reminder via email. Works Great.
var nodemailer = require('nodemailer');
let smtpConfig = {
host: 'hosthere', // you can also use smtp.gmail.com
port: porthere,
secure: false, // use TLS
auth: {
user: 'emailhere',
pass: 'passwordhere'
}
};
function main(params) {
return new Promise(function (resolve, reject) {
let response = {
code: 200,
msg: 'E-mail was sent successfully!'
};
if (!params.reminder) {
response.msg = "Error: Reminder was not provided.";
response.code = 400;
}
else if (!params.email) {
response.msg = "Error: Destination e-mail was not provided.";
response.code = 400;
}
else if (!params.conversation_id) {
response.msg = "Error: Conversation id was not provided.";
response.code = 400;
}
if (response.code != 200) {
reject(response);
}
console.log(`Validation was successful, preparing to send email...`);
sendEmail(params, function (email_response) {
response.msg = email_response['msg'];
response.code = email_response['code'];
response.reason = email_response['reason'];
console.log(`Email delivery response: (${email_response['code']}) ${response.msg}`);
resolve(response);
});
});
}
function sendEmail(params, callback) {
let transporter = nodemailer.createTransport(smtpConfig);
let mailOptions = {
from: `Watson Assistent Message <${smtpConfig.auth.user}>`,
to: params.email,
subject: `REMINDER: ${params.reminder}`,
text: `Do it!`
};
transporter.sendMail(mailOptions, function (error, info) {
let email_response = {
code: 200,
msg: 'Email was sent successfully',
reason: 'Success'
};
if (error) {
email_response.msg = 'Error';
email_response.code = 500;
email_response.reason = error;
}
else {
email_response.msg = info.response;
email_response.code = 200;
email_response.reason = info.response;
}
callback(email_response);
});
}
I just need to customize the response shown at ‚Results‘ after a successful request. So I can print to the user something like ‚Email sent successfully’ (if it actually was successful)
When calling the function it gives me this:
Results:
{
"code": 200,
"msg": "250 Requested mail action okay, completed: id=1N0Fxf-1lzPHJ457n-00xO6a",
"reason": "250 Requested mail action okay, completed: id=1N0Fxf-1lzPHJ457n-00xO6a"
}
Logs:
[
"2021-01-20T23:11:38.706021Z stdout: Validation was successful, preparing to send email...",
"2021-01-20T23:11:39.117946Z stdout: Email delivery response: (200) 250 Requested mail action okay, completed: id=1N0Fxf-1lzPHJ457n-00xO6a"
]
The thing is, this whole answer printed to „msg“ is nowhere defined in the code. Actually there is a defined answer for a successful request in the code, it just doesn’t get printed.
Why is that? How could I change that?
With the error response everything works fine. The defined answers from the code get printed at ‚results’ :
Results:
{
"error": {
"code": 400,
"msg": "Error: Destination e-mail was not provided."
}
}
Logs:
[
"2021-01-21T09:47:14.531598Z stdout: Validation was successful, preparing to send email..."
]
What is wrong with the success message?