im having a problem getting sendgrid to send emails successfully on a rails 3.1 app that's using authlogic for authentication and is being deployed on heroku. i have the following action mailer configuration on config/environments/[development.rb and production.rb]:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.default_charset = "utf-8"
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => 587,
:domain => ENV['SENDGRID_DOMAIN'],
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => 'plain',
:enable_starttls_auto => true
}
for production.rb, the above code is the same except for
config.action_mailer.default_url_options = { :host => [app name in heroku] }
when i run it development mode, i get the following error reported:
Completed 500 Internal Server Error in 21740ms
Net::SMTPFatalError (550 Cannot receive from specified address notification#[app-domain]: Unauthenticated senders not allowed
):
i now dont really know how to set it up to get it working. does anyone with some prior experience on setting up sendgrid on heroku and rails know what's going on?
thank you so much. you guys are the best!!!
I spent half a freakin' day on this and finally got mine working now. Quite frustrated as it was due to a poor documentation error. I'm running Rails 3.1 and Cedar stack on Heroku by the way.
So http://devcenter.heroku.com/articles/sendgrid will tell you to put your SMTP settings stuff in config/initializers/mail.rb. BUT... on http://docs.sendgrid.com/documentation/get-started/integrate/examples/rails-example-using-smtp/ it says to put all your SMTP settings stuff in config/environment.rb instead of config/initializers/mail.rb
So the solution is to put that in your environment.rb file. This is how my environment.rb looks:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Freelanceful::Application.initialize!
# Configuration for using SendGrid on Heroku
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:user_name => "yourSendGridusernameyougetfromheroku",
:password => "yourSendGridpasswordyougetfromheroku",
:domain => "staging.freelanceful.com",
:address => "smtp.sendgrid.net",
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
To get your SendGrid username and password, type
$ heroku config -long
Hope that helps.. and more people in the future of this headache.
I'm assuming you mean development mode as in locally? If so, I don't think the SendGrid add-on let you send email from outside the Heroku network (as they have standalone accounts that they would prefer you to use).
Saying that, you don't need to configure mail in production when using the SendGrid add-on as it is automagically configured for you when you deploy your application.
Therefore you can remove your config.action_mailer.smtp_settings code and simply use the default in development.
Also note that if you're running your Heroku app on the Bamboo stack you don't need to configure your settings in the environment.rb file since Heroku does it for you.
However you do need to git push at least once after you activated the app to Heroku to set these settings. I made that mistake this morning and found your post.
Related
I am trying to push my app on heroku. I am still in dev.
I use devise with the confirmable module.
When I try to add a user with the heroku console I got this error:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
in test and dev environment i have the following line:
environments/development.rb and environments/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I don't have set up something in the production environment.
I've tried to push with
config.action_mailer.default_url_options = { :host => 'mywebsitename.com' }
config.action_mailer.default_url_options = { :host => 'heroku.mywebsitename.com' }
but it doesn't work too..
I see on the web that it could be related to ActionMailer but I don't know what I have to configure.
Many thanks for your idea!
EDITED:
Hi,
In order to not make my app crashes when I push on heroku I put this in my env/test.rb and my env/dev.rb (not in env.rb I think it is because it's a rails 3 app)
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
But when I tried to create a user in the heroku console:
User.create(:username => "test", :email => "test#test.com", :password => "test1234", :password_confirmation => "test1234", :confirmed_at => "2010-11-03 14:11:15.520128")
here are errors I got:
ActionView::Template::Error: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:473:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:195:in `user_confirmation_url'
EDITED (2)
When I type heroku logs on console I got this ==> production.log <== So I think when one deploys on heroku it's already in production.
I configure the env/prod.rb like this:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
and now I have this as error when I try to create a User:
Errno::EAFNOSUPPORT: Address family not supported by protocol - socket(2)
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `initialize'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `open'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/ruby1.8.7/lib/ruby/1.8/timeout.rb:62:in `timeout'
You need to add this to your environment.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
Make sure you change host to your production url and keep it localhost for development. This is for the mailer, it needs a default email to send out notices such as confirmations etc...
You should check the logs on the heroku server heroku logs run that from the console and it will tell you the exact error.
When you push to heroku you need to configure the environment.rb file with the heroku subdomain:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Depending upon version, this should go in production.rb, not environment.rb.
Ok,
First you have to install the sendgrid gem with this command line:
heroku addons:add sendgrid:free
Then you just have to configure your env/dev.rb and env/prod.rb like this:
env/dev.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
env/prod.rb
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Push on git and heroku. It should work..
Codeglot's anwser above does the job, but we wanted something a bit more flexible, so we did this:
On Heroku, we run multiple Production environments for staging and testing, so we need a flexible solution for the production.rb environment file.
In production.rb
config.action_mailer.default_url_options = { :host => ENV['MAILER_URL'] }
Then set the MAILER_URL environment variable for your app like so
heroku config:set MAILER_URL=my-awesome-app.herokuapp.com --app my-awesome-app
If you're running on Cedar:
run heroku addons:add sendgrid:free from your console.
Add the following lines to config/environments/production.rb in your app.
.
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'YOUR-DOMAIN-HERE.COM' }
I had to do a number of things to get it to work in the production environment:
Inside of my production.rb file (/config/environments/production.rb) I added the following:
Rails.application.routes.default_url_options[:host] = 'myappsname.herokuapp.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
This is with Rails 4 and Devise 3
The working one after so many research,
Don't forget to add default from: mail address in your ApplicationMailer (application_mailer.rb) as,
class ApplicationMailer < ActionMailer::Base
default from: 'yourmail#gmail.com'
layout 'mailer'
end
Add the below configuration in your production.rb.
config.action_mailer.default_url_options =
{ :host => 'yourapp.herokuapp.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'heroku.com',
user_name: 'yourmail#gmail.com',
password: 'yourgmailpassword',
authentication: 'login',
enable_starttls_auto: true
}
Enable IMAP from your Gmail settings in Forwarding IMAP/POP tab.
Allow less secure apps: ON from https://myaccount.google.com/lesssecureapps
You're now good to go. :)
I have my Sendgrid password set in an external file (config/application.yml) which I set up with the Figaro gem. This works fine on my local machine, but on my server I am getting an error that no password has been set:
ArgumentError (SMTP-AUTH requested but missing secret phrase)
When I change the Sendgrid config to just the plaintext password it works fine, so I assume that Rails isn't recognising the environment variable. The weird thing is that when I go into rails console production and execute puts ENV["SENDGRID_PASSWORD"] it works fine.
Any ideas?
Here's my Sendgrid config:
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587,
:user_name => "chrislawrence",
:password => ENV['SENDGRID_PASSWORD'],
:domain => "lakecinema.net.au",
:authentication => :plain,
:enable_starttls_auto => true
}
I had the same problem and couldn't figure out why Rails thought I wasn't giving it a password. Turns out I was defining config.action_mailer.smtp_settings in one file, and then adding key-value-combos to it in another file. The problem is that I was using merge instead of merge! so my password was never making it into smtp_settings but instead into a temporary hash.
From this experience I learnt that the error message you're getting is ActionMailer's way of saying "Where's the password?"
So my guess is that your issue is the inverse of Environment variable in Rails console and Pow, where an environment variable is working on the Rails server but not in Rails Console. Try executing:
$ echo "$SENDGRID_PASSWORD"
and see what you get. I have a feeling the Env variable is not set but that instead it's just a local variable in Rails Console.
It should be :authentication => 'plain'
I need to use a mailer for sending out emails to users to set their passwords to the "recoverable" function of Devise and active admin. On the development environment I have done this by adding the following to these files:
config/environments/development
#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#These settings are for the sending out email for active admin and consequently the devise mailer
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings =
{
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com', #you can also use google.com
:authentication => :plain,
:user_name => 'XXXXX#gmail.com',
:password => 'XXXXXXX'
}
How do I get the same functionality for the production environment? I want to deploy my app to Heroku. What files and code would I need to add?
All configurations you have set in Development mode will work EXCEPT you will need to reconfigure the default mailer url.
So.
Copy-paste your settings from development.rb.
Point your default mailer to your heroku app:
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
Also, be careful of any email limits your smtp may have when moving to production. It's hard to trigger gmail's smtp limits while developing, for example, but they could be more easily triggered in production.
If it works in development mode, then it will work in production mode.
Supposing everything is setup correctly, resetting a password in development will already send an actual email using your gmail account.
Devise only relies on the mailer config setup correctly (which you have done), and configuring devise to allow password reset, and possibly another setting for the From field of the email.
This should work fine!
As long as config/environments/production.rb has the same thing with an exception. The default_url_options should have a :host value of 'localhost' only in development and 'YOURAPPNAME.herokuapp.com' in heroku production.
i.e.
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
Remember to unlock captcha on gmail, otherwise it won't send email from heroku (unknown source). You can do that by going to this link: http://www.google.com/accounts/DisplayUnlockCaptcha
Just as a suggestion, I'd say move this from environments.rb
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
and place is in environments/development.rb as
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
It's not needed in production.
See Net::SMTPAuthenticationError when sending email from Rails app (on staging environment) for more information in regards to gmail seeing heroku as an unknown host.
I managed to configure ActionMailer on my local machine to send emails via Gmail. (it required tlsmail in gemfile)
### config/environment.rb
require 'tlsmail'
Ideas::Application.configure do
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => '587',
:domain => 'xxxx#gmail.com',
:user_name => 'xxxx#gmail.com',
:password => 'xxxxxxx',
:authentication => :plain
}
end
This worked on my local machine (the emails were sent) but as usually hreoku had some problems with this (Errno::ECONNREFUSED (Connection refused - connect(2))). I googled that they have a particular solution for gmail:
http://blog.heroku.com/archives/2009/11/9/tech_sending_email_with_gmail/
They are saying I need an additional SMTP TLS library. As mentioned above I added a gem that resolved the issue but only on my local machine. Well ok, I tried their solution and it worked... on heroku, but stopped working on my local. (it doesn't give an error, it just says the email was sent, but it never is.)
Environmental variables are set properly.
Do you have any ideas how to make at least one of this methods work both on my local machine and heroku?
Bye
You need to set it up in the correct environment. You'll need to do this in your gemfile with groups
group :development do
gem '<development gem here>'
group :production do
gem '<production gem here>'
Don't forget to rebundle. Then move the config relevant to each environment into either config/environments/production.rb or config/environments.development.rb
I'm trying to configure ActionMailer to send mail from Devise in development mode with my Google Apps account. I've added the following to my config/environments/development.rb file, but it looks like mail is not being sent. Note: this is for Google Apps, not Gmail (but the Gmail servers should work -- they do in my mail client).
Anything jump out as strange in my config?
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "mydomain.com",
:authentication => :login,
:user_name => "myemaiL#mydomain.com",
:password => "mypass"
}
We're using all the same settings successfully with our Google Apps account (and Devise) -- the only difference is that we're using "plain" for :authentication.
A slight difference -- we're using this in production and sort of on our staging environment (there we send email, but all to a test email address rather than to users). On development we just look in the rails log to debug emails...
But one thing you might check: I was testing using GMail on a macbook that had been set up with MacPorts, but with ruby/rails and other stuff set up using rvm and Homebrew and was getting SSL exceptions in this environment -- when I set :enable_starttls_auto => false the error stopped, but no mail was sent. I think there was a conflict between the libraries installed by MacPorts and the ones used by Rails.
Not sure if that helps :-)