Rails actionmailer, gmail works, office 365 does not.
Asked Answered
M

5

6

I've got Actionmailer sending emails using gmail with the following settings:

ActionMailer::Base.smtp_settings = {
   :address => "smtp.gmail.com",
   :port => 587,
   :domain => "gmail.com",
   :user_name => "[email protected]",
   :password => "password",
   :authentication => "plain",
   :enable_starttls_auto => true
}

However, I can't get office 365 mail to work, I have the following settings:

ActionMailer::Base.smtp_settings = {
    :address => "smtp.office365.com",
    :port => 587,
    :domain => "my_domain.com",
    :user_name => "username@my_domain.onmicrosoft.com",
    :password => "password",
    :authentication => :login, 
    :enable_starttls_auto => true
}

If I try and send an email with this client I get:

Net::SMTPFatalError
550 5.7.1 Client does not have permissions to send as this sender
Muscarine answered 1/10, 2014 at 22:7 Comment(2)
Hi, have you been able to solve the problem? I'm having the same problem. Thanks!Juglandaceous
Hi, what I found was this was an internal IT issue, and nothing was wrong with my code - they just hadn't set permissions correctly.Muscarine
M
7

Turns out microsoft requires the same email for both the smtp_settings and the :from field in emailer.rb.

Muscarine answered 1/10, 2014 at 22:12 Comment(2)
any reference to above answer?Linsang
I changed the from in app/mailers/application_mailer.rb, and that did it. And like always, never forget to reset the rails server when testing!Atalya
L
3

I have faced same issue with Gmail and Office365, after scratching head for many hours I find out solution.

If you are using Office365 account for sending emails, it only works if user_name and sender_address and same value. i.e

config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.office365.com',
    port:                 587,
    domain:               'my_domain.pk',
    user_name:            'noreply@my_domain.pk',
    password:             ENV['PASSWORD'],
    authentication:       'login',
    enable_starttls_auto: true
  }

  config.middleware.use ExceptionNotification::Rack,
  email: {
    deliver_with:         :deliver,
    email_prefix:         '[Email Prefix]',
    sender_address:       %{<noreply@my_domain.pk>},
    exception_recipients: %w{[email protected]}
  }

config.action_mailer.perform_deliveries = true

Key Point: So keeping following values same worked for me

user_name: 'noreply@my_domain.pk' 

sender_address: %{<noreply@my_domain.pk>}
Linsang answered 30/9, 2016 at 7:38 Comment(0)
T
0

Read more on sending with SMTP through Office 365 over here: https://technet.microsoft.com/en-us/library/dn554323.aspx

With the authentication method like this you don't need to set :domain.

Turnabout answered 2/2, 2015 at 21:30 Comment(0)
C
0

In devise.rb I set "no-reply@******.com", and I mentioned at production.rb in smtp setting as "noreply@*****.com".

After replace the email id in devise.rb as "no-reply@*******.com" with "noreply@******.com", it's working fine now.

Centaurus answered 25/5, 2016 at 5:43 Comment(0)
D
0

Configure Mailer to use Office 365 (config/environments/development.rb:

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
    address:              'smtp.office365.com',
    port:                 587,
    domain:               ENV.fetch("APP_MAILER_DOMAIN"),    # Your domain
    user_name:            ENV.fetch("APP_MAILER_USERNAME"),  # Your Office 365 email address
    password:             ENV.fetch("APP_MAILER_PASSWORD"),  # Your Office 365 password
    authentication:       'login',
    enable_starttls_auto: true
}

Now you need to disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online:

  1. Sign in to the Microsoft Entra admin center as at least a Security Administrator.
  2. Browse to Identity > Overview > Properties.
  3. Select Manage security defaults.
  4. Set Security defaults to Disabled.
  5. Select Save.

Now the mailer should work for you. NOTE: as you have disabled default security settings, you need to make sure you have your custom security policies in place to protect your organization.

Additional resource to read regarding security settings:

  1. https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission
  2. https://learn.microsoft.com/en-us/entra/fundamentals/security-defaults
Displant answered 25/3, 2024 at 23:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.