Creating a Ruby Gem for SMTP_TLS
UPDATE (1/12/10)
Since this is remarkably one of my most popular posts, I feel compelled to point you towards this Ruby Inside article that will probably help you out a lot more than this post.
As you may know, the Ruby 1.8.* Net::SMTP library does not allow for TLS (SSL) connections. This prevents you from connecting to Gmail, or other mail servers that require a secure connection. Fortunatly, there is a pretty tight bit of code to fix this problem. As far as I can tell, this code originated at a japanese site. This code is all over, if you search for 'SMTP_TLS ruby.'
Great! We have the code! This is actually one of my favorite examples of the open source community. I originally got this code from a guy in Russia, who translated the Japanese page above. Pretty cool. Anyway, I don't really want to paste this code above every script I write that sends out an email. What about a gem!
UPDATE (12/18/07): Well it turns out there already is a gem for this. TLS Mail is not exactly the same as above, but it does the trick. The code in this gem is the code in the Ruby 1.9 source. The only catch is that you have to enable the TLS protocol before you send the mail:
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)Grab the code with "sudo gem install tlsmail"
The RubyGems website has concise, but not overly helpful, documentation about creating gems. It is pretty easy, though. I copied the .gemspec file from the simplist gem I could find, and went from there. I don't know about copy rights or licenses for this code, and this is mostly for internal use, so the gemspec is pretty dumbed down.
Gem::Specification.new do |s|
s.name = %q{SMTP_TLS}
s.version = "1.0.0"
s.date = %q{2007-12-10}
s.summary = %q{SMTP TLS (SSL) extension for Net::SMTP}
s.email = %q{nick@nuclearrooster.com}
s.homepage = %q{google for smtp_tls}
s.description = %q{This adds support for TLS (SSL) connections to SMTP mail servers}
s.has_rdoc = false
s.authors = ["Dunno"]
s.files = ["lib/smtp_tls.rb"]
end
Make sure any ruby files are stuck in a lib directory.
nick-stielaus-computer:~/Desktop/smtp_tls nick$ ls lib/ smtp_tls.1.0.0.gemspec nick-stielaus-computer:~/Desktop/smtp_tls nick$ gem build smtp_tls.1.0.0.gemspec Successfully built RubyGem Name: SMTP_TLS Version: 1.0.0 File: SMTP_TLS-1.0.0.gem nick-stielaus-computer:~/Desktop/smtp_tls nick$ sudo gem install SMTP_TLS Password: Successfully installed SMTP_TLS, version 1.0.0
And then in your scripts
require "net/smtp"
require "rubygems"
require "smtp_tls"
Net::SMTP.start('mail.server.net', 25, 'mail.from.server','usernam', 'password', :login) do |smtp|
hdr = "From: Me <nick@nuclearrooster.com>\n"
hdr += "To: Nick Stielau <nick@nuclearrooster.com>\n"
hdr += "Subject: Subject\n\n"
msg = hdr + "Email Body"
smtp.send_message msg, 'nick@nuclearrooster.com'
end
Now I can install this gem on my app servers, etc, and the scripts are good to go.
