Class: Outbox::Messages::Email

Inherits:
Base
  • Object
show all
Defined in:
lib/outbox/messages/email.rb

Overview

Email messages use the same interface for composing emails as Mail::Message from the mail gem. The only difference is the abstraction of the client interface, which allows you to send the email using whatever client you wish.

email = Outbox::Messages::Email.new do
  to '[email protected]'
  from 'Mikel Lindsaar <[email protected]>'
  subject 'First multipart email sent with Mail'

  text_part do
    body 'This is plain text'
  end

  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<h1>This is HTML</h1>'
  end
end
email.client :mandrill, api_key: '...'
email.deliver

Instance Method Summary collapse

Methods inherited from Base

#deliver

Methods included from Outbox::MessageFields

#[], #[]=, #fields, #fields=, included, #validate_fields

Methods included from Outbox::MessageClients

#client, included

Constructor Details

#initialize(fields = nil, &block) ⇒ Email

Returns a new instance of Email.



42
43
44
45
# File 'lib/outbox/messages/email.rb', line 42

def initialize(fields = nil, &block)
  @message = ::Mail::Message.new
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/outbox/messages/email.rb', line 64

def method_missing(method, *args, &block)
  if @message.respond_to?(method)
    @message.public_send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#audience=(audience) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/outbox/messages/email.rb', line 52

def audience=(audience)
  case audience
  when String, Array
    self.to = audience
  else
    audience = Outbox::Accessor.new(audience)
    self.to = audience[:to]
    self.cc = audience[:cc]
    self.bcc = audience[:bcc]
  end
end

#message_objectObject

Returns the internal Mail::Message instance



48
49
50
# File 'lib/outbox/messages/email.rb', line 48

def message_object
  @message
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/outbox/messages/email.rb', line 72

def respond_to_missing?(method, include_private = false)
  @message.respond_to?(method, include_private) || super
end