class SendWithUsMailer::Base

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: 'no-reply@example.com'

  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:

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: 'system@example.com'
end

Attributes

message[R]

Public Class Methods

default(value = nil) click to toggle source

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

class Notifier < SendWithUsMailer::Base
  default  from: 'no-reply@test.lindsaar.net',
           reply_to: 'bounces@test.lindsaar.net'
end
# File lib/sendwithus_ruby_action_mailer/base.rb, line 63
def default(value = nil)
  @defaults = defaults.merge(value)
end
defaults() click to toggle source
# File lib/sendwithus_ruby_action_mailer/base.rb, line 52
def defaults
  @defaults || {}
end
inherited(heir) click to toggle source

Inherit defaults from ancestor

# File lib/sendwithus_ruby_action_mailer/base.rb, line 68
def inherited(heir)
  heir.__send__(:default, defaults)
end
mailer_methods() click to toggle source

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

# 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) click to toggle source

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

Calls superclass method
# 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
new(method_name, *args) click to toggle source

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

# File lib/sendwithus_ruby_action_mailer/base.rb, line 101
def initialize(method_name, *args)
  @message = MailParams.new
  self.send(method_name, *args)
end
respond_to?(method_name, include_all = false) click to toggle source

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

Calls superclass method
# 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

Public Instance Methods

assign(key, value) click to toggle source

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: 'dave@test.example.net')
  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"
# File lib/sendwithus_ruby_action_mailer/base.rb, line 154
def assign(key, value)
  @message.assign(key, value)
end
mail(params = {}) click to toggle source

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: 'no-reply@test.example.net'

  def welcome
    mail(email_id: 'EMAIL_ID from Send With Us', to: 'dave@test.example.net')
  end
end
# File lib/sendwithus_ruby_action_mailer/base.rb, line 129
def mail(params = {})
  @message.merge!(self.class.defaults.merge(params))
end