Some confusion here. Using devise:invitable, and sending emails from heroku with the sendgrid add on.
On local, in the email that's generated when a user is invited, <%= root_url %> generates http://localhost:3000/ - which seems perfectly correct -
but when deployed to heroku, when I receive the invite email, that same <%= root_url %> is coming out as http://yourhost.com/ ...why? Where's that set?
I thought root_url worked off routes.rb automatically and I have
root :to => 'home#splash'
I feel like I'm missing something obvious? I added the suggestions from this question to the application controller, but it has made no difference, the email still references http://yourhost.com/
Help appreciated thank you.
In your config/environments/production.rb set
config.action_mailer.default_url_options = {:host => 'www.yourdomain.com'}
You probably followed the Devise setup instructions displayed after you installed Devise which gets you to add
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
to your developments.rb file - but you just forgot about production.
Related
I am trying to send an email to a user upon sign up in my web app, which is built with Rails in the back end and React-Redux on the front end. I used a gem called letter_opener when testing on a local server, and everything seemed to work fine. When I tried it live on Heroku, I got back a 500 error, and I'm not sure what is wrong.
I tried reading the ActionMailer docs, but it didn't seem to mention any issue like this.
The method in the UserMailer:
def welcome_email(user)
#user = user
mail(to: #user.email, subject: "Welcome")
end
Relevant code in UsersController:
def create
# ...
email = UserMailer.welcome_email(#user)
email.deliver_now # This line throws the error
# ...
end
The error from the Heroku logs:
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
I expected to the email to be delivered, or at least to have a more informative error message. I am not sure why Rails is looking at localhost's port 25.
I am not sure why Rails is looking at localhost's port 25.
ActionMailer defaults to localhost:25, and you probably only changed your development configuration.
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25)
Heroku doesn't provide an SMTP server, and there certainly won't be one on localhost. Use an email addon or some other third-party mail service of your choice.
You'll probably have to modify config/environments/production.rb to use whatever mail service you choose.
Chris is right, Heroku doesn't provide an SMTP server and you've to use an Addon. I'll further add on how you can proceed with using sendgrid addon which is free upto much extent.
First, goto your Heroku app -> Enable sendgrid addon. Then you'll be given sendgrid credentials. Keep them safe.
In your config/environments/production.rb add:
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: "your-domain.com", protocol: 'https' }
Then in your config/environment.rb file add:
...
# Initialize the Rails application.
Rails.application.initialize!
# Add sendgrid support
if Rails.env.production?
ActionMailer::Base.smtp_settings = {
:user_name => "sendgrid_username",
:password => "sendgrid_password",
:domain => "your-domain.com",
:address => "sendgrid_hostname",
:port => 3000, # sendgrid_port
:authentication => :plain,
:enable_starttls_auto => true
}
end
Change the credentials above as required. Also consider using ENV variables for above credentials.
During my training, I'm working on a website and we use Ruby on Rails. We need to send mails to users so I created a mailer.
I have tried to put the smtp in both development.rb and environment.rb
config.action_mailer.default_url_options = {host: '0.0.0.0:3000'}
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = 'smtp'
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
adress: $SMTP_SERVER,
port: $PORT,
from: $MAIL,
enable_starttls_auto: true
#authentication: 'login'
}
It tells me that the error comes from this method line 6
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
# Tell the UserMailer to send a welcome Email after save
UserMailer.welcome_email(#user).deliver_now
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.json { render :json => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.json { render :json => #user.errors, :status => :unprocessable_entity }
end
end
end
I have set the port to 587 but i keep getting the error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost"
port 25
It looks as if another file was overwriting my settings. I also saw that it might be related to my ssh key not being authorized by the server.
Do know what is wrong?
Thanks in advance
First of all, when developing on localhost, it's common to not actually send out mail, rather to treat that as a deployment detail and stick with the Rails default behavior which is to spit out the mail headers and contents into the console STDOUT (where you can verify that the text looks right). Is there a specific reason why you need to test sending messages in the dev environment?
Secondly, you mentioned that you set the SMTP settings in both development.rb and environment.rb. You shouldn't need to set these settings twice; in general I'd use development.rb for settings specific to the dev environment, and environment.rb only for settings that will always apply to all environments (dev, tests, and on the live deployed server). So if you're setting the same settings in both development.rb and environment.rb, I'd start by removing one or the other; redundancy will only make your job harder down the road.
Finally, to troubleshoot this I'd start by asking Rails what its settings are rather than waiting for the mail delivery to fail. Try the following:
Start up rails console
Enter Rails.configuration.action_mailer.smtp_settings and compare the resulting hash against your expectations. This hash should contain the port and domain settings that are used when sending out all mail (in the current environment), so if ActionMailer is trying the wrong port then I'd expect the port to be wrong here too.
Where are you setting $SMTP_SERVER, $PORT and $MAIL? Is there any reason you aren't using Rails' convention for environment variables, ENV['SMTP_SERVER'] etc.?
Hope some of that helps. Good luck!
replace
config.action_mailer.delivery_method = 'smtp'
with
config.action_mailer.delivery_method = :smtp
Ensure your Rails.configuration.action_mailer.smtp_settings is symbolized keys
I ran into the same error message well developing my own application. What I discovered is that as I was not actually sending any emails in a development environment I needed to change one of the lines in the configuration file found at: /your_apps_name/config/environments/development.rb
from
config.action_mailer.raise_delivery_errors = true
to
config.action_mailer.raise_delivery_errors = false
This was causing my application to raise errors when emails were not successfully delivered, and I wasn't actually sending emails so of course they were not being delivered successfully.
The app might be using mailcatcher gem for all outbound emails on development, which you haven't installed or don't have running. At least that was my issue. Check out https://mailcatcher.me and follow the instructions given.
You need to remove config.action_mailer.perform_deliveries = true line.
I was running into this issue when running Sidekiq::Worker.drain_all in my RSpec tests, and it was driving me crazy because I had config.action_mailer.delivery_method = :test in my config/environments/test.rb.
The solution was to set config.action_mailer.delivery_method = :test in my config/environments/development.rb, which is confusing because the implication is that my config/environments/development.rb is overriding my config/environments/test.rb in my RSpec tests.
Regardless, this might fix the problem for others.
For anyone clumsy like me, I got this message when I had everything set up perfectly in my app, but I had simply forgotten to run the command that adds the mailing addon in my production environment. In my case, that line was heroku addons:create mailgun:starter.
Currently I have myapp.herokuapp.com as staging for a little project I'm working on. Once finished it'll be running under myapp.com.
When I'm running it locally, I'm using lvh.me:3000 to test things. This is working well for the problem I'm having, but since this staging environment is under a subdomain I'm having routing issues.
The problem is that my users are given a custom subdomain, thus, someuser.myapp.com points to users#landing. I accomplish this locally with this route:
match '', to: 'users#landing', constraints: lambda { |r| r.subdomain.present? && r.subdomain != 'www' }
My issue is that when I visit myapp.herokuapp.com I end up hitting users#landing instead of my root.
I'm seriously drawing a blank.
Totally overlooked what I was doing...
I ended up,
if request.subdomain == 'myapp'
redirect_to root_url
else
Which fixed the problem. I'm sure there's a better way to do this though.
Edit: furthermore, I added default_url_options in application_controller.rb
def default_url_options
if Rails.env.production?
{:host => 'myapp.herokuapp.com'}
else
{:host => 'lvh.me'}
end
end
I see I can test routes with rspec like this:
get("/").should route_to("welcome#index")
but I have constraints based on the hostname or parts of hostnames and redirects between several ones. How do I specify a hostname when testing?
How do I run the tests with proper configuration? I tried printing root_url and I got:
Missing host to link to! Please provide the :host parameter, set
default_url_options[:host], or set :only_path to true
The same error happens on mine whenever I run rspec spec/
The entire error is actually:
Failure/Error: #user = Factory(:user)
ActionView::Template::Error:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
# ./app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb__980323237__638702928'
# ./spec/models/campaign_spec.rb:21
The following line:
# ./app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb__980323237__638702928'
actually gave me the hint that devise is the one throwing out the error.
Turns out I haven't set
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
in config/environments/test.rb (only in development.rb)
Adding the config option cleared out the errors on mine.
I'm suspecting you're using other gems that requires the same option set.
None of the above worked for me. Adding the following to my spec_helper.rb (in my case spec/support/mailer.rb which is included in spec_helper.rb) fixed the error:
Rails.application.routes.default_url_options[:host] = 'test.host'
In my case I had to add
config.action_mailer.default_url_options = { :host => 'localhost:5000' }
to following to
config/environments/test.rb
because I was using FactoryGirl to generate a user without skipping the email_confirmation from user.
It's just been one problem after another today. I just deployed to my production server and testing it out, with issues whenever anything involves email. Particularly with Devise's confirmable registration email, whenever I sign up for an account, the following error is thrown up in the log...
ActionView::Template::Error (You can no longer call ActionMailer::Base.default_url_options directly. You need to set config.action_mailer.default_url_options. If you are using ActionMailer standalone, you need to include the routing url_helpers directly.):
Looking at my config/environments/production.rb, I have the following set...
config.action_mailer.default_url_options = { :host => 'localhost' }
So config.action_mailer.default_url_options IS being used, but is being completely ignored. I tried changing the host to '127.0.0.1' and my server's IP addy, but it wouldn't have it. Threw it in config/application.rb, but no go. I hunted in my project for any other lines declaring default_url_options, but it's only set in the production.rb file. I Googled and found a post suggesting to set config.cache_classes to false, but negative there as well.
Now here is what's crazy...I got the same error whenever I tried to submit a brand new comment in my project (set up to send an email to confirm the email address if you are a first time commentor). I took OUT config.action_mailer...etc from the production.rb file, and viola - my comment mailer worked and sent the email just fine!
Still, Devise is being absolutely stubborn and keeps throwing the above error at me. Any ideas on why? BTW, all my gems are up to date. Below is my production.rb file.
--- UPDATE ---------------
Have made a little progress, but this is just weird. When I start the server and attempt to do something that will send mail (user registration, comment), I get the ActionMailer::Base error message above. When I try the action AGAIN...it works and the mail gets sent. From that point on (until Passenger and/or the server is rebooted), all mailer actions work fine.
But not without yet another issue (it never ends)...for some reason the email body is completely blank.
I ended up hitting this problem once again when I attempted to test in the test database, and I was determined once and for all to figure out why I was getting this error, and why no one else seemed to have come across it (very little results from Google).
After completely tearing down my project and reconstructing it, I finally pinpointed the issues to the Sitemap-generator plugin I had installed. Once I removed all traces of that, the above error finally ceased.
I'm working on this issue for Fat Free CRM, and I realized that this error happens because Rails 3 lazy-loads classes. All you need to do is 'touch' the ActionMailer::Base class in your environments/**.rb files, and it will load the class. Then you can call ActionMailer::Base.default_url_options from a controller, etc. (We do this so that we can automatically set the mailer host from request.host_with_port)
So my environment files now look like this:
FatFreeCRM::Application.configure do
...
ActionMailer::Base
end
I believe I have solved this issue...though I'm not "exactly" sure what did it. It very well may have been config.cache_classes = true which I commented out, and although I was concerned this would effect my site caching or something else, it doesn't seem to have broke it.
In addition, the empty email body I solved by switching from :sendmail to :smtp.
Here is my final production.rb file, hopefully this might be of use to someone with a similar issue in the future.
MyProject::Application.configure do
# Commented out, causes 'ActionView::Template' error
#config.cache_classes = true
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = true
config.cache_store = :mem_cache_store
config.active_support.deprecation = :log
config.action_dispatch.best_standards_support = :builtin
config.action_mailer.raise_delivery_errors = true
Sunspot.config.solr.url = 'http://127.0.0.1:8080/solr'
Paperclip.options[:command_path] = "/usr/bin/"
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:enable_starttls_auto => true,
:port => 587,
:authentication => :plain,
:user_name => "user#domain.com",
:password => 'password'
}
config.action_mailer.default_url_options = { :host => 'dev.mydomain.com' }
config.time_zone = "Central Time (US & Canada)"
end