# Email Notifications

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

Emails use a JSON template for content - no need to wrestle with HTML tables. The JSON is then injected into an email template located in the `/emails` directory.

```javascript
await mail.send({

 to: 'name@email.com,
 template: 'welcome',
 content: {
  
  name: 'John',
  plan: 'startup',
  price: '$49'
  
 }
});
```

You can also use a custom HTML template by passing the file name as a `custom` value.

## Sending Notifications to Yourself

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

```javascript
POST /api/utility/mail

// params 
{
  name: 'from-name',
  email: 'from-email-address',
  message: 'message body'
}
```

This will send an email to the address stored in `SUPPORT_EMAIL` [environment variable](/gravity-server/environment-variables.md). You can see an example of this in the /help view.&#x20;

## Using Other Mail Providers

Gravity uses [nodemailer](https://nodemailer.com/) with the default mail service set to [Mailgun](httsp://mailgun.com).&#x20;

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](https://www.npmjs.com/)
2. Change the require import on line 7 of **helper/mail** to import your package
3. Update the authentication object in line 25 of **helper/mail** to match your service's requirements

```javascript
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 f[ull list of well-known services is available here.](https://nodemailer.com/smtp/well-known/)

## 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.

{% hint style="info" %}
I recommend using [htmlemail.io](https://htmlemail.io) for premium templates. **Gravity customers get 20% OFF** – you’ll receive a coupon via email after you purchase.
{% endhint %}

For full instructions on how to implement a custom template, [please follow the instructions in this blog post](https://usegravity.app/blog/how-to-use-custom-email-templates-with-gravity).

{% hint style="info" %}
If you're migrating from a previous version of Gravity, please run the seeds below to populate the database with the email content.
{% endhint %}

```javascript
knex seed:run // sql
node seed/mongo // mongodb
```

## Notification Preferences

Users can't 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](/gravity-server/config.md). This way, all new users will automatically have the these preferences added to the database.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.usegravity.app/gravity-server/email-notifications.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
