Rails 4 action_mailer host - ruby-on-rails

For Rails 4, I am using config.action_mailer.asset_host = "http://localhost:3000" in
config > environments > development.rb
to load assets to my mailer.
Is there a better value than a hardcoded url? I imagine there should be one since I would not use this value in my production.rb. What is the code to find the current host url in rails config?

If you deploy to a known host, you can put it directly in production.rb
But if you really need to read it from the url, than you check this out
http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url

If you're using SendGrid, inside config/environment.rb file specify your ActionMailer settings to point to SendGrid’s servers.
ActionMailer::Base.smtp_settings = {
:user_name => 'your_sendgrid_username',
:password => 'your_sendgrid_password',
:domain => 'yourdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}

Related

Missing host to link to Rails heroku production

I have an app that I am trying to put onto production. When the user signs up they have an activation email that should be sent to their email accounts but I am getting stopped with this 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)
The problem with this error is that I have everything set up correctly in my files. For example my production.rb file inside my environments folder looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'secret-everglades-54128.heroku.com'}
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
My enviorments.rb file looks like this:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
config.action_mailer.default_url_options = { :host => 'secret-everglades-54128.heroku.com'}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
This does not make any sense to me whatsoever, if anyone could shed some light on this problem I will be very great full.
The problem was that I had errors still concuring when I ran the command
git push heroku master
The problem was that I had my host set in the actually environment.rb file which wasn't needed so I removed that and once I did that I was able to get everything to work.

Net::SMTPFatalError (550 Unauthenticated senders not allowed; tried varoius methods to solve the issue [duplicate]

I have integrated Sendgrid settings on a Rails 4 server. These settings work fine for development environment. But this is giving error on production environment.
Net::SMTPFatalError (550 Cannot receive from specified address <simmi#mydomain.com>: Unauthenticated senders not allowed)
config/initializers/email_setup.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:domain => DOMAIN,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true
}
config/initializers/devise.rb
config.mailer_sender = 'simmi#mydomain.com'
config/environments/production.rb
# Default URL
config.action_mailer.default_url_options = { host: 'mysite.mydomain.com' }
DOMAIN = 'mysite.mydomain.com'
According to sendgrid support team, this error comes when username or password are incorrect. I tried logging manually into the smtp server through telnet and it was working.
On my server commandline, I followed these steps:
telnet smtp.sendgrid.net 587
EHLO
AUTH LOGIN
Enter username in Base64
Enter password in Base64
Link to convert text into Base64 - http://www.opinionatedgeek.com/dotnet/tools/base64encode/
The ENV variables were somehow not working on my production environment. As a workaround, I tried adding the username and password directly and it worked.
I have also faced the same problem and fixed it by adding the following:
config/environment.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:domain => DOMAIN,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options = { host: 'mysite.mydomain.com' }
config/application.rb
ActionMailer::Base.delivery_method = :smtp
The letter_opener gem is very useful if you want to test sending emails in development mode.
If you want to overwrite the letter_opener, add the following configuration
config/environments/development.rb
ActionMailer::Base.delivery_method= :letter_opener
And also add the port under ActionMailer::Base.smtp_settings.
You are probably loading your environment variables after you are trying to initialize your mailer. You can do the initialization directly after loading your variables to be sure that they exist.
Set up a config file with your username and password variables:
# config/mailer.yml
production:
SENDGRID_USERNAME: 'username'
SENDGRID_PASSWORD: 'password'
Set up an initializer file:
# config/initializers/mailer.rb
if Rails.env.production?
config_path = File.expand_path(Rails.root.to_s + '/config/mailer.yml')
if File.exists? config_path
ENV.update YAML.load_file(config_path)[Rails.env]
end
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:domain => "yourdomain",
}
end
If your production environment is Heroku:
Login to your Heroku account and select the application. Under "Settings", click the "Reveal Config Vars" button. Enter in your sendgrid key and value pairs, then submit. Run: heroku restart.

How do I setup different ActionMailer Base smtp settings for development and production?

I have ActionMailer working correctly in both production and development. I use different smtp settings for each environment, gmail for development and a SendGrid account through Heroku for production. I manually switch the settings in the setup_mail.rb file to work in development and then switch them back before pushing into production. This prevents my gmail password from becoming public on github as the SendGrid/Heroku settings do not require my password in the file:
development setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mysite.com",
:user_name => "me#mysite.com",
:password => 'mypassword',
:authentication => "plain",
:enable_starttls_auto => true
}
production setup_mail.rb
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
I am concerned that I will accidentally push the development settings with my password to github. I'd like to stop switching settings manually to prevent this from happening. How do I setup different ActionMailer Base smtp settings for development and production? Thanks
Have this setting in production.rb and development.rb, instead of having your password hard coded, you could use environment variables locally too, create a .env file in your project, which will be loaded when you cd in:
EMAIL=me#mysite.com
EMAIL_PASSWORD= mypassword
Use ENV['EMAIL'] AND ENV['EMAIL_PASSWORD'] in development.rb

Configure mail.rb for localhost testing

is it possible to configure mail.rb (in RESTFUL authentication) to test email activation locally? the default file is
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "mail.example-domain.com",
:port => 25,
:domain => "www.example-domain.com",
:authentication => :login,
:user_name => "user#example-domain.com",
:password => "secret"
}
thanks
This might help:
http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer
Just make a yaml file with all the config
I really like using MailTrap -- a local SMTP server that knows Just Enough about SMTP to listen to ActionMailer requests... and write them to a file where you can look at them later.

Why wouldn't a specific environment override environment.rb in rails here?

I'm trying to work on a Rails site here in it's development environment, where I want to test email delivery, and I can't work out why properties declared in the main environment.rb aren't being overwritten by the more specific development.rb file that I presume would be loaded when booting a rails app.
My understanding here is that values in more specific environment config files like this should override the shared 'environment.rb' config file, so if I have declared some email settings like so in config/environment.rb...
ActionMailer::Base.smtp_settings = {
:address => 'smtp.hostingcompany.com',
:port => 25,
:domain => 'productiondomain.net',
:authentication => :login,
:user_name => "productiondomainmailer",
:password => "TOP_SEKRIT"
}
... then code here in config/environments/development.rb below should override the ActionMailer:Base.smtp_settings hash:
ActionMailer::Base.smtp_settings = {
:domain => 'developmentdomain.net'
}
However, when load the app in the development environment, or from script/console to check the value of ActionMailer::Base.smtp_settings[:domain], it's still listed as 'productiondomain.net'.
Why might this happening?
To set the configuration for your development environment put:
config.action_mailer.smtp_settings =
rather than:
ActionMailer::Base.smtp_settings =
in config/environments/development.rb
Note that in config/environments/development.rb you will need to specify the full set of settings and not just :domain as the hash overwrites the existing value rather than being merged with it.
And similarly, set the default in config/environments.rb by putting config.action_mailer.smtp_settings = inside the Rails::Initializer.run do |config| block.
This should work:
config.action_mailer.smtp_settings = {
:address => 'smtp.some_host.com',
:port => 25,
:domain => 'developmentdomain.net',
:authentication => :login,
:user_name => 'blabla',
:password => "some_pass"
}

Resources