1

I had some trouble integration ActionMailer into my Rails app so I tried to make a very simple example to test it out. I followed the ActionMailer guide to do so.

When the user visits /test it should send an email to them and redirect them to the root. The log below seems to indicate that the message was sent without error but I'm not receiving anything in my inbox.

log when user visits /test

UserMailer#welcome: processed outbound mail in 16.5ms

Sent mail to me@gmail.com (6.5ms)
Date: Fri, 15 Aug 2014 14:38:26 +0800
From: itsupport@mydomain.com
To: me@gmail.com
Message-ID: <53edaae23b766_58de3fe95291a584391d@MacBook-Pro.local.mail>
Subject: Welcome
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

#message content correctly formatted

Redirected to http://localhost:3000/
Completed 302 Found in 29ms (ActiveRecord: 1.2ms)

development.rb

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true 
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address              => 'smtp.gmail.com',
      :port                 => 587,
      :domain               => 'mydomain.com',
      :user_name            => 'itsupport@mydomain.com',
      :password             => 'password',
      :authentication       => 'plain',
      :enable_starttls_auto => true }

usermailer.rb

class UserMailer < ActionMailer::Base
  default from: "itsupport@mydomain.com"

  def welcome(user)
    @user = user
    mail(to: user.email, subject: 'Welcome')
  end
end

lessons controller

def sendemail
    UserMailer.welcome(current_user).deliver
    redirect_to root_path
end

routes.rb

 get 'test' => 'lessons#sendemail'

I've tried

  • Checking Spam
  • Changing the authentication on 'plain' and 'login'
  • Explicitly starting the server in development mode
  • Not specifying the 'from' field
  • Having domain: as both my domain and as gmail.com (different answers use these interchangeably)

The Message ID in the log suggests it's being sent from my personal computer instead of gmail - am I completely wrong or is this an issue?

If you can help me in getting these emails received it'd me much appreciated!

EDIT - I've pushed the app to Heroku to get around the possibility of gmail not liking email sent from localhost as some answers have suggested. My Heroku logs state that mail was sent to me@gmail.com but still nothing shows up in my inbox...

Community
  • 1
  • 1
  • set config.action_mailer.raise_delivery_errors = true in development could help and it may show you error – raykin Aug 15 '14 at 10:28
  • I've got that set to true already, but it doesn't seem to throw up any errors... –  Aug 15 '14 at 10:34
  • try SendGrid plugin on heroku. It's not a good debug way, but if SendGrid can works on heroku production, the problem may exists in your mail config code – raykin Aug 15 '14 at 10:59

3 Answers3

1

I tried making a new fresh app with the SMTP settings to see if it worked, and it did! It made me wonder what was different, and I realised it might be the Devise gem I have on my app. Searching for help with Devise email turned up this answer which lead me to change my code to this:

config/environments/development.rb

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true 
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
   :address => "smtp.gmail.com",
   :port => 587,
   :domain => "gmail.com",
   :authentication => 'plain',
   :user_name => "me@domain.com",
   :password => "password",
   :enable_starttls_auto => true
}

and now the email is sending!

If anyone could elaborate why config.action_mailer wasn't working that'd be great, for my own understanding.

Community
  • 1
  • 1
  • Hmm. I had exactly the same issue. My setup was just like yours, and your solution has gotten mine working. My config worked previously, so Something-has-changed (TM), I'm guessing it's part of an upgrade, although I find no mention of it in the upgrade logs. Many thank for taking the time to document your solution. Worked for me. – Tim Nov 12 '15 at 09:54
1

Maybe you had an ActionMailer SMTP configuration in config/initializers which is overriding your correct SMTP settings.

muhammadn
  • 330
  • 3
  • 18
0

For me the solution was changing authentication from:

ActionMailer::Base.smtp_settings = {authentication: :plain}

to ActionMailer::Base.smtp_settings = {authentication: 'plain'}.

String instead of a symbol. Which is weird because it worked for the last 2 mail features I wrote last week.

Kermit
  • 4,922
  • 4
  • 42
  • 74