Unable to send welcome email apon user signup in node.js app

27 Views Asked by At

I have built an app for keeping notes using node.js, express, mongoDB, parcel and pug. I have deployed the app with Render.com.

The app functions as it should EXCEPT for when a new user signs up they are supposed to get a confirmation email and they don't. However, the new user is created as mongoDB compass shows me. In the email the new user gets a link with a token(JWT) to click on to activate their account. I can confirm the token and url are created and when the url is pasted into the browser it successfully activates the user.

In the console I get this error:

POST https://note-it-v3.onrender.com/api/v1/users/signup 500 (Internal Server Error)

This error object:

{
"message": "Request failed with status code 500",
"name": "AxiosError",
"stack": "AxiosError: Request failed with status code 500\n    at s (https://note-it-v3.onrender.com/js/bundle.js:61:252)\n    at XMLHttpRequest.j (https://note-it-v3.onrender.com/js/bundle.js:77:2030)\n    at c.request (https://note-it-v3.onrender.com/js/bundle.js:89:691)",
"config": {
    "transitional": {
        "silentJSONParsing": true,
        "forcedJSONParsing": true,
        "clarifyTimeoutError": false
    },
    "adapter": [
        "xhr",
        "http"
    ],
    "transformRequest": [
        null
    ],
    "transformResponse": [
        null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1,
    "env": {},
    "headers": {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json"
    },
    "method": "post",
    "url": "/api/v1/users/signup",
    "data": "{\"name\":\"Bob Smith\",\"email\":\"[email protected]\",\"password\":\"pass1234\",\"passwordConfirm\":\"pass1234\"}"
},
"code": "ERR_BAD_RESPONSE",
"status": 500

}

In the render logs I get this error: render logs

This is the live site Note It

I am using SendGrid and nodemailer to create and send the email.

email.js

const nodemailer = require('nodemailer');
const pug = require('pug');
const htmlToText = require('html-to-text');

module.exports = class Email {
  constructor(user, url) {
    this.to = user.email;
    this.firstName = user.name.split(' ')[0];
    this.url = url;
    this.from = `Elli Turner-Bisset <${process.env.EMAIL_FROM}>`;
  }

  newTransport() {
    if (process.env.NODE_ENV === 'production') {
      // sendgrid or alternatives
      // Sendgrid
      return nodemailer.createTransport({
        host: 'smtp.sendgrid.net',
        secure: true,
        port: 25,
        auth: {
          user: process.env.SENDGRID_USERNAME,
          pass: process.env.SENDGRID_PASSWORD,
        },
        tls: {
          rejectUnauthorized: false,
        },
      });
    } else {
      return nodemailer.createTransport({
        host: process.env.EMAIL_HOST,
        port: process.env.EMAIL_PORT,
        auth: {
          user: process.env.EMAIL_USERNAME,
          pass: process.env.EMAIL_PASSWORD,
        },
      });
    }
  }

  async send(template, subject) {
    const html = pug.renderFile(
      `${__dirname}/../views/emails/${template}.pug`,
      {
        firstName: this.firstName,
        url: this.url,
        subject,
      },
    );
    const mailOptions = {
      from: this.from,
      to: this.to,
      subject,
      html,
      text: htmlToText.convert(html),
    };
    await this.newTransport().sendMail(mailOptions);
  }

  async sendWelcome() {
    await this.send('welcome', 'Welcome to Note It!');
  }

  async sendResetPassword() {
    await this.send(
      'resetPassword',
      'Reset password link. (Only valid for 10 minutes.)',
    );
  }

  async sendAfterReset() {
    await this.send('afterReset', 'Your password has been reset.');
  }
};

Here is the GitHub repo

I have gone through the troubleshooting on Render and posted a help question. No help yet. I have run this app in dev and production mode on local host with no issues or errors. When I check the SendGrid dashboard it confirms no emails have been sent.

Please can someone help me to troubleshoot this problem. I have no idea where to start.

0

There are 0 best solutions below