Getting "You must send the STARTTLS command first" when trying to send an email

I get an error when trying to use the action_mailer_tls plugin to communicate with Gmail in a Rails application:

 Must issue a STARTTLS command first 

Others seem to be facing the same problem :

The problem is that Gmail requires TLS authentication, but the standard Ruby net / smtp library does not support TLS.

This article recommends the following steps that I have taken:

Of course, there is a useful plugin created by Mark Chung to overcome this barrier. You can find it here and manually add it to your project or you can export it to your plugin directory.

  • $ cd vendor/plugins
  • $ svn export http://code.openrain.com/rails/action_mailer_tls/

In any case, make sure you require 'Smtp_tls'

Now you need to update smtp_settings if you have not already done so.

  • ActionMailer :: Base.smtp_settings = {
  • : address => "smtp.gmail.com",
  • : port => 587,
  • : domain => "domain.com",
  • : user_name => " user@domain.com ",
  • : password => "password",
  • : authentication =>: plain
  • }

Any suggestions for a better solution for communicating with Gmail will be appreciated.

+6
ruby-on-rails gmail
source share
4 answers

I used the solution of Alexander Pomozhov to talk with Gmail from my Rails application. I believe the original article is gone, but someone played the Google cache here .

Library / smtp _tls.rb

 require "openssl" require "net/smtp" Net::SMTP.class_eval do private def do_start(helodomain, user, secret, authtype) raise IOError, 'SMTP session already started' if @started check_auth_args user, secret, authtype if user or secret sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) } @socket = Net::InternetMessageIO.new(sock) @socket.read_timeout = 60 #@read_timeout #@socket.debug_output = STDERR #@debug_output check_response(critical { recv_response() }) do_helo(helodomain) if starttls raise 'openssl library not installed' unless defined?(OpenSSL) ssl = OpenSSL::SSL::SSLSocket.new(sock) ssl.sync_close = true ssl.connect @socket = Net::InternetMessageIO.new(ssl) @socket.read_timeout = 60 #@read_timeout #@socket.debug_output = STDERR #@debug_output do_helo(helodomain) end authenticate user, secret, authtype if user @started = true ensure unless @started # authentication failed, cancel connection. @socket.close if not @started and @socket and not @socket.closed? @socket = nil end end def do_helo(helodomain) begin if @esmtp ehlo helodomain else helo helodomain end rescue Net::ProtocolError if @esmtp @esmtp = false @error_occured = false retry end raise end end def starttls getok('STARTTLS') rescue return false return true end def quit begin getok('QUIT') rescue EOFError, OpenSSL::SSL::SSLError end end end 

config /environment.rb

(add after everything else)

  require "smtp_tls" ActionMailer::Base.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :authentication => :plain, :user_name => " someone@openrain.com ", :password => 'someonesPassword' } 

Use ActionMailer as usual.

+10
source share

With Ruby 1.8.7 and Rails 2.3.4 (although it was there for several releases), I had success without the need for TLS-specific ActionMailer plugins using the option :enable_starttls_auto . An example configuration (from a production environment) is as follows:

 ActionMailer::Base.smtp_settings = { :enable_starttls_auto => true, :address => "smtp.gmail.com", :port => 587, :domain => "domain.com", :authentication => :plain, :user_name => " username@domain ", :password => "secret" } 
+5
source share

I enabled starttls using :enable_starttls_auto => true , but still got the same error. Finally, I was able to solve the problem without making any changes to the code. If you use smtp.gmail.com to send mail, you must first allow less secure applications to use email to send emails. To do this, log in to your account with which you want to send mail, and go to this link and enable access to less secure applications.
Change If you are not allowed to change the settings for less secure applications, you need to contact the owner of the administrator account for this domain to change the settings so that users can use less secure applications.
If you have administrator rights, you can allow users to change their less secure application settings, you can change it from https://admin.google.com/domainname.com/AdminHome#ServiceSettings/notab=1&service=securitysetting&subtab=lesssecureappsaccess
PS: Remember to change the domain name in the link above.

Hope this is a hep!

+1
source share

I run rails 2.3.4 and although I thought (from googling around) you don't need any plugins and only a line is needed

: enable_starttls_auto => true,

In fact, I managed to get it to work when I used the solution of Alexander Pomozhov, which came out on skis (thank you very much guys). Any comments as to why? It would be great, but I'm just happy that it works.

0
source share

All Articles