Class: Ramaze::EmailHelper

Inherits:
Object show all
Includes:
Innate::Traited
Defined in:
lib/ramaze/contrib/email.rb

Class Method Summary collapse

Class Method Details

.send(recipient, subject, message) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ramaze/contrib/email.rb', line 51

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

.send_smtp(email, recipient, subject) ⇒ Object

the raw mail sending method used by Ramaze::EmailHelper



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ramaze/contrib/email.rb', line 74

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