Microsoft Exchange Server - Ruby on Rails - ruby-on-rails

My configuration for sending mails in Thunderbird for this exchange server email account is as follows:
server name: pod51003.outlook.com
port: 587
username: my email address
authentication method: normal password
connection security: STARTTLS
I am trying to send email from my rails application, my configuration in my application is as follows:
config.action_mailer.smtp_settings = {
address: "pod51003.outlook.com",
port: 587,
authentication: "plain",
:enable_starttls_auto => 'true',
user_name: 'myemail',
password: 'mypassword'
}
I have tried different authentication methods such as none and login. I have tried the gem ruby-ntlm and set ntlm as the authentication method too but I keep getting the error:
504 5.7.4 Unrecognized authentication type

Try changing the authentication setting to :login

Related

Rails ActionMailer sends email but it does not show up as a sent email in the mailbox used

I am using namecheap to send emails and it uses privateemail.
My setup in ActionMailer is:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'mail.privateemail.com',
port: 587,
domain: 'privateemail.com',
user_name: 'very#cool.com',
password: "very_secret",
authentication: 'plain',
enable_starttls_auto: true
}
As they say here:
https://www.namecheap.com/support/knowledgebase/article.aspx/1179/2175/general-private-email-configuration-for-mail-clients-and-mobile-devices/
Now it sends the emails, but when I login in the web client, it does not show any mails sent in the sent folder.
Why is that ?
SMTP delivery means sending an email and not send an email and store it in sent folder. This has to be done by the client additionally!

Sending emails with pure Ruby

I have problems with sending emails with pure Ruby. Here is how my script looks like:
Mail.defaults do
delivery_method(
:smtp,
address: 'smtp.sendgrid.net',
port: 587,
user_name: 'sendgrid_username',
password: 'sendgrid_password',
authentication: :login,
enable_starttls_auto: false,
openssl_verify_mode: "none"
)
end
Mail.deliver do
from 'Test Email'
to 'user#example.com'
subject 'Here is the image you wanted'
body "Test Email"
end
This script raise the following error:
/Users/mateuszurbanski/.rubies/ruby-2.7.2/lib/ruby/2.7.0/net/smtp.rb:975:in `check_auth_response': 535 Authentication failed: Bad username / password (Net::SMTPAuthenticationError)
I'm using credentials from one of my Ruby on Rails project and they are fine. Any ideas?
Sendgrid recently transitioned to api keys and will reject plain auth, see details here.
Your old credentials while still being valid may not be accepted.
Generate a new api key in sendgrid and use it in place of password. Username will be apikey
Instead of using your SendGrid username and password in this config, use the following,
user_name: 'apikey',
password: '<sendgrid_api_key>',
This works for me when defining the smtp settings for ActionMailer, so it should work here as well.

Net::SMTPAuthenticationError (535 5.7.3 Authentication unsuccessful while sending email using Action Mailer and office 365 email

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: "username#mydomain.com",
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.
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 noreply#brownwebdesign.com and use support#brownwebdesign.com as your SMTP_USERNAME.
More info here: https://www.brownwebdesign.com/blog/connecting-rails-to-microsoft-exchange-smtp-email-server

gmail blocking rails app from sending email

I'm pretty sure I have everythign configured right, but I keep getting 'Net::SMTPAuthenticationError: 535-5.7.8 Username and Password not accepted. Learn more at' error when trying to send mails through my rails app. Also, there is only a blank space after the 'at' in the error message so I don't even know where to look for more information. I am also 100% sure the password and email are correct.
in my config/development.rb (I also have this in application.rb)
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'username#gmail.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true
}
I have allowed less secure apps access on the actual gmail account.
Google blocks all suspicious login attempts. By using app password, I was able to solve the problem:
Enable 2-Step Verification if you haven't done so.
Go to App passwords (Google will ask you to login again to verify here)
Create a new App password: Choose Other (Custom name) under Select app, then enter app's name (any name you want)
Click generate, a popup will be shown with a password. Use that password in your rails configuration instead of your google password.
This is the config that works for me with Google apps for business. Try changing your port to 465
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
tls: true,
address: ENV["EMAIL_URL"],
port: 465,
domain: ENV["EMAIL_DOMAIN"],
user_name: ENV["EMAIL_USER_NAME"],
password: ENV["EMAIL_PASSWORD"],
authentication: 'plain',
enable_starttls_auto: true }

Unable to send email via SMTP over SSL using cram_md5 authentication

I am writing a Ruby script to send email using the 'mail' gem.
These are my SMTP settings on my local machine:
mailer_options:
address: smtp.gmail.com
port: 465
domain: gmail.com
user_name: example#gmail.com
password: example_password
authentication: :cram_md5
enable_starttls_auto: true
ssl: true
When I try to send email with the above SMTP settings, I get the following exception:
/opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:968:in `check_auth_continue': 504 5.7.4 Unrecognized Authentication Type ka3sm12016635pbc.32 - gsmtp (Net::SMTPSyntaxError)from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:758:in `block in auth_cram_md5from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:941:in `critical'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:756:in `auth_cram_md5'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:731:in `authenticate'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:566:in `do_start'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:519:in `start'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:2129:in `do_delivery'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:234:in `deliver'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/mail.rb:140:in `deliver'
I tried searching for this error and found
SASL LOGIN authentication failed: Invalid authentication mechanism on Rails using Postfix and Dovecot on Ubuntu 12.10
but it does not help.
Why are you using MD5? If you're using TLS (SSL) you won't need to do this because the connection itself is encrypted and even a Base64 encoded password is secure.
When you connect to a server it will advertise what authentication types are allowed. In the case of Google Gmail the header looks like:
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
CRAM-MD5 is not supported. All the others are.
Google's probably dropped MD5 because that method provides little in the way of security given how easily cracked MD5 is.
You won't be able to authenticate to Gmail using cram_md5. Here is an example configuration for using Gmail:
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => ENV['GMAIL_SMTP_USER'],
:password => ENV['GMAIL_SMTP_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
end
Source: https://github.com/mikel/mail/wiki/Sending-email-via-Gmail-SMTP

Resources