Ish Rak
Ish Rak

Reputation: 3

email cannot be send by mailgun on sveltekit

This is my code. Showing 404

import formData from 'form-data';
import Mailgun from 'mailgun.js';
import { PRIVATE_MAILGUN_API_KEY, PRIVATE_MAILGUN_DOMAIN } from '$env/static/private';

const mailgun = new Mailgun(formData);
const client = mailgun.client({
    username: 'api',
    key: PRIVATE_MAILGUN_API_KEY
});

export const actions = {
    sendEmail: async (event) => {
        const { request } = event;
        const formData = await request.formData();

        const messageData = {
            from: '[email protected]',
            to: '[email protected]',
            subject: 'Hello from Mailgun',
            text: 'This is an example email sent with Mailgun.',
            html: '<strong>This is an example email sent with Mailgun.</strong>'
        };

        try {
            const response = await client.messages.create(PRIVATE_MAILGUN_DOMAIN, messageData);
            console.log(response);
            return { status: 201, message: 'Email sent' };
        } catch (error) {
            console.error(error);
            return { status: 500, message: 'Failed to send email' };
        }
    }
};

I'm hoping to integrate mailgun into my sveltekit so that email can be send. But showing this error message: [Error: Not Found] { status: 404, details: '404 page not found\n', type: 'MailgunAPIError' } this is the domain: https://api.mailgun.net/v3/xxxxx.mailgun.org.com/messages

Upvotes: 0

Views: 103

Answers (1)

g3muse
g3muse

Reputation: 121

Just encountered the same thing and figured I'd post the fix if it helps anyone.

My env PRIVATE_MAILGUN_DOMAIN was set like the mailgun docs: https://api.mailgun.net/v3/yourdomain.com/messages

But mailgun.js is expecting just yourdomain.com

Upvotes: 0

Related Questions