Search code examples
ruby-on-railsrubyruby-on-rails-4smtpmailer

Mailer stopped working after switching emails


A few weeks ago I created a mailer object to send an email whenever a new "Thing" was created. It worked without a hitch, but I then commented out the relevant lines and didn't deal with it for a while. Today I tried it again, and nothing happened. I discovered that the email account I was using had been disconnected for some reason, so I tried it with another email account. Still nothing. Does anyone have a guess for why it might not be doing anything, or how I can debug it? I'm sure the username/password/domain are accurate.

config/environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: 'smtp.gmail.com',
  port: 587,
  domain: 'gmail.com',
  user_name: 'username',
  password: 'password',
  authentication: 'plain',
  enable_starttls_auto: true
}

app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: '[email protected]'

  def notify(user)
    @user = user
    mail(to: @user.email,subject: "Notification")
  end
end

app/views/user_mailer/notify.html.erb

<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Heading</h1>
    <p>First paragraph</p>
    <p>Second paragraph</p>
  </body>
</html>

app/controllers/users_controller.rb

def create
  #...
  UserMailer.notify(@user).deliver
end

Solution

  • Try this:

    config.action_mailer.delivery_method = :smtp
    config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
    config.action_mailer.perform_deliveries = true
    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.default :charset => "utf-8"
    config.action_mailer.smtp_settings = {
      address: 'smtp.gmail.com',
      port: 587,
      domain: 'localhost:3000',
      user_name: '[email protected]',
      password: 'password',
      authentication: 'plain',
      enable_starttls_auto: true
    }