Skip to content
Gravity Logo / Docs Gravity home
Browse documentation
Documentation/Gravity Server

Email Notifications

You can send email notifications to your users from anywhere in your application.

The mail helper looks up localized content by template name in the database and renders it with an HTML layout in emails/.

await mail.send({

 to: '[email protected]',
 template: 'new_account',
 content: {
  
  name: 'John',
  plan: 'startup',
  price: '$49'
  
 }
});

You can also use a custom HTML template by passing its base name as html_template. For example, html_template: 'receipt' loads emails/receipt.html.

Sending Notifications to Yourself

If you wish to send a notification to yourself, you can use the mail utility endpoint:

Send POST /api/utility/mail with a JSON body:

{
  "name": "Example user",
  "email": "[email protected]",
  "message": "I have a question about my account."
}

This will send an email to the address stored in SUPPORT_EMAIL environment variable. You can see an example of this in the /help view.

Using Other Mail Providers

Gravity uses nodemailer with the default mail service set to Mailgun.

If you wish to use another mail provider, you can simply install the nodemailer transport package for your chosen service and update the mail.send method in helper/mail.

  1. Find and install the transport package from npm
  2. Change the require import in helper/mail to import your package
  3. Update the authentication object in helper/mail to match your service’s requirements
const mailgun = require('nodemailer-mailgun-transport'); // require transport

exports.send = async function(data){

  // transport auth
  const transport = nodemailer.createTransport(mailgun({
    host: settings.host,
    auth: {
    api_key: process.env.MAILGUN_API_KEY,
    domain: settings.domain
    }
  }));
}

A full list of well-known services is available here.

Using Custom Email Templates

Gravity includes a clean, responsive email template, but you can also add your own templates to cover a wider variety of use cases.

Info

I recommend using htmlemail.io for premium templates. Gravity customers get 20% OFF – you’ll receive a coupon via email after you purchase.

For full instructions on how to implement a custom template, please follow the instructions in this blog post.

Info

If you’re migrating from a previous version of Gravity, please run the seeds below to populate the database with the email content.

node seeds/sql.js   # SQL
node seeds/mongo.js # MongoDB

Notification Preferences

Users can toggle which email notifications they would like to receive in the notifications section of their account.

These settings are stored in the database notifications table. By default, the following preferences are included for you:

  • new_signin
  • plan_updated
  • card_updated
  • invite_accepted

If you’d like to add more options you, add them to the notifications object in config. This way, all new users will automatically have the these preferences added to the database.