Class: Useful::SinatraHelpers::Mailer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/useful/sinatra_helpers/mailer/base.rb

Constant Summary collapse

CONFIGS =
[:server, :domain, :port, :reply_to, :username, :password, :authentication, :content_type, :charset]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs = {}) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
27
28
29
# File 'lib/useful/sinatra_helpers/mailer/base.rb', line 20

def initialize(configs={})
  CONFIGS.each do |config|
    instance_variable_set("@#{config}", configs[config])
  end
  @authentication ||= :login
  @reply_to ||= @username
  @content_type ||= 'text/plain'
  @charset ||= 'UTF-8'
  @logger = block_given? ? yield : nil
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/useful/sinatra_helpers/mailer/base.rb', line 18

def logger
  @logger
end

Instance Method Details

#send(settings = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/useful/sinatra_helpers/mailer/base.rb', line 31

def send(settings={})
  check_configs
  [:to, :subject].each do |setting|
    raise Useful::SinatraHelpers::Mailer::SendError, "cannot send, #{setting} not specified." unless settings[setting]
  end
  mail = generate_mail(settings)
  mail.body = yield(mail) if block_given?
  mail.body ||= ''
  Sinatra::Application.environment.to_s == 'production' ? send_mail(mail) : log_mail(mail)
  log(:info, "Sent '#{mail.subject}' to #{mail.to.join(',')}")
  mail
end