Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

verify if email is delivered successfully nodemailer (sails.js)

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.

like image 618
Ahsan Hussain Avatar asked Sep 30 '15 19:09

Ahsan Hussain


People also ask

How to verify e-mail with nodemailer?

We are going to use nodemailer to send email’s. To verify e-mail we will generate one random key in our script, store it, and when user click on verification link in e-mail we will fetch the key from that link and compare it to the store one.

What is nodemailer?

Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default. Nodemailer is licensed under MIT license. See license details in the License page.

Does nodemailer support Delivery Status Notifications (DSN)?

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. To set up a DSN call, add a dsn property to message data dsn – optional object to define DSN options

How to send e-mail from node server?

When user click on Send button, we are calling Node Server using complete URL (http://localhost:3000/send) and in “/send” router we dealt with sending e-mail. I hope this is useful to you.


1 Answers

It'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.com/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.com/usage/) so there is no need in custom wrappers like mine sails-service-mailer anymore.

like image 126
Eugene Obrezkov Avatar answered Oct 12 '22 23:10

Eugene Obrezkov