Class: Talktome::Strategy::Email

Inherits:
Talktome::Strategy show all
Defined in:
lib/talktome/strategy/email.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &defaulter) ⇒ Email

Returns a new instance of Email.



6
7
8
9
# File 'lib/talktome/strategy/email.rb', line 6

def initialize(options = {}, &defaulter)
  @options = options
  @defaulter = defaulter
end

Instance Method Details

#send_message(message, user) {|mail| ... } ⇒ Object

Yields:

  • (mail)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/talktome/strategy/email.rb', line 11

def send_message(message, user)
  # Take a base email, with all info coming from the environment (if set)
  mail = base_email

  # Override environment defaults with template behavior, for flexibility
  [
    :to,
    :reply_to,
    :in_reply_to,
    :attachments,
    :subject
  ].each do |which|
    if arg = message.[which.to_s]
      mail.send(:"#{which}=", arg)
    end
  end

  # If the user is actually known from source code behavior, override the
  # `mail.to` to send the email to that particular person.
  mail.to = user[:email] if user.has_key?(:email)

  case message.extension
  when 'md', 'html', 'htm'
    mail.text_part do
      content_type 'text/plain; charset=UTF-8'
      body message.to_text
    end
    mail.html_part do
      content_type 'text/html; charset=UTF-8'
      body message.to_html
    end
  when 'text'
    mail.body message.to_text
  else
    raise "Unsupported extension `#{message.extension}`"
  end

  yield(mail) if block_given?

  mail.deliver!
end