/bolt.new-ai-integrations

Bolt.new AI and Mailgun integration: Step-by-Step Guide 2025

Integrate Bolt.new AI with Mailgun effortlessly using our step-by-step guide. Boost your email automation and streamline your workflow with advanced AI.

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 Bolt.new AI with Mailgun?

 

Setting Up Mailgun Configuration

 

  • Create a new file named mailgunConfig.ts in your project’s root directory. This file will hold your Mailgun configuration such as API key and domain.
  • Copy and paste the following code inside mailgunConfig.ts. Replace YOURMAILGUNAPIKEY and YOURMAILGUN_DOMAIN with your actual Mailgun credentials.

export const MAILGUNAPIKEY = 'YOURMAILGUNAPI_KEY';
export const MAILGUNDOMAIN = 'YOURMAILGUN_DOMAIN';

 

Creating the Mailgun Service Module

 

  • Create a new file named mailgunService.ts in your project’s root directory. This module will contain the TypeScript code that sends emails through Mailgun using its HTTP API.
  • Since Bolt.new AI projects do not support terminal installations, we will use the built-in fetch API for HTTP requests rather than installing a Mailgun SDK.
  • Copy and paste the following code into mailgunService.ts. This will define a sendEmail function that you can call from anywhere in your project.

import { MAILGUNAPIKEY, MAILGUN_DOMAIN } from './mailgunConfig';

interface EmailOptions {
  to: string;
  subject: string;
  text: string;
  html?: string;
}

export async function sendEmail(options: EmailOptions): Promise<Response> {
  const url = https://api.mailgun.net/v3/${MAILGUN_DOMAIN}/messages;
  
  // Basic authentication using base64 encoding of 'api:YOURMAILGUNAPI_KEY'
  const auth = 'Basic ' + btoa('api:' + MAILGUNAPIKEY);
  
  // Construct form data for the request
  const formData = new URLSearchParams();
  formData.append('from', Mailgun Sandbox <postmaster@${MAILGUN_DOMAIN}>);
  formData.append('to', options.to);
  formData.append('subject', options.subject);
  formData.append('text', options.text);
  
  if (options.html) {
    formData.append('html', options.html);
  }
  
  // Send the HTTP POST request using fetch
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Authorization': auth,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: formData.toString()
  });
  
  return response;
}

 

Integrating Mailgun Email Sending Into Your Application Logic

 

  • Open the file that contains the logic where you want to trigger sending an email (for example, index.ts or your main application file).
  • Import the sendEmail function from mailgunService.ts at the top of your file:

import { sendEmail } from './mailgunService';
  • Whenever you need to send an email (for instance, as a callback after a user action), add the following snippet to call the sendEmail function:

async function handleUserAction() {
  // Your application logic here
  
  try {
    const response = await sendEmail({
      to: '[email protected]',
      subject: 'Hello from Bolt.new AI using Mailgun!',
      text: 'This is a test email sent from our Bolt.new AI project integrated with Mailgun.',
      // html: 'This is a test email' // Optional: Uncomment to send HTML formatted email
    });
    
    if (response.ok) {
      console.log('Email sent successfully!');
    } else {
      console.error('Error sending email:', await response.text());
    }
  } catch (error) {
    console.error('Exception occurred while sending email:', error);
  }
}

// Call the function as needed in your code
handleUserAction();

 

Setting Environment Variables in Bolt.new AI

 

  • Since you cannot use a terminal to install dependencies or set environment variables, store your Mailgun credentials directly in the mailgunConfig.ts file as shown in the first step. Please note that this method is suitable for development or testing purposes only. For production, use secure methods to manage secrets.

 

Reviewing the Integration

 

  • Ensure that all files (mailgunConfig.ts, mailgunService.ts, and your main file such as index.ts) are saved.
  • When your Bolt.new AI project runs, the handleUserAction function will send an email using the Mailgun API, and you can monitor the console logs for success or error messages.

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