Rails 3 Action Mailer Gmail - ruby-on-rails

I am trying to understand how to properly configure Action Mailer for Rails 3 to work with Gmail. Read the article by Ryan Bates and also read the edge Rails page article. Ryan's article said to put the config details in /config/initializers/setup_mail.rb initializer file
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "asciicasts.com",
:user_name => "asciicasts",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
But the edge Rails article said put it in the config/environments/$RAILS_ENV.rb, anyone know which is the preferred way of doing it?

Put it wherever it makes sense for your particular project. If you need different settings for different environments then do that, otherwise you can put it into an initializer.

Related

Adding sensitive data to heroku application (ruby on rails)

I am trying to set up a mailer with my heroku application. I fear if I push up my production.rb, my gmail password will be exposed. How can I prevent this?
Production.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:user_name => "whatever#gmail.com",
:password => "**********"
}
You can use environment variables: https://devcenter.heroku.com/articles/config-vars
It's a best-practice to never include sensitive information in your repo. Env variables are also a good way to go, because they'll work with whatever provider you use, so if you move from Heroku to another platform, you won't need to make any changes to your code.

Rails Mailer is not actually sending emails

I have just started learning ruby on rails and this was one of the tutorials i followed:
http://railscasts.com/episodes/206-action-mailer-in-rails-3
I followed it to a T and I have yet to receive anything in my gmail account. Is it fair to assume some updates to ruby have changed how you do things?
My best guess from reading everything over the net, the tutorial is missing actually setting the mailer to use smtp setting.
Here are my smtp settings:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "alexei.herokuapp.com",
:user_name => "mygmailaccount",
:password => "mygmailpassword",
:authentication => "plain"
:enable_starttls_auto => true
}
Could it be that i am still in local environment when testing this?
In that Railscast, Ryan has a line in there that doesn't send any email in development:
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
Did you put that in? If so, then no email will go out when you run your app locally in development.
Try removing it.
Also open myapp/logs/development.log to see if what shows up there when you try to send an email.

Rails - using multiple email providers in single app

Anyway I can use multiple email providers within the same Rails 3 app ?
Context
1. Im using postmark for sending out mails currently (using delayed job)
2. Our app also needs to send out some mass emails - for which we will be using a separate provider.
Now I dont want to separate out and create a new app for the mass emailing part. How can I use/choose different email providers at the point of sending email ?
Thanks in advance
You can override ActionMailer settings on a per mailer basis, for example
class BulkMailer < ActionMailer::Base
self.smtp_settings = {...}
end
will cause BulkMailer and its subclasses to use those settings.
The one thing to be wary of is not to change smtp_settings in place, i.e. do not do something like self.smtp_settings[:user_name] = 'blah' as this would be acting on the shared settings rather than creating new settings private to BulkMailer
I'm using mailserver fallback in my application, so when one mail server is down it switches mailserver. Your problem is similar, except you don't need to alias the old Mail::Message.deliver and use Mail::Message.mass_deliver for instance.
This is how you do it:
Mail::Message.class_eval do
def mass_deliver
self.delivery_method.settings = {
:address => "smtp.massdeliverserver.com",
:port => 587,
:domain => 'yourdomain.com',
:user_name => 'mass-email#quadnode.com',
:password => 'yourpassword',
:authentication => 'plain',
:enable_starttls_auto => true
}
deliver
end
end
Then you could use YourMailer.your_method.deliver to use defalt settings you provided in environment.rb for config.action_mailer.smtp_settings and YourMailer.your_method.mass_deliver to use the other server settings.
Put the code inside some file in config/initializers and mass_deliver method will be available for any Mail::Message instance in your application.
You have a mass email list to which you need to send out from say mass-email#email.com and some other emails for other purpose from otheremail#email.com
You need to do these steps if i am getting the question correct ::
Remove the default from default :from if you have written it.
Create an action mailer for mass-email and put up the :from => "mass-email#email.com"
Go to your environment.rb file and fill up the details like this
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'yourdomain.com',
:user_name => 'mass-email#quadnode.com',
:password => 'yourpassword',
:authentication => 'plain',
:enable_starttls_auto => true
}
You can create it for as many files as you wish.
Hope it helps.

What is the :domain symbol referring to when configuring action mailer?

Appname::Application.configure do
config.action_mailer.delivery_method = :smtp
#typical smtp_settings for gmail account
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.of.sender.net",
:authentication => "plain"
:user_name => "spencecooley"
:password => "secret"
:enable_starttls_auto => true
}
end
I have two questions about configuring action mailer
Do you know what the :domain symbol is referring to? Is it talking about the domain name of the application? Is it talking about the mail server domain? I saw baci.lindsaar.net written in on a few sites that I googled, but I don't know why people are using that particular domain. List item
I also don't know what the :enable_starttls_auto => true is doing
update:
Ok, so I found this in the docs in reference to question 2
:enable_starttls_auto - When set to true, detects if STARTTLS is enabled in your SMTP server and starts to use it
Didn't know what STARTTLS was, so I looked it up here http://en.wikipedia.org/wiki/STARTTLS
update:
I found this in the docs, but still don't understand
:domain - If you need to specify a HELO domain, you can do it here.
so I guess the new question is: what is a HELO domain? can't seem to find a clear answer.
The :domain key is set up for HELO checking. You don't need to specify this if you're using GMail.
The STARTTLS call starts an encrypted connection with your mail server, which is required to use GMail's SMTP.

how to set up restful_authentication email activation using gmail SMTP?

I have installed restful_authentcation from technoweenie with activation, and so I see the generated UserMailer < ActionMailer::Base.
However, the instructions don't include how to set it up to work with Google SMTP.
I am guessing that environments/development.rb needs to have the SMTP settings, but still not sure given Google (via Google apps) are all TLS.
Anyone set up activation using restful_authentication?
I currently put into environments.rb the following:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "xxx#mydomain.com",
:password => "mypassword"
}
Thanks!!
As far as I know, ActionMailer doesn't do TLS out of the box (2.3.2). A couple of months ago I had the same issue and found some code on a Japanese page and integrated that. it appears that code has been wrapped up into a plugin now (with english docs yeah!). That's not exactly what I'm using, but it advertises the same effect.
so add this plugin:
http://github.com/openrain/action_mailer_tls/tree/master
and in environments/development.rb or environements.rb you need something like this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "yourdomain.com",
:user_name => "first.last#gmail.com",
:password => "passwd",
:authentication => :plain
}
I see that :enable_starttls_auto => true is now in the docs, but it wasn't when I started. this at least works for me...
Edit: for some reason that link doesn't work if you follow it, but copy paste in the address bar and it's live...
I've never used SMTP from ruby (I have from python), but that looks right. You have the right domain and port (actually, multiple ports are supported, but that's one of them), and you're using starttls and AUTH PLAIN, which Google does use.

Resources