Skip to content

Instantly share code, notes, and snippets.

@jjulian
Created October 25, 2012 19:32
Show Gist options
  • Save jjulian/3954895 to your computer and use it in GitHub Desktop.
Save jjulian/3954895 to your computer and use it in GitHub Desktop.
Send an email using Ruby's standard lib.
require 'net/smtp'
host = 'smtp.gmail.com'
port = 587
from = '[email protected]'
password = 'secret'
just_test = false
to = '[email protected]'
msgstr = <<END_OF_MESSAGE
From: #{from}
To: #{to}
Subject: testing Net::SMTP
Message body here.
END_OF_MESSAGE
smtp = Net::SMTP.new(host, port)
smtp.set_debug_output $stderr
smtp.enable_starttls_auto
if just_test
begin
smtp.start('localhost', from, password, :login)
smtp.finish
puts "TEST OK"
rescue Net::SMTPAuthenticationError
puts "TEST FAILED"
end
else
smtp.start('localhost', from, password, :login) do |smtp|
smtp.send_message msgstr, from, to
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment