/usr/lib/ruby/1.8/ramaze/contrib/email.rb is in libramaze-ruby1.8 2010.06.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # EmailHelper can be used as a simple way to send basic e-mails from your app.
#
# Usage:
#
# require 'ramaze/contrib/email'
#
# # Set the required traits:
# Ramaze::EmailHelper.trait :smtp_server => 'smtp.your-isp.com',
# :smtp_helo_domain => "originating-server.com",
# :smtp_username => 'username',
# :smtp_password => 'password',
# :sender_address => 'no-reply@your-domain.com'
#
# # Optionally, set some other traits:
# Ramaze::EmailHelper.trait :smtp_auth_type => :login,
# :bcc_addresses => [ 'admin@your-domain.com' ],
# :sender_full => 'MailBot <no-reply@your-domain.com>',
# :id_generator => lambda { "<#{Time.now.to_i}@your-domain.com>" },
# :subject_prefix => "[SiteName]"
#
# To send an e-mail:
#
# Ramaze::EmailHelper.send(
# "foo@foobarmail.com",
# "Your fooness",
# "Hey, you are very fooey!"
# )
require 'net/smtp'
module Ramaze
class EmailHelper
include Innate::Traited
# Required to be set
trait :smtp_server => 'smtp.your-isp.com'
trait :smtp_helo_domain => 'your.helo.domain.com'
trait :smtp_username => 'no-username-set'
trait :smtp_password => ''
trait :sender_address => 'no-reply@your-domain.com'
# Optionally set
trait :smtp_port => 25
trait :smtp_auth_type => :login
trait :bcc_addresses => []
trait :sender_full => nil
trait :id_generator => lambda { "<" + Time.now.to_i.to_s + "@" + trait[ :smtp_helo_domain ] + ">" }
trait :subject_prefix => ""
class << self
def send(recipient, subject, message)
{:recipient => recipient, :subject => subject, :message => message}.each do |k,v|
if v.nil? or v.empty?
raise(ArgumentError, "EmailHelper error: Missing or invalid #{k}: #{v.inspect}")
end
end
sender = trait[:sender_full] || "#{trait[:sender_address]} <#{trait[:sender_address]}>"
subject = [trait[:subject_prefix], subject].join(' ').strip
id = trait[:id_generator].call
email = %{From: #{sender}
To: <#{recipient}>
Date: #{Time.now.rfc2822}
Subject: #{subject}
Message-Id: #{id}
#{message}
}
send_smtp( email, recipient, subject )
end
# the raw mail sending method used by Ramaze::EmailHelper
def send_smtp( email, recipient, subject )
options = trait.values_at(:smtp_server, :smtp_port, :smtp_helo_domain,
:smtp_username, :smtp_password, :smtp_auth_type)
Net::SMTP.start( *options ) do |smtp|
smtp.send_message( email, trait[ :sender_address ], Array[ recipient, *trait[ :bcc_addresses ] ] )
Log.info "E-mail sent to #{recipient} - '#{subject}'"
end
rescue => e
Log.error "Failed to send e-mail to #{recipient}"
Log.error [ e.class.to_s, e.message, *e.backtrace ].join( "\t\n" )
end
end
end
end
|