Unable to send email via SMTP over SSL using cram_md5 authentication - ruby-on-rails

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

Related

Net::SMTPAuthenticationError (504 5.3.3 AUTH mechanism PLAIN not available

I am trying to send emails from my website by using aplus.net
I am getting this error
Net::SMTPAuthenticationError (504 5.3.3 AUTH mechanism PLAIN not available
Here is my configiration
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "mail.aplus.net",
:port => 1025,
:domain => 'www.limoexotic.com',
:user_name => 'booking#limoexotic.com',
:password => 'xxxx',
:authentication => :plain,
:enable_starttls_auto => true
}
In cases like this, you should check with telnet to see which authentication mechanisms are available by the mail server. In this case, mail.aplus.net only allows LOGIN authentication (See AUTH LOGIN below), so changing :plain to :login should make it work.
> $ telnet mail.aplus.net 1025
Trying 64.29.151.235...
Connected to mail.aplus.net.
Escape character is '^]'.
220 mail42c40.carrierzone.com ESMTP Sendmail 8.14.9/8.14.9; Tue, 7 Jul 2020 05:04:56 +0000
EHLO mail.aplus.net
250-mail42c40.carrierzone.com Hello [x.x.x.x], pleased to meet you
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-SIZE 52428800
250-DSN
250-AUTH LOGIN
250-STARTTLS
250-DELIVERBY
250 HELP

What does :tls => true do in SMTP settings in Rails 5? [duplicate]

This question already has answers here:
How do I set the SSL protocol needed for ActionMailer to use a TLS connection?
(2 answers)
Closed 2 years ago.
I'm using Sendgrid on a Rails 5.2 application and was getting a
Net::ReadTimeout error when trying to send an email. The post here
https://github.com/mikel/mail/issues/639#issuecomment-29016055 suggested adding :tls => true to the SMTP settings. That worked, but it seems like an old solution and I'd like to understand what it's doing and why it worked.
This is my SMTP setup that gave the Net::ReadTimeout error:
ActionMailer::Base.smtp_settings = {
:user_name => 'username',
:password => 'password',
:domain => 'mydomain.com',
:address => 'smtp.sendgrid.net',
:port => 465,
:authentication => :plain,
:enable_starttls_auto => true
}
This is the update that's working.
ActionMailer::Base.smtp_settings = {
:user_name => 'username',
:password => 'password',
:domain => 'mydomain.com',
:address => 'smtp.sendgrid.net',
:port => 465,
:authentication => :plain,
:enable_starttls_auto => true,
# this line added
:tls => true
}
Email is effectively a plaintext communication sent from email clients to receiving email servers or from one server to another. This design limitation leaves the content of a message in transit open for anyone to eavesdrop; from a wireless hotspot at the airport or coffee shop to your ISP and internet backbone providers that carry your messages throughout the world.
Transport Layer Security (TLS) helps solve this issue by offering encryption technology for your message while it is “in transit” from one secure email server to another. That is, TLS helps prevent eavesdropping on email as it is carried between email servers that have enabled TLS protections for email. Just as TLS can be used to secure web communications (HTTPS), it can secure email transport. In both applications, TLS has similar strengths and weaknesses. To maximize the content security and privacy, TLS is required between all the servers that handle the message including hops between internal and external servers.
Key features of TLS includes:
Encrypted messages: TLS uses Public Key Infrastructure (PKI) to encrypt messages from mail server to mail server. This encryption makes it more difficult for hackers to intercept and read messages.
Authentication: TLS supports the use of digital certificates to authenticate the receiving servers. Authentication of sending servers is optional. This process verifies that the receivers (or senders) are who they say they are, which helps to prevent spoofing.
For reference

SMTP connection timing out

I'm pulling my hair out trying to debug an 'execution expired' error while sending email through Rails' ActionMailer, and having no luck. I don't think that it's a Rails problem as the connection also times out when executing telnet smtp.gmail.com 587 from the terminal as well.
The output when executing the telnet command is:
Trying 2607:f8b0:400d:c07::6c...
telnet: connect to address 2607:f8b0:400d:c07::6c: Operation timed out
Trying 74.125.192.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP z32sm7598799qtz.0 - gsmtp
My settings are in config/development.rb as follows:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true
}
The stack trace is as follows:
Completed 500 Internal Server Error in 30095ms (ActiveRecord: 1.0ms)
Net::OpenTimeout - execution expired:
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/timeout.rb:114:in `timeout'
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/smtp.rb:550:in `do_start'
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/smtp.rb:520:in `start'
mail (2.6.4) lib/mail/network/delivery_methods/smtp.rb:113:in `deliver!'
mail (2.6.4) lib/mail/message.rb:2149:in `do_delivery'
mail (2.6.4) lib/mail/message.rb:237:in `block in deliver'
...
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
I have also tried turning on "Allow access for less secure apps" in Gmail, and have tried my personal gmail address as well as a custom domain Google Apps domain. I initially tried it with only the custom domain Google Apps address and thought that maybe Namecheap blocks SMTP connections the same way that I've read that Digital Ocean does, but the problem persisting with a simple #gmail address seems to rule that out.
When I was going down that path, I also tried adding:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
to /etc/sysctl.conf, but to no avail.
My thinking is now that maybe it's just my ISP blocking SMTP connections and if I were to deploy this to production that maybe it would just work? But I really don't know.
Thanks in advance.

