abstract class
Amber::Mailer::Base
- Amber::Mailer::Base
- Reference
- Object
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.crInstance Method Summary
-
#attach(filename : String, content : Bytes, mime_type : String = "application/octet-stream") : self
Attaches binary content as a file attachment.
-
#attach_file(path : String, filename : String | Nil = nil, mime_type : String = "application/octet-stream") : self
Attaches a file from the filesystem.
-
#bcc(*addresses : String) : self
Sets the BCC (blind carbon copy) recipient addresses for this email.
-
#build : Email
Builds an
Emailstruct from the current mailer state. -
#cc(*addresses : String) : self
Sets the CC (carbon copy) recipient addresses for this email.
- #custom_headers : Hash(String, String)
-
#deliver : DeliveryResult
Delivers the email using the configured delivery adapter.
- #email_subject : String
-
#from(address : String) : self
Sets the sender address for this email.
-
#header(name : String, value : String) : self
Adds a custom header to this email.
-
#html_body : String | Nil
Returns the HTML body content for this email, or nil if no HTML body.
- #list_of_attachments : Array(Attachment)
- #list_of_bcc_recipients : Array(String)
- #list_of_cc_recipients : Array(String)
- #list_of_recipients : Array(String)
-
#reply_to(address : String) : self
Sets the Reply-To address for this email.
- #reply_to_address : String | Nil
- #sender : String
-
#subject(text : String) : self
Sets the subject line for this email.
-
#text_body : String | Nil
Returns the plain text body content for this email, or nil if no text body.
-
#to(*addresses : String) : self
Sets the recipient addresses for this email.
Instance Method Detail
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 headercontent- The raw binary content of the filemime_type- The MIME type of the file (defaults to "application/octet-stream")
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 filefilename- Optional override for the attachment filenamemime_type- The MIME type of the file (defaults to "application/octet-stream")
Raises
Raises File::NotFoundError if the file does not exist.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Sets the recipient addresses for this email.
Multiple addresses can be provided as separate arguments. Calling this method replaces any previously set recipients.