I upgraded my Rails app to 5.0.6 from 4.2.5. Now emails are not being sent by the Mail gem. I don't seem to be getting errors. I updated the Mail gem to the latest version with no luck. I am not sure what else to try.
I am running:
Rails 5.0.6
Ruby 2.3.0
Mail 2.7.0
controllers/send_email.rb
class SendEmail < ApplicationController
def initialize(to_address, from_address, email_pass, subject, body)
begin
options = {
:address => 'abc.prod.1234.secureserver.net',
:port => '465',
:domain => 'mydomain.com',
:user_name => from_address,
:password => email_pass,
:authentication => :login,
:ssl => true,
:openssl_verify_mode => 'none'
}
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
to to_address
from from_address
subject subject
body body
end
puts("\nSent message. From: #{from_address} To: #{to_address} \nMessage body: \n#{body}")
return true
rescue Exception => e
puts e.to_s
return false
end
end
end
Update:
I tried sending an email with Action Mailer as well. It says it sent in the console but it never gets delivered. I am using similar settings for Action Mailer as I am for the Mail gem.
config/environments/development.rb
config.action_mailer.smtp_settings = {
:address => 'abc.prod.1234.secureserver.net',
:port => '465',
:domain => 'mydomain.com',
:user_name => ENV['default_username'],
:password => ENV['default_password'],
:authentication => :login,
:ssl => true,
:openssl_verify_mode => 'none'
}
I am running this from the console:
EmailMailer.sample_email(UserEmail.first)
Console output:
Rendering email_mailer/sample_email.html.erb within layouts/mailer
Rendered email_mailer/sample_email.html.erb within layouts/mailer (0.1ms)
Rendering email_mailer/sample_email.text.erb within layouts/mailer
Rendered email_mailer/sample_email.text.erb within layouts/mailer (0.0ms)
EmailMailer#sample_email: processed outbound mail in 9.4ms
Solution:
I have some code that sends an email when there is an unknown error that I need to look at. When I updated Rails that code got caught in a loop that sent a bunch of emails really fast. That caused my server provider to mark the email account as a spam account. I am not sure why my original code was not showing any errors but when I ran EmailMailer.sample_email(UserEmail.first).deliver_now
it gave me an error message that helped me track it down.
Well, wild guessing here but try this:
EmailMailer.sample_email(UserEmail.first).deliver_now
If you had Rails version below 4.2.1 it was not necessary to call deliver on the mail object for it to be delivered. From that point on you can operate on the mail object before delivering it, which can be now using
.deliver_now
or later, using .deliver_later
, which goes along with the use of ActiveJob or other queueing library.