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

Receiving successful response back but emails aren't being delivered #361

Closed TidyIQ closed 5 years ago

TidyIQ commented 5 years ago

Using Google OAuth2 with nodemailer transporter.

I've quadruple checked all my Google credentials and have enabled 'allow less secure apps' in Gmail.

It was working fine when I was just using nodemailer without any templates or template engines.

Here's my code:

import "dotenv/config";
import nodemailer from "nodemailer";
import EmailTemplate from "email-templates";
import path from "path";

// ::::::::::::::::::::::::::::::::::::::::::::::::
// Initialize email
// ::::::::::::::::::::::::::::::::::::::::::::::::

// Create transporter
const transporter = nodemailer.createTransport({
  host: "smtp.gmail.com",
  port: 465,
  secure: true,
  auth: {
    type: "OAuth2",
    user: process.env.GOOGLE_USER,
    clientId: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    refreshToken: process.env.GOOGLE_REFRESH_TOKEN
  }
});

// Initialize email template
const email = from =>
  new EmailTemplate({
    message: {
      from
    },
    transport: transporter,
    views: {
      options: {
        extension: "hbs"
      }
    }
  });

// ::::::::::::::::::::::::::::::::::::::::::::::::
// User templates
// ::::::::::::::::::::::::::::::::::::::::::::::::

export const sendEmailConfirmation = (
  recipient,
  name,
  token
) => {

  // Define template
  const context = { name, token };
  const templateName = "user_email_confirmation";
  const from = "ME <ME@DOMAIN.com>";

  // Send email
  const template = email(from);
  return template
    .send({
      template: path.join(__dirname, "templates", templateName),
      message: {
        to: `${name} <${recipient}>`
      },
      locals: context
    })
    .then(
      resp => {
        console.log(resp);
        return true;
      }
    )
    .catch(
      err => {
        console.log(err);
        return false;
      }
    );
};

And when I run sendEmailConfirmation("ME@OTHERDOMAIN.com", "OTHER ME", "TOKEN") I receive the following response back in console (from the .then((resp) => { console.log(resp); return true })):

{ envelope:
   { from: 'ME@DOMAIN.com',
     to: [ 'ME@OTHERDOMAIN.com' ] },
  messageId: '<7efc483b-0b99-691b-7ad0-65c14424e874@DOMAIN.com>',
  message:
   '{"to":[{"address":"ME@OTHERDOMAIN.com","name":"OTHER ME"}],"from":{"address":"ME@DOMAIN.com","name":"ME"},"attachments":[],"subject":"Confirm your account","html":"<!DOCTYPE html>\\n<html lang=\\"en\\">\\n<head>\\n    <meta charset=\\"UTF-8\\">\\n    <meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\">\\n    <meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\">\\n    <title>Document</title>\\n    \\n</head>\\n<body>\\n    <h1 style=\\"color: red;\\">MY LOGO</h1>\\n    <p>Hi OTHER ME,</p>\\n    <p>Please click the following link to verify your email address.</p>\\n    <p>TOKEN</p>\\n    \\n</body>\\n</html>","text":"Hi OTHER ME,\\n\\nPlease confirm your account with token TOKEN","headers":{},"messageId":"<7efc483b-0b99-691b-7ad0-65c14424e874@DOMAIN.com>"}',
  originalMessage:
   { to: 'OTHER ME <ME@OTHERDOMAIN.com>',
     from: 'ME <ME@DOMAIN.com>',
     attachments: [],
     subject: 'Confirm your account',
     html:
      '<!DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <meta name="viewport" content="width=device-width, initial-scale=1.0">\n    <meta http-equiv="X-UA-Compatible" content="ie=edge">\n    <title>Document</title>\n    \n</head>\n<body>\n    <h1 style="color: red;">TIDY-IQ</h1>\n    <p>Hi OTHER ME,</p>\n    <p>Please click the following link to verify your email address.</p>\n    <p>TOKEN</p>\n    \n</body>\n</html>',
     text:
      'Hi OTHER ME,\n\nPlease confirm your account with token TOKEN' } }

As you can see, it's correctly parsing the template and says that it's sent the email, however nothing is coming through in my email inbox at ME@OTHERDOMAIN.com (obviously changed from what I'm actually using).

It works fine in Nodemailer so I don't know what the issue is here...

TidyIQ commented 5 years ago

Got it working.

So apparently you need to specify send: true even if you haven't set NODE_ENV to development. I don't use NODE_ENV as I have no need for it. Perhaps it should instead assume NODE_ENV is production by default? If NODE_ENV isn't set then it's probably safe to assume that it's the production version.

niftylettuce commented 5 years ago

@TidyIQ thank you, glad you figured it out. I'll update the docs examples to reflect this.

niftylettuce commented 5 years ago

@TidyIQ two things, the docs clearly provide debugging information, and when you have DEBUG=emailtemplates node myapp.js there is a debug output line you could have seen:

https://github.com/niftylettuce/email-templates/blob/3d339e5a1412c702f9e70eed576077bed3d80a82/src/index.js#L299-L306

Debugging Documentation: https://github.com/niftylettuce/email-templates#debugging

Please thoroughly read the docs next time as they would have been helpful for debugging. In any case, I have updated the docs with a complete options section.

Options Documentation: https://github.com/niftylettuce/email-templates#options

Ref: https://github.com/niftylettuce/email-templates/commit/36ff4183c459d08393e854d1e0e89122d5525fa8