Module: Hanami::Mailer::Dsl

Defined in:
gems/gems/hanami-mailer-1.1.0/lib/hanami/mailer/dsl.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/dsl.rb

Overview

Class level DSL

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#bcc(value) ⇒ NilClass #bccString, ...

Sets the bcc (blind carbon copy) for mail messages

It accepts a hardcoded value as a string or array of strings. For dynamic values, you can specify a symbol that represents an instance method.

This value is optional.

When a value is given, it specifies the bcc for the email. When a value is not given, it returns the bcc of the email.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Hardcoded value (String)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to "user@example.com"
  bcc "other.user@example.com"
end

Hardcoded value (Array)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to ["user-1@example.com", "user-2@example.com"]
  bcc ["other.user-1@example.com", "other.user-2@example.com"]
end

Method (Symbol)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to "user@example.com"
  bcc :email_address

  private

  def email_address
    user.email
  end
end

other_user = User.new(name: 'L')
WelcomeMailer.deliver(user: other_user)

Method that returns a collection of recipients

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to "user@example.com"
  bcc :recipients

  private

  def recipients
    users.map(&:email)
  end
end

other_users = [User.new(name: 'L'), User.new(name: 'MG')]
WelcomeMailer.deliver(users: other_users)

Overloads:

  • #bcc(value) ⇒ NilClass

    Sets the bcc

    Parameters:

    • value (String, Array, Symbol)

      the hardcoded value or method name

    Returns:

    • (NilClass)
  • #bccString, ...

    Returns the bcc

    Returns:

    • (String, Array, Symbol)

      the recipient

Since:

  • 0.3.0

def bcc(value = nil)
  if value.nil?
    @bcc
  else
    @bcc = value
  end
end

#cc(value) ⇒ NilClass #ccString, ...

Sets the cc (carbon copy) for mail messages

It accepts a hardcoded value as a string or array of strings. For dynamic values, you can specify a symbol that represents an instance method.

This value is optional.

When a value is given, it specifies the cc for the email. When a value is not given, it returns the cc of the email.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Hardcoded value (String)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to "user@example.com"
  cc "other.user@example.com"
end

Hardcoded value (Array)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to ["user-1@example.com", "user-2@example.com"]
  cc ["other.user-1@example.com", "other.user-2@example.com"]
end

Method (Symbol)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to "user@example.com"
  cc :email_address

  private

  def email_address
    user.email
  end
end

other_user = User.new(name: 'L')
WelcomeMailer.deliver(user: other_user)

Method that returns a collection of recipients

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to "user@example.com"
  cc :recipients

  private

  def recipients
    users.map(&:email)
  end
end

other_users = [User.new(name: 'L'), User.new(name: 'MG')]
WelcomeMailer.deliver(users: other_users)

Overloads:

  • #cc(value) ⇒ NilClass

    Sets the cc

    Parameters:

    • value (String, Array, Symbol)

      the hardcoded value or method name

    Returns:

    • (NilClass)
  • #ccString, ...

    Returns the cc

    Returns:

    • (String, Array, Symbol)

      the recipient

Since:

  • 0.3.0

def cc(value = nil)
  if value.nil?
    @cc
  else
    @cc = value
  end
end

#from(value) ⇒ NilClass #fromString, Symbol

Sets the sender for mail messages

It accepts a hardcoded value as a string, or a symbol that represents an instance method for more complex logic.

This value MUST be set, otherwise an exception is raised at the delivery time.

When a value is given, specify the sender of the email Otherwise, it returns the sender of the email

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Hardcoded value (String)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  from "noreply@example.com"
end

Method (Symbol)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  from :sender

  private

  def sender
    "noreply@example.com"
  end
end

Overloads:

  • #from(value) ⇒ NilClass

    Sets the sender

    Parameters:

    • value (String, Symbol)

      the hardcoded value or method name

    Returns:

    • (NilClass)
  • #fromString, Symbol

    Returns the sender

    Returns:

    • (String, Symbol)

      the sender

Since:

  • 0.1.0

def from(value = nil)
  if value.nil?
    @from
  else
    @from = value
  end
end

#reply_to(value) ⇒ NilClass #reply_toString, ...

Sets the reply_to for mail messages

It accepts a hardcoded value as a string or array of strings. For dynamic values, you can specify a symbol that represents an instance method.

This value is optional.

When a value is given, it specifies the reply_to for the email. When a value is not given, it returns the reply_to of the email.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Hardcoded value (String)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to "user@example.com"
  reply_to "other.user@example.com"
end

Hardcoded value (Array)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to ["user-1@example.com", "user-2@example.com"]
  reply_to ["other.user-1@example.com", "other.user-2@example.com"]
end

Method (Symbol)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to "user@example.com"
  reply_to :email_address

  private

  def email_address
    user.email
  end
end

other_user = User.new(name: 'L')
WelcomeMailer.deliver(user: other_user)

Method that returns a collection of recipients

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to "user@example.com"
  reply_to :recipients

  private

  def recipients
    users.map(&:email)
  end
end

other_users = [User.new(name: 'L'), User.new(name: 'MG')]
WelcomeMailer.deliver(users: other_users)

Overloads:

  • #reply_to(value) ⇒ NilClass

    Sets the reply_to

    Parameters:

    • value (String, Array, Symbol)

      the hardcoded value or method name

    Returns:

    • (NilClass)
  • #reply_toString, ...

    Returns the reply_to

    Returns:

    • (String, Array, Symbol)

      the recipient

Since:

  • 1.3.0

def reply_to(value = nil)
  if value.nil?
    @reply_to
  else
    @reply_to = value
  end
end

#subject(value) ⇒ NilClass #subjectString, Symbol

Sets the subject for mail messages

It accepts a hardcoded value as a string, or a symbol that represents an instance method for more complex logic.

This value MUST be set, otherwise an exception is raised at the delivery time.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Hardcoded value (String)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  subject "Welcome"
end

Method (Symbol)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  subject :greeting

  private

  def greeting
    "Hello, #{ user.name }"
  end
end

user = User.new(name: 'L')
WelcomeMailer.deliver(user: user)

Overloads:

  • #subject(value) ⇒ NilClass

    Sets the subject

    Parameters:

    • value (String, Symbol)

      the hardcoded value or method name

    Returns:

    • (NilClass)
  • #subjectString, Symbol

    Returns the subject

    Returns:

    • (String, Symbol)

      the subject

Since:

  • 0.1.0

def subject(value = nil)
  if value.nil?
    @subject
  else
    @subject = value
  end
end

#template(value) ⇒ NilClass #templateString

Set the template name IF it differs from the convention.

For a given mailer named Signup::Welcome it will look for signup/welcome.. templates under the root directory.

If for some reason, we need to specify a different template name, we can use this method.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Custom template name

require 'hanami/mailer'

class MyMailer
  include Hanami::Mailer
  template 'mailer'
end

Overloads:

  • #template(value) ⇒ NilClass

    Sets the given value

    Parameters:

    • value (String, #to_s)

      relative template path, under root

    Returns:

    • (NilClass)
  • #templateString

    Gets the template name

    Returns:

    • (String)

See Also:

Since:

  • 0.1.0

def template(value = nil)
  if value.nil?
    @template ||= ::Hanami::Mailer::Rendering::TemplateName.new(name, configuration.namespace).to_s
  else
    @template = value
  end
end

#to(value) ⇒ NilClass #toString, ...

Sets the recipient for mail messages

It accepts a hardcoded value as a string or array of strings. For dynamic values, you can specify a symbol that represents an instance method.

This value MUST be set, otherwise an exception is raised at the delivery time.

When a value is given, specify the recipient of the email Otherwise, it returns the recipient of the email

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding class variable. When called without, it will return the already set value, or the default.

Examples:

Hardcoded value (String)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to "user@example.com"
end

Hardcoded value (Array)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer

  to ["user-1@example.com", "user-2@example.com"]
end

Method (Symbol)

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to :email_address

  private

  def email_address
    user.email
  end
end

user = User.new(name: 'L')
WelcomeMailer.deliver(user: user)

Method that returns a collection of recipients

require 'hanami/mailer'

class WelcomeMailer
  include Hanami::Mailer
  to :recipients

  private

  def recipients
    users.map(&:email)
  end
end

users = [User.new(name: 'L'), User.new(name: 'MG')]
WelcomeMailer.deliver(users: users)

Overloads:

  • #to(value) ⇒ NilClass

    Sets the recipient

    Parameters:

    • value (String, Array, Symbol)

      the hardcoded value or method name

    Returns:

    • (NilClass)
  • #toString, ...

    Returns the recipient

    Returns:

    • (String, Array, Symbol)

      the recipient

Since:

  • 0.1.0

def to(value = nil)
  if value.nil?
    @to
  else
    @to = value
  end
end