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


#1

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;


#2

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.


#3

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.


#4

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
      });
      
    }

#5

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.


#6

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 =)


#7

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

Thanks for the help with debugging :slight_smile:


#8

why do you recommend this?


#9

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.


#10

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!