Integrate Bolt.new AI with Mailgun effortlessly using our step-by-step guide. Boost your email automation and streamline your workflow with advanced AI.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
mailgunConfig.ts
in your project’s root directory. This file will hold your Mailgun configuration such as API key and domain.mailgunConfig.ts
. Replace YOURMAILGUNAPIKEY
and YOURMAILGUN_DOMAIN
with your actual Mailgun credentials.
export const MAILGUNAPIKEY = 'YOURMAILGUNAPI_KEY';
export const MAILGUNDOMAIN = 'YOURMAILGUN_DOMAIN';
mailgunService.ts
in your project’s root directory. This module will contain the TypeScript code that sends emails through Mailgun using its HTTP API.fetch
API for HTTP requests rather than installing a Mailgun SDK.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;
}
index.ts
or your main application file).sendEmail
function from mailgunService.ts
at the top of your file:
import { sendEmail } from './mailgunService';
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();
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.
mailgunConfig.ts
, mailgunService.ts
, and your main file such as index.ts
) are saved.handleUserAction
function will send an email using the Mailgun API, and you can monitor the console logs for success or error messages.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.