Nodemailer: The mail is sent using SMTP and gmail but the callback hangs until timeout

Created on 17 Mar 2018  Â·  9Comments  Â·  Source: nodemailer/nodemailer

Using nodejs and nodemailer on my localhost i'm able to send the mail but callback is never called until timeout. No debug info provided (no stack trace), also nodemailer is ignoring socketTimeout config setting. Timeout should be in 5000ms but it hangs too long.

1. Is this a bug in Nodemailer?

  • [x ] Yes
  • [ ] No

Note. Non bug issues are closed by default. Head to Stack Overflow for support questions: https://stackoverflow.com/search?q=nodemailer

2. Is this related to Gmail / Hotmail / Yahoo?

  • [x ] Yes
  • [ ] No

    Note. If you can not log in to Gmail then Gmail is blocking you. These issues are closed by default.

3. Is your issue related to SSL/TLS errors?

  • [ ] Yes
  • [x ] No

    Note. Most probably you are trying to connect to a server with a self-signed/expired cert or the server supports only ancient ciphers or you are using an antivirus that intercepts your internet connection. Such issues are closed by default.

4. Is your issue related to connection timeouts

  • [x ] Yes
  • [ ] No

Note. Most probably you are firewalled out from the server. Such issues are closed by default.

5. Do you get SyntaxError when using Nodemailer?

  • [ ] Yes
  • [ x] No

Note. You are using too old version of Node.js, please upgrade. Such issues are closed by default.

6. Nodemailer version you are having problems with (eg. v1.3.7)

4.6.0
...

7. Node.js version you are using (run node -v to see it, eg v5.5.0)

v8.9.4
...

8. Your operating system (eg. Windows 10, Ubuntu 14.04 etc.)

Windows 10
...

9. Nodemailer configuration settings (if applicable)

        const transporter = nodemailer.createTransport({
          service: 'gmail',
          host: 'smtp.gmail.com',
          port: 465,
          secure: true,
          socketTimeout: 5000,
          logger: true,
          auth: {
            user: process.env.MAIL_USER,
            pass: process.env.MAIL_PASSWORD
          }
        });

...

10. If possible, include a minimal test case that can be used to verify your issue (link to a gist would be great!)

        transporter.verify(function (error, success) {
          if (error) {
            console.log(error);
          } else {
            console.log('Server is ready to take our messages');
          }
        });

        const mailOptions = {
          to: user.email,
          from: process.env.MAIL_USER,
          subject: 'Account Activation',
          text: 'hello'
        };
        transporter.sendMail(mailOptions).then((sentMessage) => {
          console.log(sentMessage);
        }, (error) => {
          console.log(error);
        });

...

Thanks!

Most helpful comment

Same issue, any solution yet?

All 9 comments

Can't confirm. This is what I see when running the example script:

