Search code examples
ruby-on-railsrubyherokugmail

Heroku logs confirm an email is being sent, but emails do not show up in mail box (production only)


After submitting a form in the development environment, the application sends an email to my email address. In the production environment, Heroku logs are reporting the email is being sent, but when I check the mailbox the email is not there. Here is my configuration:

application.yml

GMAIL_USERNAME: "[email protected]"
GMAIL_PASSWORD: "sevenmississippi"

user_mailer.rb

class UserMailer < ApplicationMailer
  default from: "[email protected]"

  def customer_email(message)
    @message = message
    mail(to: "[email protected]", subject: "Message from your website" )
  end

end

messages_controller.rb

  def create
    @message = Message.new(message_params)
    if @message.save
      UserMailer.customer_email(@message).deliver_now
      redirect_to '/message_sent'
    else
      redirect_to '/'
    end
  end

production.rb

config.action_mailer.default_url_options = { :host => 'stormy-wildwood-29407.herokuapp.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: "gmail.com",
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"]
}

Heroku logs

2016-05-22: UserMailer#customer_email: processed outbound mail in 238.2ms
2016-05-22: Sent mail to [email protected] (158.2ms)
2016-05-22:
2016-05-22: Date: Sun, 22 May 2016 09:43:29 +0000
2016-05-22: From: [email protected]
2016-05-22: To: [email protected]
2016-05-22: Message-ID: <17f4181751_3.mail>
2016-05-22: Subject: Message from your website
2016-05-22: Mime-Version: 1.0
2016-05-22: Content-Type: text/html;
2016-05-22:  charset=UTF-8
2016-05-22: Content-Transfer-Encoding: 7bit

The problem is only in production and does not have a problem being sent through localhost:3000


Solution

  • The problem was in the Heroku's Environment Variables, because after Heroku app is created the environment variables are not setup exactly as in the file application.yml. For example, to setup an username you should run in console this command:

    $ heroku config:add [email protected]
    

    More about this can be found here: Setting Environment Variables on Heroku

    Happy coding! ;)