Meteor application email delivery fails with MailGun - Method 'sendEmail' returns error 500

I’m working on a Meteor v1 application and trying to implement email functionality using the Email package with MailGun. However, when I try to send emails, I keep getting an Internal server error [500] when calling the sendEmail method.

Client-side code:

Template.ContactForm.events({
    'submit .contact-form': function(evt, tmpl){
        evt.preventDefault();

        var departureCity = tmpl.find('#departure').value;
        var destinationCity = tmpl.find('#destination').value;
        var userEmail = tmpl.find('#email-input').value;
        var fullName = tmpl.find('#name-input').value;
        var phoneNumber = tmpl.find('#phone-input').value;
        var additionalInfo = tmpl.find('#details-input').value;

        Meteor.call('sendEmail', '[email protected]', userEmail, 'Quote Request', 'New quote request received.');
    }
});

Server-side code:

Meteor.startup(function() {
    var mailgunUser = "postmaster%40sandboxxxxxxxx.mailgun.org";
    var mailgunPass = "xxxxxxxxxxx";
    var smtpHost = "smtp.mailgun.org";
    var smtpPort = "587";

   process.env.MAIL_URL = 'smtp://' + encodeURIComponent(mailgunUser) + ':' + encodeURIComponent(mailgunPass) + '@' + encodeURIComponent(smtpHost) + ':' + smtpPort;
});

Meteor.methods({
  'sendEmail': function (recipient, sender, emailSubject, emailBody) {
    this.unblock();

    Email.send({
      to: recipient,
      from: sender,
      subject: emailSubject,
      text: emailBody
    });
  }
});

The console shows: Error invoking Method ‘sendEmail’: Internal server error [500]

Has anyone encountered this problem before and knows how to resolve it?

make sure your mailgun domain is verified. had that issue 2, was cuz my domain wasn’t set up right in mailgun. also, lose the %40 in your mailgunUser; just go with ‘[email protected]’ instead.

Had the same issue with Meteor and MailGun. Turns out I wasn’t handling errors properly in Email.send(), so the real error was getting buried. Add some logging to your Meteor method to see what’s actually breaking. Also check your SMTP credentials in the MailGun dashboard - sandbox creds are often different than you’d think. One thing that got me was MailGun only accepts ‘from’ addresses from verified domains. If you’re using the user’s email as sender, that’s probably your problem. Use a fixed ‘from’ address instead and put the user’s email in ‘reply-to’.

Your MAIL_URL config is probably the problem. I hit this same issue with MailGun and Meteor. Remove the URL encoding from mailgunUser - just use “[email protected]” without the %40. Check your server logs for actual error details since 500 errors don’t tell you much. Wrap Email.send() in try-catch to see what’s really happening. Double-check your MailGun API key and SMTP credentials from the dashboard. Also, sandbox domains can be picky about authorized senders, so verify your sender email is allowed.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.