[2018-03-17 10:03:28] DEBUG Creating transport: nodemailer (4.6.3; +https://nodemailer.com/; SMTP/4.6.3[client:4.6.3])
[2018-03-17 10:03:28] DEBUG Sending mail using SMTP/4.6.3[client:4.6.3]
[2018-03-17 10:03:29] INFO  [eH7FPchPnoc] Secure connection established to 173.194.222.108:465
[2018-03-17 10:03:29] INFO  [syLy5C7WFlc] Secure connection established to 173.194.222.108:465
[2018-03-17 10:03:29] DEBUG [eH7FPchPnoc] SMTP handshake finished
[2018-03-17 10:03:29] DEBUG [syLy5C7WFlc] SMTP handshake finished
[2018-03-17 10:03:29] INFO  [eH7FPchPnoc] User "[email protected]" authenticated
Server is ready to take our messages
[2018-03-17 10:03:29] INFO  [syLy5C7WFlc] User "[email protected]" authenticated
[2018-03-17 10:03:29] INFO  Sending message <[email protected]> to <[email protected]>
[2018-03-17 10:03:29] DEBUG [eH7FPchPnoc] Closing connection to the server using "end"
[2018-03-17 10:03:29] INFO  [eH7FPchPnoc] Connection closed
[2018-03-17 10:03:30] INFO  [syLy5C7WFlc] <276 bytes encoded mime message (source size 273 bytes)>
[2018-03-17 10:03:31] DEBUG [syLy5C7WFlc] Closing connection to the server using "end"
{ accepted: [ '[email protected]' ],
  rejected: [],
  envelopeTime: 815,
  messageTime: 1127,
  messageSize: 276,
  response: '250 2.0.0 OK 1521281011 t68-v6sm91451lfe.59 - gsmtp',
  envelope: { from: '[email protected]', to: [ '[email protected]' ] },
  messageId: '<[email protected]>' }
[2018-03-17 10:03:31] INFO  [syLy5C7WFlc] Connection closed

Please re-open the ticket if you find any additional information why the script fails on your side.

Thanks @andris9 , i'll try to dig deeper...

I get the same issue when testing it with Jest, even if the email is correctly sent:

    const mailConfig = {
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'user',
            pass: 'pass',
        },
    },

    test('production', (done) => {
        const transporter = nodemailer.createTransport(mailConfig);
        mailOptions.subject = 'Test production e-mail ✔';

        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                console.log(error);
                throw new Error('failed');
            }
            expect(info.accepted.length).toBe(1);
            expect(info.accepted[0]).toEqual(recipient);
            expect(info.response).toEqual('250 Great success');
            expect(info.envelope.from).toEqual('[email protected]');
            // console.log(info);
            done();
        });
    }, 10000);

Finally I get Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.. This is not the case when I test it with MailGun.

Logs:

console.log node_modules/nodemailer/lib/shared/index.js:369
    [2018-06-11 16:22:55] INFO  [OAz0YIaTUc] <658 bytes encoded mime message (source size 655 bytes)>

  console.log node_modules/nodemailer/lib/shared/index.js:369
    [2018-06-11 16:22:56] DEBUG [OAz0YIaTUc] S: 250 2.0.0 OK 1528734176 132-v6sm16324480wmr.33 - gsmtp

  console.log node_modules/nodemailer/lib/shared/index.js:369
    [2018-06-11 16:22:56] DEBUG [OAz0YIaTUc] Closing connection to the server using "end"

  console.log node_modules/nodemailer/lib/shared/index.js:369
    [2018-06-11 16:22:56] ERROR Callback error for <[email protected]>: expect(received).toEqual(expected)

Hi please help me i have some problem on nodemailer.

connect ETIMEDOUT 108.177.97.109:465
Error: connect ETIMEDOUT 108.177.97.109:465
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)

Same issue, any solution yet?

I've got the weird error where transporter.sendmail isn't working despite enabling less secure apps connection on Gmail. It was working before but suddenly stopped working despite not changing anything. I'd love a help right now

@okabamac Same issue here, anyone have a work around? Was working, suddenly not.
Request takes 2 minutes to fail.

{ Error: Connection timeout
    at SMTPConnection._formatError (/sites/project/production/active/web/backend/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
    at SMTPConnection._onError (/sites/project/production/active/web/backend/node_modules/nodemailer/lib/smtp-connection/index.js:579:20)
    at Timeout._connectionTimeout.setTimeout (/sites/project/production/active/web/backend/node_modules/nodemailer/lib/smtp-connection/index.js:261:18)
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5) code: 'ETIMEDOUT', command: 'CONN' }

Getting the same issue! Did not change even a bit. Suddenly stopped working. Please help!

@RohitGalgali Hey thanks for the support on this thread lol.

I've done extensive research about this issue for mycase and it's on google's side and they don't say why so it could be anything such as your emails are being detected as spam some how. I switched to mailgun and using this package: nodemailer-mailgun-transport as a quick alternative switch. Their service is free up to a certain amount of emails.

Honestly I've had this issue before with a laravel server and postfix, it's always the google side of things and I'm going to avoid using gmail as a SMTP.

Was this page helpful?
0 / 5 - 0 ratings