Class: Emailer::SmtpFacade
- Inherits:
-
Object
- Object
- Emailer::SmtpFacade
- Defined in:
- lib/emailer/smtp_facade.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.default ⇒ Object
Returns the value of attribute default.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#offending_mail ⇒ Object
readonly
Returns the value of attribute offending_mail.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #get_net_smtp_instance ⇒ Object
-
#initialize(settings) ⇒ SmtpFacade
constructor
A new instance of SmtpFacade.
- #open ⇒ Object
- #send_html(options) ⇒ Object
-
#send_mail(options) ⇒ Object
Sends a mail to the represented SMTP server.
- #send_text(options) ⇒ Object
Constructor Details
#initialize(settings) ⇒ SmtpFacade
Returns a new instance of SmtpFacade.
15 16 17 18 19 20 21 22 |
# File 'lib/emailer/smtp_facade.rb', line 15 def initialize(settings) @settings = settings @settings.keys.each do |key| raise ArgumentError.new("invalid option, {"+key.to_s+" => "+@settings[key].to_s+"}") unless [ :host, :port, :username, :password, :authentication, :domain ].include?(key) end end |
Class Attribute Details
.default ⇒ Object
Returns the value of attribute default.
11 12 13 |
# File 'lib/emailer/smtp_facade.rb', line 11 def default @default end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
13 14 15 |
# File 'lib/emailer/smtp_facade.rb', line 13 def error @error end |
#offending_mail ⇒ Object (readonly)
Returns the value of attribute offending_mail.
13 14 15 |
# File 'lib/emailer/smtp_facade.rb', line 13 def offending_mail @offending_mail end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
13 14 15 |
# File 'lib/emailer/smtp_facade.rb', line 13 def settings @settings end |
Instance Method Details
#get_net_smtp_instance ⇒ Object
33 34 35 |
# File 'lib/emailer/smtp_facade.rb', line 33 def get_net_smtp_instance Net::SMTP.new settings[:host], settings[:port] end |
#open ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/emailer/smtp_facade.rb', line 24 def open @open = true open_connection yield self ensure @open = false close_connection end |
#send_html(options) ⇒ Object
65 66 67 |
# File 'lib/emailer/smtp_facade.rb', line 65 def send_html() send_mail(.merge(:content_type => 'text/html', :encoding => 'utf-8')) end |
#send_mail(options) ⇒ Object
Sends a mail to the represented SMTP server. Options is expected to include :to, :from, :subject, :body, :content_type and :encoding. Use send_html or send_text to provide defaults for :content_type and :encoding.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/emailer/smtp_facade.rb', line 42 def send_mail() raise ConnectionNotOpenError unless @open content_type_args = [:content_type].split('/') << { 'charset' => [:encoding] } mail = TMail::Mail.new mail.set_content_type *content_type_args mail.to = [:to] mail.from = [:from] mail.subject = [:subject] mail.body = [:body] @connection.sendmail(mail.encoded, mail.from[0], mail.destinations) true rescue ConnectionNotOpenError => e raise e rescue StandardError => e @error = e @offending_mail = mail close_connection false end |
#send_text(options) ⇒ Object
69 70 71 |
# File 'lib/emailer/smtp_facade.rb', line 69 def send_text() send_mail(.merge(:content_type => 'text/plain', :encoding => 'utf-8')) end |