Skip to content

Instantly share code, notes, and snippets.

@lzell
Created July 9, 2009 02:35
Show Gist options
  • Save lzell/143380 to your computer and use it in GitHub Desktop.
Save lzell/143380 to your computer and use it in GitHub Desktop.
Send mail from your gmail account
require 'net/smtp'
require 'rubygems'
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
class GMailer
SMTP_ADDR = 'smtp.gmail.com'
def initialize(opts = {})
unless opts[:login] && opts[:password]
raise ArgumentError.new("provide login and password")
end
@login = opts[:login]
@password = opts[:password]
@domain = @login[/@(.+)$/,1]
end
def deliver(opts = {})
tostr = opts[:to].is_a?(Array) ? opts[:to].join(', ') : opts[:to]
msg = ["FROM: #{@login}",
"TO: #{tostr}",
"SUBJECT: #{opts[:subject]}"]
msgstr = msg.join("\n") << "\n\n#{opts[:body]}"
smtp_args = [SMTP_ADDR, 25, @domain, @login, @password, :login]
Net::SMTP.start(*smtp_args) do |smtp|
status = smtp.send_message msgstr, @login, opts[:to]
puts status
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment