abstract class Amber::Mailer::Base

Overview

Abstract base class for all application mailers.

Provides a fluent API for composing and delivering email messages. Subclasses must implement #html_body and #text_body to define the email content.

Usage

class WelcomeMailer < Amber::Mailer::Base
  def initialize(@user_name : String, @user_email : String)
  end

  def html_body : String?
    "<h1>Welcome, #{HTML.escape(@user_name)}!</h1>"
  end

  def text_body : String?
    "Welcome, #{@user_name}!"
  end
end

mailer = WelcomeMailer.new("Alice", "[email protected]")
mailer.to("[email protected]").from("[email protected]").subject("Welcome!").deliver

Fluent API

All setter methods return self to enable method chaining:

mailer.to("[email protected]", "[email protected]")
  .from("[email protected]")
  .subject("Update")
  .cc("[email protected]")
  .deliver

Defined in:

amber/mailer/base.cr

Instance Method Summary

Instance Method Detail

def attach(filename : String, content : Bytes, mime_type : String = "application/octet-stream") : self #

Attaches binary content as a file attachment.

The content is base64-encoded in the generated MIME message.

Parameters

  • filename - The filename to use in the Content-Disposition header
  • content - The raw binary content of the file
  • mime_type - The MIME type of the file (defaults to "application/octet-stream")

[View source]
def attach_file(path : String, filename : String | Nil = nil, mime_type : String = "application/octet-stream") : self #

Attaches a file from the filesystem.

Reads the file content and determines a filename automatically. If filename is not provided, the basename of the path is used.

Parameters

  • path - The filesystem path to the file
  • filename - Optional override for the attachment filename
  • mime_type - The MIME type of the file (defaults to "application/octet-stream")

Raises

Raises File::NotFoundError if the file does not exist.


[View source]
def bcc(*addresses : String) : self #

Sets the BCC (blind carbon copy) recipient addresses for this email.

Multiple addresses can be provided as separate arguments. BCC recipients are not visible to other recipients. Calling this method replaces any previously set BCC recipients.


[View source]
def build : Email #

Builds an Email struct from the current mailer state.

Populates the sender from the configuration default if not explicitly set. This method is called automatically by #deliver but can be used directly to inspect the email before sending.


[View source]
def cc(*addresses : String) : self #

Sets the CC (carbon copy) recipient addresses for this email.

Multiple addresses can be provided as separate arguments. Calling this method replaces any previously set CC recipients.


[View source]
def custom_headers : Hash(String, String) #

[View source]
def deliver : DeliveryResult #

Delivers the email using the configured delivery adapter.

Builds the email and sends it through the adapter specified in the mailer configuration.

Returns

A DeliveryResult indicating whether the delivery succeeded.


[View source]
def email_subject : String #

[View source]
def from(address : String) : self #

Sets the sender address for this email.

If not set, the default_from address from the mailer configuration will be used when building the email.


[View source]
def header(name : String, value : String) : self #

Adds a custom header to this email.

Custom headers are included in the generated MIME message. Standard headers (From, To, Subject, etc.) should be set using their dedicated methods rather than this one.


[View source]
abstract def html_body : String | Nil #

Returns the HTML body content for this email, or nil if no HTML body.

Subclasses should override this method to provide HTML content. The content is NOT auto-escaped; the developer is responsible for escaping user-provided data.


[View source]
def list_of_attachments : Array(Attachment) #

[View source]
def list_of_bcc_recipients : Array(String) #

[View source]
def list_of_cc_recipients : Array(String) #

[View source]
def list_of_recipients : Array(String) #

[View source]
def reply_to(address : String) : self #

Sets the Reply-To address for this email.

When set, email clients will use this address instead of the From address when the recipient clicks Reply.


[View source]
def reply_to_address : String | Nil #

[View source]
def sender : String #

[View source]
def subject(text : String) : self #

Sets the subject line for this email.


[View source]
abstract def text_body : String | Nil #

Returns the plain text body content for this email, or nil if no text body.

Subclasses should override this method to provide plain text content.


[View source]
def to(*addresses : String) : self #

Sets the recipient addresses for this email.

Multiple addresses can be provided as separate arguments. Calling this method replaces any previously set recipients.


[View source]