tags:

views:

1739

answers:

2

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, '[email protected]', '[email protected]'
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 <[email protected]>
To: you <[email protected]>
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, '[email protected]',  ['[email protected]']
end

EDIT: Works with ruby 1.8.6

+1  A: 

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
+2  A: 

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
What version of Ruby are you using?
Mark Robinson
I'm using ruby 1.8.6
brodie31k