Learn to integrate v0 with Mailgun for secure, automated email delivery. Follow our step-by-step guide to streamline your messaging setup.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
package.json
in the root of your project with the following content:
{
"dependencies": {
"mailgun-js": "^0.22.0"
}
}
mailgun-js
package. When your project runs (or when the dependency manager in v0 processes the file), it will install the package automatically.
config.ts
in your project’s root (or in a folder like src/config
if you keep files organized). This file will store your Mailgun credentials.
config.ts
:
// config.ts
export const MAILGUNAPIKEY = 'YOURMAILGUNAPIKEYHERE';
export const MAILGUNDOMAIN = 'YOURMAILGUNDOMAINHERE';
YOURMAILGUNAPIKEYHERE
and YOURMAILGUNDOMAIN_HERE
with your actual Mailgun API key and domain.
mailgun.ts
in your project (or in a suitable folder such as src/services
).
mailgun.ts
which demonstrates how to initialize Mailgun and send an email:
import mailgunModule from 'mailgun-js';
import { MAILGUNAPIKEY, MAILGUN_DOMAIN } from './config';
const mailgun = mailgunModule({ apiKey: MAILGUNAPIKEY, domain: MAILGUN_DOMAIN });
export interface EmailData {
from: string;
to: string;
subject: string;
text?: string;
html?: string;
}
export const sendEmail = async (data: EmailData): Promise => {
return new Promise((resolve, reject) => {
mailgun.messages().send(data, (error: any, body: any) => {
if (error) {
console.error('Mailgun Error:', error);
return reject(error);
}
console.log('Mailgun Response:', body);
resolve(body);
});
});
};
mailgun-js
module and your configuration constants.sendEmail
that accepts an object with mail details and sends an email using Mailgun’s API.
index.ts
).
sendEmail
function. For example, add the following code snippet into your main file:
import { sendEmail } from './mailgun';
// Example function to send a welcome email
const sendWelcomeEmail = async () => {
const emailData = {
from: 'Your Name <you@yourdomain.com>',
to: 'recipient@example.com',
subject: 'Welcome!',
text: 'Thank you for signing up.',
// html: '<h1>Thank you for signing up</h1>'
};
try {
const response = await sendEmail(emailData);
console.log('Email sent successfully:', response);
} catch (error) {
console.error('Failed to send email:', error);
}
};
// Call the function when appropriate in your application
sendWelcomeEmail();
emailData
according to your requirements, such as updating the sender, recipient, and email content.
package.json
for dependencies automatically, when the code is executed, Mailgun should be available.
sendWelcomeEmail
and check the console output for the success or error messages.
sendEmail
function or create additional functions in the mailgun.ts
file.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.