forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Email Template send in secure SMTP mode #270

Closed newkillerbeast2017 closed 6 years ago

newkillerbeast2017 commented 6 years ago

I am trying to send email over secure mode. Following is the way I try to create the smtp connection:

const emailTemplate = new EmailTemplate({
    secureConnection: true,
    secure: true,
    message: {
        from: process.env.MAIL_FROM
    },
    transport: {
        host: process.env.MAIL_HOST,
        port: process.env.MAIL_PORT,
        auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
        }
    }
});

Notice I tried both secure:true as well secureConnection:true. But following is the error I am receive:

(node:5764) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Greeting never received
[1] (node:5764) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
newkillerbeast2017 commented 6 years ago

@niftylettuce any help?

niftylettuce commented 6 years ago

@newkillerbeast2017 can you please share a complete/full example? how are you sending the messages? what version of email-templates are you using? what version of node are you using?

in the error you shared it shows that you are not catching a promise error

newkillerbeast2017 commented 6 years ago

@niftylettuce My complete code:

mailer.ts:

import * as EmailTemplate from 'email-templates';
import * as dotenv from 'dotenv';
import * as path from 'path';

dotenv.load({ path: '.env' });

const emailTemplate = new EmailTemplate({
    secure: true,
    message: {
        from: process.env.MAIL_FROM
    },
    transport: {
        host: process.env.MAIL_HOST,
        port: process.env.MAIL_PORT,
        auth: {
            user: process.env.MAIL_USERNAME,
            pass: process.env.MAIL_PASSWORD
        }
    }
});

export function sendVerficationLink(obj) {
    return emailTemplate.send({
        template: path.join('../src/mailers/templates', 'confirmation-mail'),
        message: {
            to: obj.email
        },
        locals: {
            name: obj.first_name + " " + obj.last_name,
            token: obj.token
        }
    }).then(console.log).catch(console.error);
};

And in my controller, I call it like:

userctrl.ts:

import * as mailer from '../mailers/mailers';
....
// On user register
mailer.sendVerficationLink(obj);
....

In my .env file:

MAIL_FROM=no-reply@xxx.com
MAIL_HOST=xxx.com
MAIL_PORT=465
MAIL_USERNAME=no-reply@xxx.com
MAIL_PASSWORD=yyy

Email-templates version: 3.2.0 Node version: v9.2.0

EDIT: Perhaps your right I didn't catch the error. I have made changes to catch the error now and this is what I receive:


[1] { Error: Greeting never received
[1]     at SMTPConnection._formatError (C:\Users\User\Documents\Vivavii-API\node_modules\nodemailer\lib\smtp-connection\index.js:591:19)
[1]     at SMTPConnection._onError (C:\Users\User\Documents\Vivavii-API\node_modules\nodemailer\lib\smtp-connection\index.js:564:20)
[1]     at Timeout._greetingTimeout.setTimeout (C:\Users\User\Documents\Vivavii-API\node_modules\nodemailer\lib\smtp-connection\index.js:505:22)
[1]     at ontimeout (timers.js:478:11)
[1]     at tryOnTimeout (timers.js:302:5)
[1]     at Timer.listOnTimeout (timers.js:262:5) code: 'ETIMEDOUT', command: 'CONN' }
niftylettuce commented 6 years ago

Try changing the code here of mailer.sendVerficationLink(obj); to:

mailer.sendVerficationLink(obj).then(function(data) {
  console.log('Email sent', data);
}).catch(function(err) {
  console.log('Error occurred', err);
});

Then run it and share the console output (or perhaps you'll see from the output what the error is).

niftylettuce commented 6 years ago

Just saw your "EDIT" above, sorry GitHub doesn't ping us when edits are made to comments.

Greeting never received indicates the server never responded with HELO (a greeting) to accept the mail delivery.

I think that this is a configuration issue with your port / configuration.

Are you using a known provider https://nodemailer.com/smtp/well-known/?

You could possible use telnet to try to connect to the mail server using those credentials so you know it's not just a code-related or code-configuration issue.

niftylettuce commented 6 years ago

Closing this as this question should be posted in https://github.com/nodemailer/nodemailer as its an issue while configuring your transport method. Also note that there is no secure option for this library. You may want to pass that option to the transport object instead so it gets passed along to Nodemailer:

const emailTemplate = new EmailTemplate({
-  secure: true,
  message: {
    from: process.env.MAIL_FROM
  },
  transport: {
+    secure: true,
    host: process.env.MAIL_HOST,
    port: process.env.MAIL_PORT,
    auth: {
      user: process.env.MAIL_USERNAME,
      pass: process.env.MAIL_PASSWORD
    }
  }
});