Blogs

3 Ways to Send Emails with Supabase

3 Ways to Send Emails with Supabase

3 Ways to Send Emails with Supabase

Jun 5, 2024

We discuss and implement 3 solutions for including email notifications in your Supabase project using NodeJS.

While Supabase has a built-in email functionality, it can only be used to send authentication emails - and only 4 per hour. These heavy limitations require users to find and implement alternative email solutions. There are several 3rd-party solutions to this issue - we will cover 3 of them using NodeJS.

The 3 ways we cover are:

  • SendGrid: Email Only, 2-3 hours, 3k free emails

  • Resend: Email Only, 1 hour, 3k free emails

  • NotificationAPI: Multi-Channel, 5 minutes, 10k free emails

SendGrid

SendGrid is an email-oriented solution for sending notifications to users. Its features are beneficial for both marketing and transactional emails, allowing you to make the most of your emails. SendGrid also includes a template editor for designing your emails.

If you’re looking for more than just emails for your project’s notifications, you will need to integrate additional 3rd-party services to include those channels, as SendGrid is strictly for emails.

After creating an account with SendGrid Twilio, you’ll need to complete their onboarding requirements, such as:

  • Creating a sender identity

  • Having either a domain or email address you wish to use to send emails

Make sure to select the Web API option during their onboarding when asked which method you will use to send emails.

SendGrid’s onboarding does a good job of preparing you to send your first email, but here is some sample code:

const sendgrid = require('@sendgrid/mail');
// Make sure to use .env files for API keys, etc
const sendgridKey = process.env.SENDGRID_KEY;
const sendgridSender = process.env.SENDGRID_ADDRESS;

const sendEmail = async (req, res) => {
  // Connecting to SendGrid
  sendgrid.setApiKey(sendgridKey);
  /* Creating our mail object for SendGrid 
  Replace these values with your own */
  const mail = {
    to: `your_email@mail.com`,
    from: sendgridSender,
    subject: `Sending an email using SendGrid`,
    html: `<strong>We sent this email using SendGrid!</strong>`
  };
  // Sending our email
  sendgrid.send(mail);
};

return sendEmail();

Resend

Resend supports Supabase integration via their dashboard, allowing you to handle authentication emails without building your own. This also lets you bypass Supabase’s 4 emails per hour restriction. To do this, you will need to create a custom SMTP server.

This solution requires you to have your own domain in order to send emails, along with a more extensive setup, which will take the longest of our solutions to integrate. If you already have a domain, all of this can be done through Resend's dashboard via Settings:

  • Authorize Resend to access your Supabase API

  • Configure SMTP via Resend’s integration settings

  • Confirm your SMTP integration

Resend provides great documentation on sending emails, but we’ve included an example as well:

import { Resend } from 'resend';
// Make sure to use .env files for API keys, etc
const resendKey = process.env.RESEND_KEY;

const sendEmail = async (req, res) => {
  // Connecting to Resend
  const resend = Resend(resendKey);
  await resend.emails.send({ 
    /* Creating our Resend mail object
    Replace these values with your own */
    from: `Testing <name@your_domain.com>`,
    to: [`your_email@mail.com`],
    subject: `Test email`,
    text: `We sent this email using Resend!`,
  });
};

return sendEmail();

NotificationAPI

NotificationAPI is an all-in-one notification service, allowing you to expand to more than just emails without integrating more 3rd-party services. We let you skip steps such as creating a sender identity and preparing a custom SMTP for your domain, letting you get your email system up and running faster. We also support a multi-template editor for designing emails and any other notification you need.

All you need to do upon signing up is create a notification ID, which we cover during onboarding. Once that’s complete you can try implementing this sample code:

const notificationapi = require('notificationapi-node-server-sdk').default;

// Make sure to use .env files for API keys, etc
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;

const sendEmail = async (req, res) => {
  // Connecting to NotificationAPI
  notificationapi.init(clientId, clientSecret);
  // Sending the email
  notificationapi.send({
    /* Passing in our notificationId for we made for our template
    Replace these with your own values */
    notificationId: `testing_email`,
    user: {
      id: `your_email@mail.com`,
      email: `your_email@mail.com`,
    },
    mergeTags: {
      "comment": `We sent this email using NotificationAPI!`
    }
  });
};

return sendEmail();