Documentation

Setup Mailgun Integration

Setup Mailgun Integration

AuthAlert integrates with Mailgun to send authentication alerts. This integration allows you to connect AuthAlert with Mailgun and use a Mailgun template to send customized authentication alerts without coding.

Setup Mailgun Template

To send emails using Mailgun, first add and verify your domain in your Mailgun account. Once your domain is ready for sending emails, navigate to Domain settingsSending API keys to create a new API key. Be sure to save this key for later use.

You'll need a template to format the alert emails. To create one, go to Templates and click Create message template. Mailgun provides a drag-and-drop visual editor for this purpose. The AuthAlert integration offers the following variables to help personalize your alert emails:

  • first_name
    • Example: "John"
    • Description: The user's first name.
  • last_name
    • Example: "Smith"
    • Description: The user's last name.
  • new_device_os
    • Example: "Mac OS X"
    • Description: The new device's operating system.
  • new_device_os_version
    • Example: "10"
    • Description: The new device's operating system version.
  • new_device_browser
    • Example: "Firefox"
    • Description: The new device's browser.
  • new_device_browser_version
    • Example: "128"
    • Description: The new device's browser version.
  • new_device_device_brand
    • Example: "Apple"
    • Description: The new device's brand.
  • new_device_country
    • Example: "AU"
    • Description: The country code where the device is located.
  • new_device_region
    • Example: "NSW"
    • Description: The state or province where the device is located.
  • new_device_city
    • Example: "Sydney"
    • Description: The city or metro area where the device is located.
  • new_device_detection_time
    • Example: "2021-08-31 17:32:38 AEST"
    • Description: The time when the device is first detected.

Here's an example template in Mailgun:

Mailgun Template Example

Enable Mailgun Integration in AuthAlert

After setting up Mailgun, go to the AuthAlert project settings interface. Toggle on the Mailgun Notification Integration and fill in these details:

  • Mailgun Domain: The domain for sending emails in Mailgun
  • Mailgun API Key: The API key you created earlier
  • Sender Email: The email address for the sender (e.g., no-reply@yoursite.com)
  • Notification Subject: The subject line for alert emails (e.g., "Unrecognized device signed in to your account")
  • Notification Template: The name of your email template in Mailgun

Save these settings to activate the integration.

Calling the API

After enabling the Mailgun integration, call the API like this:

import AuthAlertAPI from 'authalert';

const projectId = "your_project_ID";

const result = await AuthAlertAPI(projectId, {
 user_id: user.id,
 email: user.email,
 first_name: user.first_name,
 last_name: user.last_name
});

console.log(result);

To use the Mailgun integration, you must provide a valid email for the user so that alert emails can be sent. Both first_name and last_name are optional but helpful for personalizing the message.

Chaoming Li
Chaoming Li
A technical entrepreneur with a passion for technology and innovation.

Our latest articles

Learn more about how to boost your application security in software development

Ready to get started?

Get Started for Free