Mailgun Integration

Overview

In Patronus, Mailgun is integrated with Appwrite functions to enable automated email notifications for authenticated users. Once the AI model processes a flight query and generates predictions, users receive an email with the optimal booking details, including the predicted lowest price and booking link. This integration ensures that users are notified promptly, enhancing their overall experience.

Mailgun Integration Setup

Mailgun integration is set up within the flightQueryFunction Appwrite function, where the Mailgun API is invoked to send personalized email updates to each user.

1. Configuration in Appwrite Function

The Appwrite function uses environment variables to securely manage Mailgun credentials and configuration settings. This setup includes:

  • MAILGUN_API_KEY: Mailgun API key.

  • MAILGUN_DOMAIN: Domain configured in Mailgun for email sending.

These credentials are securely stored in the Appwrite environment and accessed within the flightQueryFunction to avoid hardcoding sensitive information in the codebase.

2. Sending Email Notifications with Mailgun

The email notification process involves generating an email template based on the AI model’s response, then using Mailgun to send this email to the user's registered address.

Here’s how the Appwrite function sends an email using Mailgun:

  1. Initialize Mailgun:

    • The function imports and configures Mailgun using the API key and domain stored in environment variables.

    const mailgun = require("mailgun-js");
    const mg = mailgun({ apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN });
  2. Prepare the Email Content:

    • The email content is dynamically generated based on the AI model’s prediction. Key data includes:

      • Predicted Lowest Price for the flight.

      • Predicted Date/Time for optimal booking.

      • Booking Link to allow the user to book immediately if the price is favorable.

    const {
              fromLocation,
              toLocation,
              departureDate,
              arrivalDate,
              passengers,
              ticketPrice,
              airline,
              bookingLink,
              takeOffTime,
              landingTime,
            } = extractedFields;
    
            const emailContent = `
            <!DOCTYPE html>
            <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
            <head>
            .......
                </div>
            </body>
            </html>`;
  3. Send the Email Using Mailgun:

    • With the email content ready, the function calls the Mailgun messages().send() method, delivering the message to the user.

    const emailResponse = await mail.createEmail(
       messageId,
       'AI Response for Flight Details', 
       emailContent, 
       [],
       [userId],
       [],
       [],
       [],
       [],
       false,
       true,
     );
  4. Handle Error and Success Responses:

    • The function logs any errors in the email-sending process, ensuring quick troubleshooting and stability.

Benefits of Mailgun Integration

  • Real-Time Notifications: Users receive instant email alerts when optimal flight prices are predicted.

  • Improved User Experience: By notifying users directly via email, Patronus ensures they don’t miss booking opportunities at the best rates.

  • Scalability: Mailgun’s capabilities allow Patronus to handle a high volume of email notifications, essential as the user base grows.

Use Case in Patronus

  1. Automated Email Alerts: When a user’s flight query is processed, they receive an email with the AI-generated predictions, including the date and time to book for the best price.

  2. Efficient User Interaction: This setup means users don’t need to repeatedly check the app; instead, they can rely on receiving key updates directly to their inbox.


This integration of Mailgun within Appwrite functions enables effective and scalable communication with Patronus users, ensuring they stay informed on important updates. Let me know if you’d like more information on any part of this setup!

Last updated