Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/smtp'
- require 'erb'
- module Pixelcop
- module Net
- class EmailAddr
- attr_accessor :email, :name
- def initialize(email, name = nil)
- @email = email
- @name = name
- end
- def to_s
- if name.nil? then
- return @email
- end
- return "%s <%s>" % [@name, @email]
- end
- end
- class SMTP
- attr_accessor :server, :port, :user, :pass, :auth_type
- def initialize(server, user = nil, pass = nil, port = 25, auth_type = :login)
- @server = server
- @port = port
- @user = user
- @pass = pass
- @auth_type = auth_type
- end
- # to and cc can be either Strings or Arrays
- def send(to, subject, body, cc = nil, from = nil)
- hostname = `hostname`.strip
- from = "ruby script <#{@user}>" if from.nil?
- if not to.kind_of? Array then
- to = [ to ]
- end
- if not cc.kind_of? Array then
- cc = [ cc ]
- end
- template = ERB.new <<-EOM
- From: <%= from %>
- To: <%= to.join(', ') %>
- Subject: <%= subject %>
- <%= body %>
- EOM
- msg = template.result(binding)
- #puts msg
- smtp = ::Net::SMTP.new(@server, @port)
- #smtp.set_debug_output $stderr
- if @user.nil? then
- smtp.start(hostname)
- else
- smtp.start(hostname, @user, @pass, @auth_type)
- end
- to.each { |to_addr|
- smtp.send_message(msg, from, to_addr)
- }
- smtp.finish
- end
- end
- end
- end
- # example
- smtp = Pixelcop::Net::SMTP.new('mail.operative.com')
- smtp.send("dpeterka@operative.com", "yo", "this is my test arrrrrr")
Add Comment
Please, Sign In to add comment