Class: Ramaze::EmailHelper

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

Class Method Summary collapse

Class Method Details

.send(recipient, subject, message) ⇒ Object



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

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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ramaze/contrib/email.rb', line 70

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
end