I’m trying to set up email functionality in my SvelteKit project using Mailgun but keep getting a 404 error. Here’s what I have so far:
import FormData from 'form-data';
import MailgunJS from 'mailgun.js';
import { MAILGUN_SECRET_KEY, MAILGUN_SENDER_DOMAIN } from '$env/static/private';
const mailgunClient = new MailgunJS(FormData);
const mg = mailgunClient.client({
username: 'api',
key: MAILGUN_SECRET_KEY
});
export const actions = {
submitMessage: async ({ request }) => {
const data = await request.formData();
const emailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Test Message',
text: 'Plain text version of the message.',
html: '<p><strong>HTML version of the message.</strong></p>'
};
try {
const result = await mg.messages.create(MAILGUN_SENDER_DOMAIN, emailOptions);
console.log('Success:', result);
return { success: true, message: 'Message delivered' };
} catch (err) {
console.error('Error:', err);
return { success: false, message: 'Delivery failed' };
}
}
};
The error I’m getting is:
[Error: Not Found] {
status: 404,
details: '404 page not found\n',
type: 'MailgunAPIError'
}
I’ve double-checked my domain configuration and API credentials. Has anyone encountered this issue before? What could be causing the 404 response from Mailgun’s API?
Had this exact problem last month - drove me crazy for hours. The 404 usually means your Mailgun region doesn’t match the API endpoint. Check if your domain’s configured for EU region. If it is, you need to explicitly set the URL when creating the client: url: 'https://api.eu.mailgun.net'. Also make sure your MAILGUN_SENDER_DOMAIN variable matches exactly what’s in your Mailgun dashboard, including any subdomain prefixes. I was using just the root domain when I needed the subdomain part. One more thing - check if your domain finished DNS verification. Mailgun throws 404s for unverified domains but the error message doesn’t tell you that.
This got me too! log your MAILGUN_SENDER_DOMAIN var first - it’s probably missing the subdomain or has extra whitespace. Mailgun’s also picky about the ‘from’ address. Make sure [email protected] is authorized for your domain or just use [email protected] instead. Quick test: hit their API directly with Postman using the same creds to see if it’s a SvelteKit issue.
Had the same problem with Mailgun and SvelteKit. Turns out my domain verification wasn’t actually complete - one DNS record was still propagating even though I thought everything was set up right. Check your domain status in the Mailgun dashboard under Domains to verify this. Also caught me off guard: make sure you’re using the full sending domain in your environment variable (like mg.yourdomain.com), not just your root domain. Double-check your API key has proper permissions and isn’t restricted. I’d test with a simple curl request to Mailgun’s API first - helps you figure out if it’s a config issue or something SvelteKit-specific.