Class: Emailer::SmtpFacade

Inherits:
Object
  • Object
show all
Defined in:
lib/emailer/smtp_facade.rb

Direct Known Subclasses

AuthSmtpFacade, MockSmtpFacade

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

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

.defaultObject

Returns the value of attribute default.



11
12
13
# File 'lib/emailer/smtp_facade.rb', line 11

def default
  @default
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/emailer/smtp_facade.rb', line 13

def error
  @error
end

#offending_mailObject (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

#settingsObject (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_instanceObject



33
34
35
# File 'lib/emailer/smtp_facade.rb', line 33

def get_net_smtp_instance
  Net::SMTP.new settings[:host], settings[:port]
end

#openObject



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(options)
  send_mail(options.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(options)
  raise ConnectionNotOpenError unless @open
  
  content_type_args = options[:content_type].split('/') << { 'charset' => options[:encoding] }
  
  mail = TMail::Mail.new
  mail.set_content_type *content_type_args
  mail.to = options[:to]
  mail.from = options[:from]
  mail.subject = options[:subject]
  mail.body = options[: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(options)
  send_mail(options.merge(:content_type => 'text/plain', :encoding => 'utf-8'))
end