Send email from Ruby program with TLS
email, ruby
Solution
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
Problem
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