I'm using nodemailer module to send email in my sails.js project. Now i'm wondering if there is a way that i could know if the email is delivered successfully or the email is somehow failed due to some attachment or may be wrong email address.
how can i make it sure that the email address is valid or not ? or the email is being delivered to the relevant email address successfully or not ?
I've read the nodemailer documentation and also have done some R&D on it but so far i'm not able to find anything productive.
Please let me know if there is a way to confirm about email or not.
Thanks.
I'm using nodemailer module to send email in my sails.js project. Now i'm wondering if there is a way that i could know if the email is delivered successfully or the email is somehow failed due to some attachment or may be wrong email address.
how can i make it sure that the email address is valid or not ? or the email is being delivered to the relevant email address successfully or not ?
I've read the nodemailer documentation and also have done some R&D on it but so far i'm not able to find anything productive.
Please let me know if there is a way to confirm about email or not.
Thanks.
Share Improve this question edited Nov 2, 2015 at 20:11 Joe Hill 3333 silver badges12 bronze badges asked Sep 30, 2015 at 19:04 Ahsan HussainAhsan Hussain 9825 gold badges22 silver badges42 bronze badges 1- There is no way you can do that, in general, short of asking the recipient to click on a link. – Lasse V. Karlsen Commented Oct 1, 2015 at 7:20
2 Answers
Reset to default 10It's simple. sendMail
has a callback as an argument. You just checking if some error is exists in this callback.
transport.sendMail({}, (error, result) => {
if (error) return console.error(error);
return console.log(result);
});
Or you can use sails-service-mailer
package for sending mails - https://github./ghaiklor/sails-service-mailer. Then you can write your code like this
SmtpMailer
.send({})
.then(console.log.bind(console))
.catch(console.error.bind(console));
If you want to check pool or something then there is no possibility, I suppose. If you send mail to non-existing email address it sends as usual, just not be received by recipient. Anyway, you can check if mail was added to pool only.
UPD: transport
has a Promise API as well (see https://nodemailer./usage/) so there is no need in custom wrappers like mine sails-service-mailer
anymore.
As far as I can tell, there might be two solutions here.
One is described here: https://nodemailer./smtp/dsn/
DELIVERY STATUS NOTIFICATIONS
If your delivery service supports it (not all SMTP servers have DSN extension enabled), then you can use Delivery status notifications (DSN) with Nodemailer as defined in RFC3461.
Examples
1.Request DSN for delivered messages
let message = {
from: '[email protected]',
to: '[email protected]',
subject: 'Message',
text: 'I hope this message gets read!',
dsn: {
id: 'some random message specific id',
return: 'headers',
notify: 'success',
recipient: '[email protected]'
}
};
2.Request DSN for undelivered and delayed messages
let message = {
from: '[email protected]',
to: '[email protected]',
subject: 'Message',
text: 'I hope this message gets read!',
dsn: {
id: 'some random message specific id',
return: 'headers',
notify: ['failure', 'delay'],
recipient: '[email protected]'
}
};
Another option is discussed in github here: https://github./nodemailer/nodemailer/issues/487
The "standard" bounce handling is to use VERP, eg. to use a custom FROM address for the SMTP envelope and then listen for messages for this address – if anything is received then the message has bounced.
mailOptions = {
from: '[email protected]',
to: '[email protected]',
messageId: 'abcdef',
envelope: {
from: '[email protected]',
to: ['[email protected]']
}
}
The smtp-server would listen for ining messages, accept everything but storing nothing. Once a message is received, it checks the TO address and if the address is [email protected] then it can emit an event that a message with message-id 'abcdef' to [email protected] was bounced. You should also look into the message body to see the reason but this is probably quite difficult as every server software uses a different format for bounce messages.
And then you'd need to monitor inbox and handle the bounce emails.