Class: SendWithUsMailer::Base

Inherits:
AbstractController::Base
  • Object
show all
Defined in:
lib/sendwithus_ruby_action_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.



101
102
103
104
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 101

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

Instance Attribute Details

#messageObject (readonly)

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



97
98
99
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 97

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


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

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

.defaultsObject



52
53
54
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 52

def defaults
  @defaults || {}
end

.inherited(heir) ⇒ Object

Inherit defaults from ancestor



68
69
70
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 68

def inherited(heir)
  heir.__send__(:default, 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.



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

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.



82
83
84
85
86
87
88
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 82

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

.respond_to?(method_name, include_all = false) ⇒ Boolean

Add our mailer_methods to the set of methods the mailer responds to.

Returns:

  • (Boolean)


91
92
93
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 91

def respond_to?(method_name, include_all = false)
  mailer_methods.include?(method_name.to_sym) || super
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:

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


154
155
156
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 154

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.

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

  • :recipient_name - Recipient’s name

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

  • :from_name - Who the email is from

  • :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


129
130
131
# File 'lib/sendwithus_ruby_action_mailer/base.rb', line 129

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