Microsoft Exchange Server - 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

ActionMailer and Exchange

I successfully send Mails via SMTP using my Rails App and my Postfix Server. Now I need to move to an Exchange: Microsoft ESMTP MAIL Service, Version: 6.0.3790.3959 that has POP3 and SMTP support enabled.
I use actionmailer 1.2.5 and am not able to successfully login to the server while trying to send a mail.
In case I use Mail.app sending and recieving works fine as long as I change the authentication schema to "Password". Checking the server looks like so:
READ Nov 18 10:37:00.509 [kCFStreamSocketSecurityLevelNone] -- host:mail.my-mail-server-domain.com -- port:25 -- socket:0x11895cf20 -- thread:0x11b036a10
250-mail.my-mail-server-domain.com Hello [xxx.xxx.xxx.xxx]
250-TURN
250-SIZE
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-X-EXPS GSSAPI NTLM LOGIN
250-X-EXPS=LOGIN
250-AUTH GSSAPI NTLM LOGIN
250-AUTH=LOGIN
250-X-LINK2STATE
250-XEXCH50
250 OK
WROTE Nov 18 10:37:00.852 [kCFStreamSocketSecurityLevelNone] -- host:mail.my-mail-server-domain.com -- port:25 -- socket:0x11895cf20 -- thread:0x11b036a10
AUTH LOGIN
READ Nov 18 10:37:01.848 [kCFStreamSocketSecurityLevelNone] -- host:mail.my-mail-server-domain.com -- port:25 -- socket:0x11895cf20 -- thread:0x11b036a10
235 2.7.0 Authentication successful.
So authentication method :login seems to be properly supported. Now when it comes to my configuration for actionmailer it looks like so:
ActionMailer::Base.server_settings = {
:address => "mail.my-mail-server-domain.com",
:port => 25,
:domain => "my-mail-server-domain.com",
:authentication => :login,
:user_name => "myusername",
:password => "mypassword"
}
And I get authentication errors over and over. I also tried to change
:user_name => "my-mail-server-domain.com\myusername"
:user_name => "my-mail-server-domain.com\\myusername"
:user_name => "myusername/my-mail-server-domain.com"
:user_name => "myusername#my-mail-server-domain.com"
but nothing works. Can anyone help me?
Regards.
Jason
i think you need to add
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:domain => "my-mail-server-domain.com",
:address => "mail.my-mail-server-domain.com",
:port => 25
:authentication => :login ,
:user_name => 'myusername',
:password => 'mypassword',
}
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
or try changing your port. usually port 25/26 is blocked to send emails, and some email providers are refusing to receive email from port 25 that uses localhost smtp.
Or maybe your internet provider is blocking port 25.
if it still doesn't work you could write the errors here.

Resources