SMTP with Gmail in 1.9.2

Hi guys,

I’ve got a program that has been working fine running 1.8.6 using the
smtp_tls gem to send emails via my gmail account. I’v now removed the
smtp_tls gem because I was under the impression it was included in Ruby
core now.

I’ve upgraded to 1.9.2 and the code I wrote is now failing:

require ‘net/smtp’

def send_email_via_smtp(message, destination_email)
Net::SMTP.start(‘smtp.gmail.com’, 587, ‘localhost.localdomain’,
USERNAME, PASSWORD, ‘plain’) do |smtp|
smtp.send_message(message, EMAIL_ADDR_THAT_SENDS_EM,
destination_email)
end

When I run the app the first line of the above method is causing this
exception to be thrown:

C:/Ruby192/lib/ruby/1.9.1/net/smtp.rb:954:in check_auth_response': 530 5.7.0 Mu st issue a STARTTLS command first. r36sm4454744qcs.39 (Net::SMTPAuthenticationEr ror) from C:/Ruby192/lib/ruby/1.9.1/net/smtp.rb:731:inauth_plain’
from C:/Ruby192/lib/ruby/1.9.1/net/smtp.rb:723:in authenticate' from C:/Ruby192/lib/ruby/1.9.1/net/smtp.rb:566:indo_start’
from C:/Ruby192/lib/ruby/1.9.1/net/smtp.rb:525:in start' from C:/Ruby192/lib/ruby/1.9.1/net/smtp.rb:463:instart’
from C:/Program Files/EmailOMatic/src/emailer.rb:36:in
`send_email_via_s
mtp’

Any thoughts to why this change is breaking my code?

Thanks for your assistance,
Alex

I’ve been digging around in the source code.

I found I need to call smtp.enable_starttls to get the starttls enabled.

Here’s my now working code from above:

def send_email_via_smtp(message, destination_email)
smtp = Net::SMTP.new(‘smtp.gmail.com’, 587)
smtp.enable_starttls
smtp.start(‘localhost.localdomain’, USERNAME, PASSWORD, ‘plain’) do
|connection|
connection.send_message(message, EMAIL_ADDR_THAT_SENDS_EM,
destination_email)
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs