OiRc
OiRc

Reputation: 1622

Unhandled promise rejection while sending email using Node.js

I'm using this script for sending email:

var mailer = require('nodemailer');

var smtpTransport = mailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'user',
        pass: 'pass'
    }
});

 smtpTransport.sendMail({       
        sender: 'user',
        to: 'user',
        subject: 'Attachment!',
        body: 'mail content...',
        attachments: [{   
        filename: 'text3.txt',
        path: 'pat/to/file.txt' 
    }]
    }), function(err, success) {
        if (err) {
        }

    }

But I'm getting this error:

Unhandled promise rejection (rejection id: 9): Error: connect ETIMEDOUT

What's wrong?

Upvotes: 1

Views: 2144

Answers (3)

Jed Richards
Jed Richards

Reputation: 12437

Well, firstly what is wrong is you have a ETIMEDOUT error coming from your email call. Are you sure you have all the parameters correct?

Secondly use Promises, rather than callback style, and catch the rejection. Like this,

smtpTransport
  .sendMail(...)
  .then(success => console.log('success: ', success))
  .catch(error => console.log('error: ', error))

Upvotes: 3

CoursesWeb
CoursesWeb

Reputation: 4237

Maybe is a rejection from GMail, Try Login in to: https//www.google.com/setti ngs/security/lesssecureapps and TURN ON "Access for less secure apps". Or use OAuth.

Upvotes: 0

Rohit Saxena
Rohit Saxena

Reputation: 667

Unhandled Promise rejection means that there is some Promise function that doesn't have a .catch. Nodemailer's .sendMail function is a Promise function so you can either have a callback or use it like a Promise.

In your code your fuction(err, sucess) is outside of your .sendMail function. Here is how you could correct it:

 smtpTransport.sendMail({       
    sender: 'user',
    to: 'user',
    subject: 'Attachment!',
    body: 'mail content...',
    attachments: [{   
    filename: 'text3.txt',
    path: 'pat/to/file.txt' 
}]
},(err, success) => {
    if (err) {
    }
});

Upvotes: 7

Related Questions