hoangvvo / nextjs-mongodb-app

A Next.js and MongoDB web application, designed with simplicity for learning and real-world applicability in mind.
https://nextjs-mongodb.now.sh/
MIT License
1.53k stars 286 forks source link

Replace SendGrid #104

Closed hoangvvo closed 3 years ago

hoangvvo commented 3 years ago

I no longer use SendGrid. This should be replaced with a different service or integration. As of right now, the tokens in this project are not valid so email functionalities are not working.

tiagostutz commented 3 years ago

Hi there! What do you think about replacing Sendgrid by https://mailslurper.com/ ? It was meant to be used for development purposes.

We could then implement in a way that when the NODE_ENV is "dev" we point to mailslurper and in production we could look for environment variables for mailing providers tokens, starting with SendeGrid.

What do you think? Also, I could grab this issue if it is not already taken.

hoangvvo commented 3 years ago

Hi there! What do you think about replacing Sendgrid by https://mailslurper.com/ ? It was meant to be used for development purposes.

We could then implement in a way that when the NODE_ENV is "dev" we point to mailslurper and in production we could look for environment variables for mailing providers tokens, starting with SendeGrid.

What do you think? Also, I could grab this issue if it is not already taken.

Hey sorry for not getting back earlier. I have contacted the SendGrid team to resolve the account problem and will update the token soon.

I have not looked at MailSlurper. We can use that instead but need to make sure it works with Vercel deployment, which does not work well with outgoing SMTP connection

l4b4r4b4b4 commented 3 years ago

I have implemented a simple smtp based solution with nodemailer and html-to-text. that works as a replacement for the sendgrid based mail.js helper.

const htmlToText = require("html-to-text");

import nodemailer from "nodemailer";

var smtpConfig = {
  host: process.env.EMAIL_DOMAIN,
  port: process.env.EMAIL_PORT,
  secure: false, // use SSL
  auth: {
    user: process.env.EMAIL_FROM,
    pass: process.env.EMAIL_PASS,
  },
  tls: {
    ciphers: "SSLv3",
  },
};

export async function sendMail(msg) {
  console.log(msg);
  const { to, from, subject, html } = msg;

  var transporter = nodemailer.createTransport(smtpConfig);
  transporter.verify(function (error, success) {
    if (error) {
      console.log(error);
    } else {
      // console.log("Server is ready to take our messages");
    }
  });
  // send mail with defined transport object
  const text = htmlToText.fromString(html, {
    wordwrap: 130,
  });
  let info = await transporter.sendMail({
    from: `"✔ Service 👻" <${process.env.EMAIL_FROM}>`, // sender address
    to, // list of receivers
    subject, // Subject line
    text, // plain text body
    html, // html body
  });

  console.log("Message sent: %s", info.messageId);

  // Preview only available when sending through an Ethereal account
  console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}