Missing host to link to! Please provide the :host parameter, - ruby-on-rails

I know this issue is there a thousand times. But mine seems strange :-)
I am using heroku. I wanna mail via SMTP. What are my correct settings for sending emails?
Please keep in mind, that my MailService is something like "gmail" or "gmx" and not my host superapp.herokuapp.com
my application.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
from: ENV['MAIL_SENDER_EMAIL_ADDRESS'],
address: ENV['MAIL_SENDER_SERVER_HOST'],
authentication: :login,
enable_starttls_auto: ENV['MAIL_SENDER_TLS'],
user_name: ENV['MAIL_SENDER_USERNAME'],
password: ENV['MAIL_SENDER_PASSWORD']}
config.action_mailer.default = {
:charset => "utf-8",
from: ENV['MAIL_SENDER_EMAIL']}
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
in my production.rb
config.action_mailer.default_url_options = {host: ENV['RAILS_HOST']}
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
What is the right config to get rid of that error?
ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true)
ENV['RAILS_HOST'] = "superapp.herokuapp.com" does not work

Loading it as an ENV variable caused it to throw the error for me and confused me for the past few hours.
In the end I added the following with the correct value to each environment file:
test.rb / development.rb / production.rb
Devise 3.2.x
Rails 4.1.x
Thanks to maudulus
# Default Mailer Host
Rails.application.routes.default_url_options[:host] = 'domain.com'

Related

Error sending emails with Rails 4 & Devise (connection refused, port 25)

I'be been having some massive issues getting emails to send. I've tried multiple ways of doing things; using gmail, mailgun, sendgrid, the mailgun gem etc, no luck at all. For some reason it's trying to send using port 25 although, as you can see below, I'm specifying port 587. The error I get is:
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
My development.rb file is as follows:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => 'smtp.mailgun.org',
:port => 587,
:api_key => 'key-5d321b99f722e71f759993ee41512b99',
:user_name => 'postmaster#sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:password => '(mailgun password)',
:domain => 'sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:authentication => :plain,
:enable_starttls_auto => true
}
I have looked around for solutions however none seem to solve the issue. I'm totally out of ideas.
entire Development.rb document:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = false
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
user_name: "chris.edhogben#gmail.com",
password: "<password>",
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Try This configuration
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'localhost',
port: 25,
domain: 'exmaple.com',
enable_starttls_auto: false
}
Use the below settigns in production.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.mailgun.org',
:port => 587,
:user_name => 'postmaster#sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:password => '(mailgun password)',
:domain => 'sandbox3ed8de9510c845c68bf0d867a8f25394.mailgun.org',
:authentication => :plain
}
Use the below settigns for gmail:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
user_name: "username",
password: "password",
authentication: 'plain',
enable_starttls_auto: true
}
If you get SMTP authentication error , then click on http://www.google.com/accounts/DisplayUnlockCaptcha & enable it. It will enable google for registering new apps within 10 minutes
add same mail configuration in production.rb also.
Restart the server and check

RefineryCMS inquiries not sending email

I'm working with an old RefineryCMS 1.0.8 project and migrating it to another server. It all works apart from the contact us form.
Here is my setup in the production.rb file
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :sendmail
# Defaults to:
# config.action_mailer.sendmail_settings = {
# location: '/usr/sbin/sendmail',
# arguments: '-i -t'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
if ENV['MAILER_USERNAME'].present? && ENV['MAILER_PASSWORD'].present?
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "my_domain.com.au",
user_name: "#{ENV['MAILER_USERNAME']}",
password: "#{ENV['MAILER_PASSWORD']}",
authentication: 'plain',
enable_starttls_auto: true
}
end
I've set the environment variables in my /etc/environment and /etc/apache2/envvars files like so;
export MAILER_USERNAME="<email address>"
export MAILER_PASSWORD="<password>"
I've also set the tld to a length of 2 as we are in Australia so we are using a .com.au. This is set in my config/application.rb
config.action_dispatch.tld_length = 2
Here is the error I get;
Sent mail to <my gmail address> (2924ms)
There was an error delivering an inquiry confirmation:
555 5.5.2 Syntax error. w20sm33182626pfi.31 - gsmtp
You should wrap your email in <> blocks.
export MAILER_USERNAME="<map#gmail.com>"
export MAILER_PASSWORD="password"
I found the problem was not the brackets around the email it was the fact that I hadn't setup the DNS to that machine yet. Once I did that last night and went to the refinerycms website at it's domain instead of the IP then I could send emails.

How to send emails from Rails 4 with Postfix on production

I know this question is posted already here but all the solutions provided didn't work for me.
I already installed Postfix and tested it successfully with:
date | mail -s test myemail#example.com
My config/environments/production.rb has:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "myhost",
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
I even changed :smtp with :sendmail
config.action_mailer.delivery_method = :sendmail
It didn't work as well.
The /var/log/mail.log doesn't show anything my application log as well nothing.
Postfix is up and running fine, any help is much appreciated.
Ps: Is there something I am missing here? I only want to send emails from my app not receive them.

Changed the domain name of heroku ruby on rails app, user mailer emails sending links to wrong domain? [duplicate]

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. :)

Rails action_mailer config not working in production

I am trying to setup action_mailer with Rails 4.1.0 to send emails.
In my config/environments/{env}.rb, I have:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
domain: 'domain.com',
authentication: 'login',
user_name: 'email#domain.com',
password: 'pass',
enable_starttls_auto: true }
It's working fine in development, but in prod it's trying to connect to localhost.
m = Mail.new
m.delivery_method
=> #<Mail::SMTP:0xbd79fdac #settings={:address=>"localhost", :port=>25, :domain=>"localhost.localdomain", :user_name=>nil, :password=>nil, :authentication=>nil, :enable_starttls_auto=>true, :openssl_verify_mode=>nil, :ssl=>nil, :tls=>nil}>
But when I do "puts Rails.application.config.action_mailer" Im getting the correct config
{:raise_delivery_errors=>false, :default_url_options=>{:host=>"domain.com"}, :delivery_method=>:smtp, :smtp_settings=>{:address=>"smtp.mandrillapp.com", :port=>587, :domain=>"domain.com", :authentication=>"login", :user_name=>"email#domain.com", :password=>"password", :enable_starttls_auto=>true}, :assets_dir=>"xxx", :javascripts_dir=>"xxx", :stylesheets_dir=>"xxx", :asset_host=>nil, :relative_url_root=>nil}
Any idea?
The Mail gem won't use your ActionMailer config by default. ActionMailer uses Mail but those configuration settings will only apply if you send mail via ActionMailer.
If it's working in your dev mode, then you certainly have configured the Mail gem separately in your development.rb environment file, or in an environment specific initializer.
I forgot I had this config in my development.rb
Mail.defaults do
delivery_method Rails.configuration.action_mailer.delivery_method, Rails.configuration.action_mailer.smtp_settings
end
You need to add the host option for the default_url_options hash on the production.rb file, like so:
config.action_mailer.default_url_options = { :host => ENV['HOST_DEFAULT_URL'],
only_path: false }
That should do the trick.

Resources