I am trying to create a log of sent mails including some other details and need to access the info.response
object from the mail transporter in node, I have tried a number of things including trying to expose the info.response
as a global variable but I cannot seem to access it. I want to send a mail using the transporter then log some entry details and the info.response
code in the same line of the log.txt file. The resInfo
is not accessible outside of the transporter and the array clientReminderCandidates[x][1]
isn't accessible from within the transporter.
index.js
gm.clientReminders();
// clientReminderCandidates made available
if (clientReminderCandidates.length != 0) {
for (x = 0; x < clientReminderCandidates.length; x++) {
const clientReminders = require('./sendClientReminders.js')
clientReminders.sendRemindClient();
const updateReminders = require('./updateClientReminderSent.js')
updateReminders.updateClientReminder();
}
}
else { console.log("No client reminders to update") }
sendClientReminders.js
module.exports = {
sendRemindClient: function () {
var fs = require('fs');
fs.appendFile('logs/logs.txt', 'LOG Reminder sent to ' + clientReminderCandidates[x][10] + ' ' + resInfo + ' \r\n', function (err) { // cannot access resInfo outside transporter
if (err) throw err;
console.log('Reminder Sent and Logged!');
});
transporter.sendMail(mailOptions, function (error, info) {
resInfo = info; // I tried to reassign the `info` var from the transport function above
if (error) {
console.log(resInfo); // but this does not pull in the `info` var
console.log(resInfo);
console.log(info.response); // I need to use this in the top function @ resInfo.
transporter.close();
} else {
console.log(error);
console.log(clientReminderCandidates); // array is available
console.log(clientReminderCandidates[x][1]); // cannot access these objects undefined
console.log('Reminder Sent and Logged!');
transporter.close();
}
});
}
}
How can I access the info.response in order to use it in the fs.appendfile
function to build my log entry including the response code?