Class: SendWithUsMailer::Base

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

Overview

Mailer models inherit from SendWithUsMailer::Base. A mailer model defines methods used to generate an email message. In these methods, you can assign variables to be sent to the Send With Us service and options on the mail itself such as the :from address.

class Notifier < SendWithUsMailer::Base
  default from: '[email protected]'

  def welcome(recipient)
    assign(:account, recipient)
    mail(email_id: 'ID-CODE-FROM-SEND-WITH-US', to: recipient.email)
  end
end

Within the mailer method, you have access to the following methods:

  • assign - Allows you to assign key-value pairs that will be data payload used by SendWithUs to interpolate the content.

  • mail - Allows you to specify email to be sent.

The mail method is used to set the header parameters.

Sending mail

Once a mailer action is defined, you can deliver your message or create it and save it for delivery later:

Notifier.welcome(david).deliver # sends the email
mail = Notifier.welcome(david)  # => a SendWithUsMailer::MailParams object
mail.deliver                    # sends the email

You never instantiate your mailer class. Rather, you just call the method you defined on the class itself.

Default Hash

SendWithUsMailer allows you to specify default values inside the class definition:

class Notifier < SendWithUsMailer::Base
  default from: '[email protected]'
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, *args) ⇒ Base

Instantiate a new mailer object. If method_name is not nil, the mailer will be initialized according to the named method.



87
88
89
90
# File 'lib/send_with_us_mailer/base.rb', line 87

def initialize(method_name, *args)
  @message = MailParams.new
  self.send(method_name, *args)
end

Instance Attribute Details

#messageObject (readonly)

———————– Instance methods —————————-



83
84
85
# File 'lib/send_with_us_mailer/base.rb', line 83

def message
  @message
end

Class Method Details

.default(value = nil) ⇒ Object

Set default values for any of the parameters passed to the #mail method. For example:

class Notifier < SendWithUsMailer::Base
  default  from: '[email protected]',
           reply_to: '[email protected]'
end


59
60
61
# File 'lib/send_with_us_mailer/base.rb', line 59

def default(value = nil)
  @defaults = defaults.merge(value)
end

.defaultsObject



48
49
50
# File 'lib/send_with_us_mailer/base.rb', line 48

def defaults
  @defaults || {}
end

.mailer_methodsObject

Return the mailer methods that are defined in any instance of SendWithUsMailer::Base. There should not be any reason to call this directly.



66
67
68
# File 'lib/send_with_us_mailer/base.rb', line 66

def mailer_methods
  public_instance_methods - superclass.public_instance_methods
end

.method_missing(method_name, *args) ⇒ Object

We use ::method_missing to delegate mailer methods to a new instance and return the SendWithUsMailer::MailParams object.



73
74
75
76
77
78
79
# File 'lib/send_with_us_mailer/base.rb', line 73

def method_missing(method_name, *args)
  if mailer_methods.include?(method_name.to_sym)
    new(method_name, *args).message
  else
    super
  end
end

Instance Method Details

#assign(key, value) ⇒ Object

Assign variables that will be sent in the payload to Send With Us.

For example:

class Notifier < ActionMailer::Base
  def welcome

    assign :login_url, "http://thefastguys.example.com"
    assign :user, {name: "Dave Lokhorst", role: "admin"}
    assign :team, {name: "The Fast Guys", captain: "Joe"}

    mail(email_id: 'EMAIL_ID from Send With Us', to: '[email protected]')
  end
end

makes the following parameters accessible to the Send With Us email:

{{ login_url }} => "http://thefastguys.example.com"
{{ user.name }} => "Dave Lokhorst"
{{ user.role }} => "admin"
{{ team.name }} => "The Fast Guys"
{{ team.captain }} => "Joe"


138
139
140
# File 'lib/send_with_us_mailer/base.rb', line 138

def assign(key, value)
  @message.assign(key, value)
end

#mail(params = {}) ⇒ Object

The main method that creates the message parameters. The method accepts a headers hash. This hash allows you to specify the certain headers in an email message, these are:

  • :email_id - The unique code associated with the SendWithUs specific email.

  • :to - Who the message is destined for. Must be a valid email address.

  • :from - Who the message is from. Must be a valid email address.

  • :reply_to - Who to set the Reply-To header of the email to.

You can set default values for any of the above headers by using the ::default class method.

For example:

class Notifier < ActionMailer::Base
  default from: '[email protected]'

  def welcome
    mail(email_id: 'EMAIL_ID from Send With Us', to: '[email protected]')
  end
end


113
114
115
# File 'lib/send_with_us_mailer/base.rb', line 113

def mail(params = {})
  @message.merge!(self.class.defaults.merge(params))
end