6

I'm trying to send an email verification via Sendgrid to users who have signed up for the first. Following the v3_mail example found here, I've created the following implementation:

const querystring = require('querystring');
const helper = require('sendgrid').mail;
const https = require('https');

const url = "http://<mywebsite.com>/users/reset/passwordLoggedOut?";
let sg = require('sendgrid')(process.env.SENDGRID_API_KEY);

const emails = {};

function sendMail(mail){
    const request = sg.emptyRequest({
       method: 'POST',
       path: '/v3/mail/send',
       body: mail.toJSON(),
    });

    sg.API(request, function(error, response) {
       console.log(response.statusCode);
       console.log(response.body);
       console.log(response.headers);
       console.log(error);
    });
}

emails.sendActivationEmail = function(user){
     let qso = {username: user.username, activationCode: user.activationCode};
     let qs = querystring.stringify(qso);
     let from = new helper.Email('Thomas.Talhelm@chicagobooth.edu');
     let to = new helper.Email(user.username);
     let subject = 'Welcome to Psych Study \'16';
     let content = new helper.Content('text/html', "<p> Thanks for signing up " +
    "for our psych study, please <a href=\"http://<mywebsite.com>/users/validate/account?" + 
     qs + "\">confirm your email</a></p>");

     let mail = new helper.Mail(from, subject, to, content);

     sendMail(mail);

     console.log('Mail Sent!');
}

module.exports = emails;

Anytime I signup a user, these functions are eventually called without error, although I receive a status code of 202 (response received, email is queued for delivery), and the email never gets sent. The problem is somewhat similar to this one, but my activity log is blank even though I've entered the SendGrid API properly by an environment variable which is well-defined. I've tried re-creating my API key, used full permissions on the key, and have not found an explanation for this behavior from the API.

Community
  • 1
  • 1
Adam Freymiller
  • 1,929
  • 7
  • 27
  • 49
  • 1
    Same problem here. I reported it to SendGrid's support because it occurred on a server that some weeks ago was working perfectly. – Tae Mar 04 '17 at 13:39
  • Possible duplicate of [Sendgrid returns 202 but doesn't send email](https://stackoverflow.com/questions/42214048/sendgrid-returns-202-but-doesnt-send-email) – Nathan Wailes Oct 03 '17 at 14:39
  • Sendgrid API V3 Hope that helps! https://stackoverflow.com/a/50479562/2392211 – ruin3936 May 23 '18 at 04:02
  • 2
    @NathanWailes why do you mark it duplicate , the answer you are pointing didn't help anyone and not any accepted answer – Maha Dev Nov 07 '19 at 07:14
  • @MahaDev The *answers* to that question have no bearing on whether this question is a duplicate of that question. – Nathan Wailes Nov 07 '19 at 11:50

0 Answers0