Created
July 9, 2009 02:35
-
-
Save lzell/143380 to your computer and use it in GitHub Desktop.
Send mail from your gmail account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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