# Send eMail from Many Different Mail Providers...cheers!
class HaxorMailer
require 'pony'
require 'highline/import'
def populate_mail_password(user_name)
mail_pass = HighLine.new.ask("#{user_name} password: ") {|q| q.echo = "\*" }
return mail_pass.chomp
end
def pony_express(opts={})
from = opts[:from]
to = opts[:to]
cc = opts[:cc]
bcc = opts[:bcc]
subject = opts[:subject]
html_body = opts[:html_body]
txt_body = opts[:txt_body] # If HTML is NOT supported or desired
attachments_hash = {}
opts[:attachments_hash].each do |attachment_name, attachment_path|
attachments_hash[attachment_name] = File.binread(attachment_path)
end
debug = opts[:debug]
address = opts[:address]
port = opts[:port]
user_name = opts[:user_name]
if ! user_name.nil? && opts[:password].nil?
password = self.populate_mail_password(user_name)
else
password = opts[:password]
end
if debug == true
puts "DEBUG ENABLED: from=>#{from.inspect}, to=>#{to.inspect}, cc=>#{cc.inspect}, bcc=>#{bcc.inspect}, subject=>#{subject.inspect}, html_body=>#{html_body.inspect}, txt_body=>#{txt_body.inspect}, attachments=>#{attachments_hash.inspect}, address=>#{address.inspect}, port=>#{port.inspect}, user_name=>#{user_name.inspect}"
end
begin
Pony.mail({
:to => to,
:cc => cc,
:bcc => bcc,
:from => from,
:subject => subject,
:html_body => html_body,
:txt_body => txt_body,
:attachments => attachments_hash,
:via => :smtp,
:via_options => {
:address => address,
:port => port,
:enable_starttls_auto => true,
:user_name => user_name,
:password => password,
:authentication => :plain,
:domain => 'localhost.localdomain'
}
})
rescue => e
puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
end
end
def gmail(opts={})
opts[:address] = 'smtp.gmail.com'
opts[:port] = 587
self.pony_express(opts)
end
def hotmail(opts={})
opts[:address] = 'smtp.live.com'
opts[:port] = 587
self.pony_express(opts)
end
def spoof(opts={})
self.pony_express(opts)
end
def yahoo(opts={})
opts[:address] = 'smtp.mail.yahoo.com'
opts[:port] = 587
self.pony_express(opts)
end
end
Yeeesss! \o/ Here's how you instantiate the class:
require './haxor_mailer'
mail = HaxorMailer.new
# A quick way to use the supported/builtin methods to the class...
puts mail.gmail(:from => 'some.user@gmail.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :user_name => 'jake.hoopes@gmail.com', :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)
puts mail.hotmail(:password=>'my_optional_passed_in_password', :from => 'some.user@hotmail.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :user_name => 'hoopes9@hotmail.com', :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)
puts mail.yahoo(:password=>'my_optional_passed_in_password', :from => 'some.user@yahoo.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :user_name => 'jake.hoopes@yahoo.com', :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)
puts mail.spoof(:from => 'spoof_mail_from@mailrelay.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :address => '127.0.0.1', :port => 25, :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)