/lovable-integrations

Lovable and Mailgun integration: Step-by-Step Guide 2025

Integrate Lovable with Mailgun using our concise, step-by-step guide. Unlock configuration tips, troubleshooting advice, and best practices for email deliverability.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to integrate Lovable with Mailgun?

 

Creating the Mailgun Service File

 
  • Create a new file in your Lovable project and name it “mailgunService.ts”.
  • This file will contain the TypeScript code that sends emails via Mailgun by calling its HTTP API.

export interface MailgunConfig {
  apiKey: string;
  domain: string;
}

export interface EmailData {
  from: string;
  to: string;
  subject: string;
  text: string;
}

export async function sendEmail(
  config: MailgunConfig,
  emailData: EmailData
): Promise {
  const url = https://api.mailgun.net/v3/${config.domain}/messages;
  const formData = new URLSearchParams();
  formData.append("from", emailData.from);
  formData.append("to", emailData.to);
  formData.append("subject", emailData.subject);
  formData.append("text", emailData.text);

  // btoa is used to Base64 encode the API key for Mailgun authentication
  const auth = btoa(api:${config.apiKey});

  const response = await fetch(url, {
    method: "POST",
    headers: {
      "Authorization": Basic ${auth},
      "Content-Type": "application/x-www-form-urlencoded"
    },
    body: formData.toString()
  });

  return response.json();
}

 

Integrating the Mailgun Service in Your Main Code

 
  • Open your main TypeScript file (for example “app.ts” or “main.ts”) where you want to send the email.
  • Add the import statement at the top of the file to include the Mailgun service functions.
  • Insert the code snippet below to configure the Mailgun credentials and send a test email.

import { sendEmail, MailgunConfig, EmailData } from "./mailgunService";

// Replace the placeholders with your actual Mailgun credentials
const mailgunConfig: MailgunConfig = {
  apiKey: "YOURMAILGUNAPI_KEY",
  domain: "YOURMAILGUNDOMAIN" // e.g. "sandbox123.mailgun.org"
};

const emailData: EmailData = {
  from: "Excited User <mailgun@YOURMAILGUNDOMAIN>",
  to: "[email protected]",
  subject: "Hello from Lovable Project",
  text: "Testing some Mailgun awesomeness with our Lovable project!"
};

// Call the sendEmail function when needed in your project
sendEmail(mailgunConfig, emailData)
  .then(response => {
    console.log("Email sent successfully:", response);
  })
  .catch(error => {
    console.error("Error sending email:", error);
  });

 

Adding Dependencies Without a Terminal

 
  • Since Lovable does not have a terminal, you must ensure that your project’s runtime supports the fetch API (most modern browsers and environments do).
  • If your environment does not include fetch by default, you would normally add a polyfill. In Lovable, add the polyfill code directly into your project if necessary.
  • There is no need to run an install command; just copy any required code into your files.

 

Securing Your Mailgun Credentials

 
  • Remember that your Mailgun API key and domain are sensitive information.
  • If Lovable supports environment variables or secret management, replace the hard-coded strings with values obtained securely.
  • Otherwise, be cautious and consider obscuring these details if your project source is publicly visible.

 

Testing the Mailgun Integration

 
  • After inserting the code snippets, save your changes.
  • Trigger the email sending functionality in your project (this could be on a button click or an event handler, as per your project’s design).
  • Check the console output for messages confirming successful sending or error messages for troubleshooting.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022