Sending email using Gmail SMTP - Meteorjs
Asked Answered
G

4

6

Hi I am trying set up my gmail account to send email for my Meteor app, not very easy so far

server.js

Meteor.startup(function () {
  smtp = {
    username: 'xxxxx',   // eg: [email protected]
    password: 'YYYYYYYY',   // eg: 3eeP1gtizk5eziohfervU
    server:   'smtp.gmail.com',  // eg: mail.gandi.net
    port: 465
  }

  process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port;
});

Email.send({
  from: "[email protected]",
  to: "[email protected]",
  subject: "Meteor Can Send Emails via Gmail",
  text: "Its pretty easy to send emails via gmail."
});

And the testing email is never sent with the below error code saying that I havent set the environment variable.

I20150715-18:14:02.641(0)? ====== BEGIN MAIL #0 ======
I20150715-18:14:02.642(0)? (Mail not sent; to enable sending, set the MAIL_URL environment variable.)
I20150715-18:14:02.643(0)? MIME-Version: 1.0
I20150715-18:14:02.643(0)? From: [email protected]
I20150715-18:14:02.643(0)? To: [email protected]
I20150715-18:14:02.643(0)? Subject: Meteor Can Send Emails via Gmail
I20150715-18:14:02.643(0)? Content-Type: text/plain; charset=utf-8
I20150715-18:14:02.643(0)? Content-Transfer-Encoding: quoted-printable
I20150715-18:14:02.644(0)? 
I20150715-18:14:02.644(0)? Its pretty easy to send emails via gmail.
I20150715-18:14:02.645(0)? ====== END MAIL #0 ======

Could this be due I am running this on C9?

Thanks

Germane answered 15/7, 2015 at 18:17 Comment(3)
seems these links will help u One & TwoTemporize
not sure if this will help, but maybe try port 25 or 587. Depends on your configuration: support.google.com/a/answer/176600?hl=enNickell
Maybe it is because I cant set the environment variable of my testing site in Cloud9 platform?Germane
U
3

Sucks that cloud9 does not permit the setting of environment variables. Perhaps you can set the Meteor.settings object rather than an environment variable?

http://docs.meteor.com/#/full/meteor_settings

Essentially, you can pass JSON to meteor when you start it using --settings. These will be available on the server side only, unless wrapped under a "public" object of the root.

{
  'public': {
     'some-setting': 'some-value'
  },
  'other-setting': 'other-value'
}

That is a work around for dealing with no envrionmental variables. As the core email package always looks at the MAIL_URL environmental variable, you will probably need to send email through another service/provider.

Mandrill (by Mailchimp) have a sizeable free tier and will allow you to send transactional email. You can even make a mail template in mailchimp, export to HTML, import to mandrill and pass in merge variables in your API calls.

Better yet, there is a kick-ass package for writing to the Mandrill API. https://atmospherejs.com/wylio/mandrill

Hope that helps!

Elliott

Unsnarl answered 16/7, 2015 at 8:33 Comment(2)
Nice, havent tried it yet, but it looks awesome, you should make a complete tutorial on setting up email in Meteor, most of the one I found on net are either incomplete or out-dated.Germane
There shouldn't be a problem setting Environment variables on Cloud9. You can do it on the shell like you would on a local machine, or if you want specific environment variables available just to the running application, there's an ENV button on the run panel that allows you to set custom Environment variables.Outguard
G
1

but decided to answer my own question anyway.

I didnt and I cant set the C9 or my testing site env variable, so that s why.

Germane answered 15/7, 2015 at 18:44 Comment(0)
R
0

The Meteor.startup(callback) method is executed at the end of Meteor initialization, but you are sending your email before the startup, so you should send it in the same block, after process.env.MAIL_URL..

Reitareiter answered 8/1, 2017 at 19:53 Comment(0)
B
0

You CAN set environmental variables using C9! You just pass it in when you run meteor.

BUT you can not send email using SMTP though on c9!

To make my life easier I just don't test my emails on c9 with Meteor. If I absolutely need to I bypass the email function and use a custom emailer instead that sends using the MailGun REST API (NOT SMTP!!) See Sending email using Gmail SMTP - Meteorjs

Basilisk answered 27/11, 2017 at 17:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.