Meteor Email and Mailgun, timeout error and this.unblock is not a function [SOLVED]

I am trying to do a very basic test with Mailgun sandbox but depending on what is in my method I get a timeout error or _this.unblock is not a function .

Specifically if I have check() in, it times out, if I leave this.unblock() in it errors as not a function.

HTML is basic submit button inside a empty form

Client JS

Template.contactView.events({
	'submit #email-form'(event){
		event.preventDefault();

		Meteor.call('sendEmail', 'Nick' , 'nicholasbydesign@gmail.com' , 'Hello Nick!', function(err){
			if(err){
				console.log(err);
			}else{
				console.log('complete');
			}
		});
	}
});

Server JS

Meteor.startup(function() {
    //smtp://_smtp-default-login_:password_@hostname_:port
    process.env.MAIL_URL = 'smtp://postmaster%40sandbox9c904eaadf23r23fsdf432.mailgun.org:41555easdf90avzdf2c963adbbf@smtp.mailgun.org:25';
});

sendEmail: (name, email, message) =>{
        
        check([name, email, message], [String]);

        this.unblock();

        Email.send({
            to: 'nicholasbydesign@gmail.com',
            from: email,
            subject: `${name} Test Subject`,
            text: message
        });
    }

Thanks for any insight.

** UPDATE **
I made the mistake of using an arrow function rendering ā€œthisā€ useless and switched to port 587 from 25;

1 Like

While it isn’t an answer to your specific issue, I’d strongly suggest checking out mailgun-js, and using their api to send rather than relying on Meteor’s built-in smtp functionality.

I’ll definitely check it out, I more or less just need solution to send email. I’d prefer to keep it Meteor native, but I don’t mind checking other solutions.

I think your syntax is causing the this to be undefined? Also, are you importing check?

sendEmail(name, email, message) { // Don't use an arrow here
      
      check([name, email, message], [String]);

      this.unblock();

      Email.send({
        to: 'nicholasbydesign@gmail.com',
        from: email,
        subject: `${name} Test Subject`,
        text: message
      });
      
    }

Check is imported. Good catch on the arrow function, that solved the ā€œthis.unblock()ā€ for any lexical issues. However I am still getting a timeout.

ā€œException while invoking method ā€˜sendEmail’ Error: connect ETIMEDOUTā€

I am wondering if it has something to do with my MAIL_URL.

My only guess there is that you are using port 25 and I’ve always used 587 when designating a mailgun smtp url? Also you may not want to post your credentials =)

Yea that was it, port 25 vs port 587. I’m not worried its sandbox.

Thanks for the help with debugging :slight_smile:

1 Like

why do you recommend this?

Looping through the built-in smtp call isn’t going to be very performant. Also, as you get into sending hundreds (or thousands) of emails, you will be able to utilize mailgun’s batch sending, which is built into their api.

Those are good reasons. It doesn’t look like mailgun’s API is that much different to use than meteors (no learning curve there). thanks!

1 Like