8

I am trying to send an email from a Ruby program. The smtp server is an Exchange 2007 server that requires me to login to send the email.

#!/usr/bin/ruby

require 'rubygems'
require 'net/smtp'

msgstr = "Test email."

smtp = Net::SMTP.start('smtp.server.com', 587, 
  'mail.server.com', 'username', 'password', :login) do |smtp|
    smtp.send_message msgstr, 'from@server.com', 'to@server.com'
end

When I run this I get the following error.

/usr/lib/ruby/1.8/net/smtp.rb:948:in `check_auth_continue': 
530 5.7.0 Must issue a STARTTLS command first (Net::SMTPAuthenticationError)
    from /usr/lib/ruby/1.8/net/smtp.rb:740:in `auth_login'
    from /usr/lib/ruby/1.8/net/smtp.rb:921:in `critical'
    from /usr/lib/ruby/1.8/net/smtp.rb:739:in `auth_login'
    from /usr/lib/ruby/1.8/net/smtp.rb:725:in `send'
    from /usr/lib/ruby/1.8/net/smtp.rb:725:in `authenticate'
    from /usr/lib/ruby/1.8/net/smtp.rb:566:in `do_start'
    from /usr/lib/ruby/1.8/net/smtp.rb:525:in `start'
    from /usr/lib/ruby/1.8/net/smtp.rb:463:in `start'

I can't find anything in the Ruby api for Net::SMTP referencing TLS or a STARTTLS command.

EDIT: Download smtp-tls.rb, the code doesn't change much but here is what I got to work.

#!/usr/bin/ruby

require 'rubygems'
require 'net/smtp'
require 'smtp-tls'

msgstr = <<MESSAGE_END
From: me <me@test.com>
To: you <you@test.com>
Subject: e-mail test

body of email

MESSAGE_END

smtp = Net::SMTP.start('smtp.test.com', 587, 'test.com', 'username', 'passwd', :login) do |smtp|
    smtp.send_message msgstr, 'me@test.com',  ['you@test.com']
end

EDIT: Works with ruby 1.8.6

Mark
  • 16,772
  • 9
  • 42
  • 55

4 Answers4

14

No gem required in Ruby 1.9.3

You don't need a gem to do this (at least, not for Gmail and Ruby 1.9.3), the documentation is just terrible.

See https://stackoverflow.com/a/3559598/4376

Community
  • 1
  • 1
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
5

I couldn't get it working using your updated example. But the following did work for me.

require 'rubygems'
require 'smtp_tls'
require 'net/smtp'

smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls

smtp.start(Socket.gethostname,username,password,:login) do |server|
   server.send_message msg, from, to
end
brodie31k
  • 571
  • 2
  • 7
  • 12
  • What version of Ruby are you using? – Mark Nov 06 '09 at 13:44
  • I'm in 1.8.7 and get an "unknown method 'enable_starttls' error". I've required smtp_tls and net/smtp. One possible problem is that in my config i have `ActionMailer::Base.delivery_method = :smtp`. Should this perhaps be set to something else eg smtp_tls or something? – Max Williams Jun 06 '11 at 11:32
  • I tried to install smtp_tls but I it said: `smtp_tls requires Ruby version ~> 1.8.6.0.` – Daniel Hernández May 13 '12 at 00:30
  • and `smtp.enable_tls` for straight TLS instead of `smtp.enable_starttls` – Claire Furney Jul 02 '22 at 19:46
1

Net::SMTP doesn't look like it supports startTLS encryption. I did find a project on github that has code for dealing with this, though.

smtp-tls

BaroqueBobcat
  • 10,090
  • 1
  • 31
  • 37
0

This is a solution using the tlsmail gem:

require 'rubygems'
require 'tlsmail'
require 'net/smtp'

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start(server,25,Socket.gethostname,username,password,:login) do |server|
  server.send_message msg, from, to
end
Daniel Hernández
  • 1,279
  • 8
  • 15