Rails Action Mailer TLS Certificate Issues - ruby-on-rails

I am trying to use rackspace ssl smtp for my mail settings for our rails application and I am receiving this error
hostname was not match with the server certificate
I need to find a way to disable verify on the certs for ActionMailer.
Anyone know how to do this?

Try setting enable_starttls_auto to false in your smtp_settings:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.smtp_settings = {
:address => 'server.com',
:port => '25',
:domain => 'yourdomain.com',
:user_name => 'username',
:password => 'password',
:authentication => :login,
:enable_starttls_auto => false
}

For some reason I can't comment on Derek's answer, but his answer does in fact work now with Rackspace. Simply changing tls to false now works great.

Resolved issue by using sendmail and configuring sendmail to use the proper smtp settings required to use rackspace email hosting.

Related

Action Mailer Configuration for Gmail

I'm trying to add e-mail delivery with Gmail SMTP to my app. I've already done the "less secure apps" way before but I don't want to use this option in this project.
I've tried to look into Google's documentation or some gem to make it work, but to no avail. Everyone just sends some code (like below, which is usually the same I have) or tells me to try 'less secure app'.
My current action mailer configuration on production.rb is:
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DOMAIN_NAME'] }
config.action_mailer.asset_host = ENV['DOMAIN_NAME']
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:authentication => :plain,
:user_name => ENV['USERNAME'],
:password => ENV['PASSWORD'],
:enable_starttls_auto => true
}
Some people say I'd need ":domain => 'gmail.com'" but with the 'less secure app' option it works, so my guess is that the problem is not that simple. Also, people talk about changing 'authentication: :plain' to :login.
Also, I realize that in the official Rails documentation it says:
Note: As of July 15, 2014, Google increased its security measures and now blocks attempts from apps it deems less secure. You can change your gmail settings here to allow the attempts. If your Gmail account has 2-factor authentication enabled, then you will need to set an app password and use that instead of your regular password. Alternatively, you can use another ESP to send email by replacing 'smtp.gmail.com' above with the address of your provider.
(From http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail)
But I'm not sure if this solution still requires enabling the 'less secure app' option, which is not what I need.
Has anyone solved this problem without resorting to 'less secure app'?
Thanks in advance!
Okay, so after some time I finally did it.
What I had to do is:
1) Make a 2-step verification on my gmail account, which you can enable here: https://myaccount.google.com/security
2) Create an app-specific password here: https://support.google.com/accounts/answer/185833
It is a string with a format of 16 small case letters. They appear separated in groups of 4 but it's all in the same string. All you have to do is add this app password in the password field inside the block.
...
:user_name => 'my#gmail.com',
:password => 'abcdefghijklmnop',
...
All other settings worked without change.
config.read_encrypted_secrets = true
config.action_mailer.default_url_options = { :host =>
"domain.com" }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "username",
:password => "password",
:enable_starttls_auto => true
}
Try this config.

Sendgrid set up on Rails 4

I have a rails 4 app. I set up ActionMailer and I can send order confirmation emails via localhost and gmail.
I installed Sendgrid on Heroku and followed the set up instructions. I get a Net::SMTPSyntaxError (501 Syntax error
my environment.rb (i have sendgrid user/pwd in application.yml)
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
}
in production.rb - the only actionamailer setting i have is this. I have this as a placeholder to put the real domain in later. I'm currently using herokuapp.com.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
in my orders_controller within the order create method, I call the below.
AutoNotifier.orderconf_email(current_user, #order).deliver
auto_notifier.rb
class AutoNotifier < ActionMailer::Base
default from: "Test Email"
def orderconf_email(current_user, order)
#buyer = current_user
#order = order
mail(to: #buyer.email, subject: 'Thank you for your order.')
end
end
What am I missing? It works on localhost with gmail so I'm missing something in the sendgrid settings or in the default_url in production.rb file.
For posterity, here's a working setup for external SMTP in Rails on Heroku:
#config/environments/production.rb
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
:authentication => 'login',
:domain => 'yourdomain.com', # your domain to identify your server when connecting
}
Change default from: "Test Email" to valid email address, even example#example.com.
I would just like to point out, this is for sending emails via SMTP. While this method is totally ok, you should also consider sending via the API.
To do this, you need to specify an interceptor. Luckily, there's a Gem that helps with that. Here's a good article showing how to use it.
https://rubyplus.com/articles/561-Sending-Emails-using-SendGrid-API-in-Rails-4-1
It took us a long time to resolve the issue when we tried to deploy the SMTP relay on heroku. It worked perfectly fine on local but when pushed we received socket errors and time out issues. Eventually got it working.
Important note: Make sure not to use starttls_auto and SSL/or TLS this causes open SSL issue.

What is wrong with my Rails SMTP config?

I was given this smtp server address: klee.cdlib.org (behind our firewall and does not need password/login) and my own local address where my dev application is running is http://128.48.204.195:3000
Here are my current configurations in the development.rb file:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "klee.cdlib.org",
:port => 587,
:domain => 'klee.cdlib.org', #'http://128.48.204.195:3000',
# :user_name => '',
# :password => '',
:authentication => 'plain',
:enable_starttls_auto => true }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
This gives an error:
Net::SMTPFatalError (550 5.7.1 <my#email.com>... Relaying denied
I am also not sure about the difference between domain and address fields. What should be in which? :) And what else am I possibly doing wrong to get this error?
Thanks,
Alex
Your rails setup seems ok. Denied relaying is probably raised because the mail server actually does not allow relaying any mails for mails from 128.48.204.195. It's possible that the mail server configuration is not configured for that or perhaps it's configured to relay mails from klee.cdlib.org which has no reverse DNS entry.

Sending mail with Rails 3 in development environment

I'm sure this has been asked a million times before but I can't find anything that works for me so I'm asking again!
I just need a way of sending emails using ActionMailer in rails 3. I have followed numerous tutorials including the Railscasts tutorial on the new ActionMailer and I can see the mails being generated but I don't receive them.
I have tried a bunch of different ways but they generally amount to configuring the following settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
I have tried the above code (with valid gmail details of course) in my config/environment.rb, config/environments/development.rb and currently have it in its own initialiser config/initialisers/setup_mail.rb
I have also tried with a few different smtp servers including Gmail and Sendgrid, adjusting the smtp settings accordingly but still nothing. I can see the mail in the terminal and the development log and that's it.
Does anyone know of any other gotcha's that I may have missed that need to be setup for ActionMailer to work? Failing that is there a way of getting more information about why the mails aren't being sent? I have
config.action_mailer.raise_delivery_errors = true
set in my config/development.rb but the development log still just shows the same as I see in the terminal.
For what it's worth, I am developing on a Ubuntu 10.04 laptop just in case there's any specific setup needed for that.
Many thanks
Well I have resolved the issue, but quite why this works and the other methods did not, I don't know.
The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following
if Rails.env != 'test'
email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end
I then added config/email.yml containing the details of the dev and production email accounts
development:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy
:enable_starttls_auto: true
production:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy
:enable_starttls_auto: true
Like I say, no idea why, but this seemed to do the trick. Thanks all for the pointers
I have the following in config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
The actual mail-configuration, config.actionmailer.* i have placed in config\application.rb.
Hope this helps :)
Try using 'sendmail' instead of 'smtp'.
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
Three things.
First, the port is an integer and does not need quotes, as in your first example. (But I think a string should still work.)
Second, don't forget to restart your server each time you modify this (or any) initializer file. This could explain why you didn't see an error after adding:
config.action_mailer.raise_delivery_errors = true
Without having that error message, it's hard to determine why the mail wasn't going but now is. One possiblity is your use of double quotes around the password. If you were using a strong password and had a token in your password that wasn't escaped it could have been reinterpreted. (i.e. "P#ssw\0rd" would become P#ssrd). For just this reason, I always use single quotes in my code unless I specifically need the syntactic sugar.
Lastly, enable_starttls_auto: true is the default and unnecessary.
ActionMailer::Base.delivery_method = :sendmail
and
config.action_mailer.perform_deliveries = true
were the two necessary steps that got me over this issue
Just put all config to:
config/environments/development.rb
I mean
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
and
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
It worked for me.
In addition to, your gmail username does not alias.
Ref: https://support.google.com/mail/answer/12096?hl=en
My two pennies worth:
I had those exact same symptoms with Rails 5.1: Nothing happened, the settings in my development.rb file were utterly ignored...
Then I remembered to restart the machine! (which solved magically the issue)
This had been pointed out by a couple of previous comments.
The issue is tricky however because you do not expect this behavior. In my view, the default comments in development.rb are, in this respect, misleading:
# 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*.

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.

Resources