1
votes

I have this code on my production.rb and development.rb.

  config.active_record.dump_schema_after_migration = false
  config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = { host: 'mydomain.com' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:       'smtp.office365.com',
  port:            '587',
  authentication: 'login',
  domain:        'mydomain.com',
  user_name:   "[email protected]",
  password:      "password",
  enable_starttls_auto: true,
  openssl_verify_mode: 'none'
 }

I am trying to send mail from my office 365 mail account using action mailer. This sends the mail perfectly fine on localhost. But it raises the following error during production. I am using digitalocean server.

App 7718 stdout: [7bd99d32-f3c4-49c3-abfd-ff19fa7ddb8a] Completed 500 Internal Server Error in 8800ms (ActiveRecord: 1.6ms) App 7718 stdout: [7bd99d32-f3c4-49c3-abfd-ff19fa7ddb8a]
App 7718 stdout: [7bd99d32-f3c4-49c3-abfd-ff19fa7ddb8a] Net::SMTPAuthenticationError (535 5.7.3 Authentication unsuccessful [CO2PR04CA0117.namprd04.prod.outlook.com]

I have searched everywhere on Google but most of them have solution for gmail not office. So, I couldn't find the exact problem.

1

1 Answers

1
votes
ActionMailer::Base.smtp_settings = {
  address: ENV['SMTP_ADDRESS'],
  port: ENV['SMTP_PORT'],
  domain: ENV['SMTP_DOMAIN'],
  authentication: 'login',
  enable_starttls_auto: true,
  user_name: ENV['SMTP_USERNAME'],
  password: ENV['SMTP_PASSWORD']
}

I already have a office 365 account working with the above settings.

The other thing that got me was you need to send from the actual account you're using to log in with. For example, don't send email from [email protected] and use [email protected] as your SMTP_USERNAME.

More info here: https://www.brownwebdesign.com/blog/connecting-rails-to-microsoft-exchange-smtp-email-server