Class: LaunchControl::Mailer

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

Overview

To start using Launch Control, you first need to create a contract class, i.e.:

class ThankyouEmail < LaunchControl::MandrillContract

  def template
    'thank-you'
  end

  def validations
    {
      first_name: 'string',
      last_name:  'string'
    }
  end
end

Define global merge vars to integrate with your Mandrill template:

mailer = ThankyouEmail.new
mailer.deliver(to: '[email protected]', subject: 'Bring it home safely', first_name: 'Pastor', last_name: 'Maldonado')
 => true

Now if you try and deliver this without the appropriate content, you’ll be pulled up on it:

mailer.deliver(to: '[email protected]', subject: 'I know what I\'m doing', first_name: 'Kimi')
 => false
mailer.errors
 => {:last_name=>"string required"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_id, options) ⇒ Mailer

Returns a new instance of Mailer.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/launch_control.rb', line 46

def initialize(template_id, options)
  options = options.dup

  raise 'Please configure your Mandrill API key before trying to deliver emails.' if LaunchControl.configuration.mandrill_api_key.nil?

  @template_id = template_id
  @to          = options.delete(:to)
  @cc          = options.delete(:cc)
  @bcc         = options.delete(:bcc)
  @from_name   = options.delete(:from_name)
  @from_email  = options.delete(:from_email)
  @reply_to    = options.delete(:reply_to)
  @subject     = options.delete(:subject)
  @attachments = options.delete(:attachments) || []
  @merge_vars  = options
end

Instance Attribute Details

#bccObject

Returns the value of attribute bcc.



43
44
45
# File 'lib/launch_control.rb', line 43

def bcc
  @bcc
end

#ccObject

Returns the value of attribute cc.



43
44
45
# File 'lib/launch_control.rb', line 43

def cc
  @cc
end

#from_emailObject

Returns the value of attribute from_email.



43
44
45
# File 'lib/launch_control.rb', line 43

def from_email
  @from_email
end

#from_nameObject

Returns the value of attribute from_name.



43
44
45
# File 'lib/launch_control.rb', line 43

def from_name
  @from_name
end

#merge_varsObject

Returns the value of attribute merge_vars.



43
44
45
# File 'lib/launch_control.rb', line 43

def merge_vars
  @merge_vars
end

#reply_toObject

Returns the value of attribute reply_to.



43
44
45
# File 'lib/launch_control.rb', line 43

def reply_to
  @reply_to
end

#statusObject

Returns the value of attribute status.



43
44
45
# File 'lib/launch_control.rb', line 43

def status
  @status
end

#subjectObject

Returns the value of attribute subject.



43
44
45
# File 'lib/launch_control.rb', line 43

def subject
  @subject
end

#template_idObject

Returns the value of attribute template_id.



43
44
45
# File 'lib/launch_control.rb', line 43

def template_id
  @template_id
end

#toObject

Returns the value of attribute to.



43
44
45
# File 'lib/launch_control.rb', line 43

def to
  @to
end

Instance Method Details

#deliverObject



67
68
69
70
71
72
73
# File 'lib/launch_control.rb', line 67

def deliver
  if valid?
    response = mandrill.messages.send_template(@template_id, [], message, false)
    @status = response[0]["status"]
    ["sent", "queued", "scheduled"].include?(@status)
  end
end

#mandrillObject



63
64
65
# File 'lib/launch_control.rb', line 63

def mandrill
  @mandrill ||= Mandrill::API.new(LaunchControl.configuration.mandrill_api_key)
end

#valid?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/launch_control.rb', line 75

def valid?
  !!(@to && @subject && @template_id)
end