Luminova Framework

PHP Luminova: Sending Email with Luminova Mailer

Last updated: 2026-11-28 20:57:12

Send plain text or HTML email with Luminova Mailer. Configure a mail client, add recipients and attachments, and render email content from views or reusable templates.

Use Luminova\Components\Email\Mailer to prepare an email, choose its recipients, and hand the message to your configured mail client. The client handles delivery through PHP’s mail transport or SMTP, depending on the implementation and your settings.


Mail Clients

Luminova includes NovaMailer and adapters for PHPMailer and SwiftMailer. Install the corresponding package before selecting a third-party client.


Application Mail Client

Override getMailer() inside your existing App\Kernel class in app/Kernel.php to choose the mail client for your application. For example, to select PHPMailer, add these imports and the method below after installing the package.

use Luminova\Components\Email\Clients\PHPMailer;
use Luminova\Interface\MailerInterface;
public function getMailer(mixed ...$arguments): MailerInterface
{
    return new PHPMailer(...$arguments);
}

For sender and transport configuration, see Application Mail Client Configuration.


Choose a Client for One Message

NovaMailer

NovaMailer is Luminova's built-in mail client. It doesn't require any third-party packages.

use Luminova\Components\Email\Clients\NovaMailer;
use Luminova\Components\Email\Mailer;

$mailer = new Mailer(NovaMailer::class);

PHPMailer

To use PHPMailer, install it first:

composer require phpmailer/phpmailer

Then pass it to the Mailer constructor:

use Luminova\Components\Email\Clients\PHPMailer;
use Luminova\Components\Email\Mailer;

$mailer = new Mailer(PHPMailer::class);

SwiftMailer

Install SwiftMailer:

composer require "swiftmailer/swiftmailer:^6.0"

Then initialize it:

use Luminova\Components\Email\Clients\SwiftMailer;
use Luminova\Components\Email\Mailer;

$mailer = new Mailer(SwiftMailer::class);

Note:

Passing a mail client directly to the Mailer constructor overrides whatever is set in your mailer configuration.Use this to choose a different client for a particular message.


Examples

Mail APIs

Set the recipient, sender, subject, and message body before calling send().

use Luminova\Components\Email\Clients\NovaMailer;
use Luminova\Components\Email\Mailer;

(new Mailer(NovaMailer::class))
    ->address('[email protected]')
    ->from('[email protected]')
    ->subject('Email Hello!')
    ->body('<p>HTML email message body</p>')
    ->text('Alt text email body!')
    // Extra options you may use:
    // ->address('[email protected]')
    // ->addresses('[email protected],[email protected]')
    // ->cc('[email protected]')
    // ->bcc('[email protected]')
    // ->reply('[email protected]')
    // ->notification('[email protected]')
    // ->isMail()
    // ->isHtml()
    // ->options([...])
    // ->header('X-Custom', 'Value')
    // ->attachment('/path/to/file1.pdf')
    // ->attachment('/path/to/file2.png')
    ->send();

Send a Reusable Email Template

Implement your welcome message as an email template. Passing the template to send() applies its subject, HTML or plain-text content, and attachments.

use App\Models\Emails\EmailTemplate;
use Luminova\Components\Email\Clients\NovaMailer;
use Luminova\Components\Email\Mailer;

$sent = (new Mailer(NovaMailer::class))
    ->address('[email protected]')
    ->send(new EmailTemplate());

Send Content from a View

Use contents() to render a view into a string for email. Check the return value before passing it to the mailer.

use Luminova\Components\Email\Clients\NovaMailer;
use Luminova\Components\Email\Mailer;
use function Luminova\Funcs\view;

$html = view('order_email_template')->contents(['orderNumber' => 123]);

if ($html !== null) {
    $sent = (new Mailer(NovaMailer::class))
        ->address('[email protected]')
        ->subject('Order confirmation')
        ->send($html);
}

Use a View Promise

The view’s promise() method provides the rendered HTML to a callback. The mail client sends the message synchronously inside that callback.

use Luminova\Components\Email\Clients\NovaMailer;
use Luminova\Components\Email\Mailer;
use function Luminova\Funcs\view;

view('order_email_template')
    ->promise(['orderNumber' => 123])
    ->then(static function (string $html): bool {
        return (new Mailer(NovaMailer::class))
            ->address('[email protected]')
            ->subject('Order confirmation')
            ->send($html);
    });

Class Definition


Methods

See Reusable Email Templates for messages with a shared layout.


__construct

Create a mailer with a client instance, a client class name, or the application’s configured client.

public function __construct(MailerInterface|string|null $interface = null)

Parameters:

ParameterTypeDescription
$interfaceMailerInterface&#124;string&#124;nullClient instance or class name. Defaults to null, which resolves the client through the application kernel.

Throws:


getInstance

Get the shared mailer. The client argument only applies when the shared instance is first created. Use a new mailer for each independent message.

public static function getInstance(MailerInterface|string|null $interface = null): self

Parameters:

ParameterTypeDescription
$interfaceLuminova\Interface\MailerInterface&#124;string&#124;nullThe mailer client interface to be used for instantiation.

Return:

self - The shared mailer.


getClient

Retrieve the Mailer client instance.

This method returns an instance of the mail client in use.

public function getClient(): ?MailerInterface

Return:

?MailerInterface - The selected mail client, or null if no client is set.

Example:

$client = $mailer->getClient()?->getMailer();

isHtml

Set the email format to HTML or plain text.

This method sets whether the email should be sent as HTML or plain text.By default, it sends HTML. If you want to send a plain text email, pass false to the method.

public function isHtml(bool $html = true): self

Parameters:

ParameterTypeDescription
$htmlboolWhether the email should be sent as HTML (default is true).

Return:

self - Return the Mailer class instance.


isMail

Set the mail transport method to either Mail or SMTP.

This method determines whether to send emails using the PHP mail() function or via SMTP. The initial transport is selected from smtp.use.credentials. To send emails via SMTP, pass false to this method.

public function isMail(bool $mail = true): self

Parameters:

ParameterTypeDescription
$mailboolWhether to use PHP mail() (default is true). Set to false to use SMTP.

Return:

self - Return the Mailer class instance.


to

Create a mailer using the application’s configured client and add one recipient. Call send() to send the message.

public static function to(string $address): self

Parameters:

ParameterTypeDescription
$addressstringEmail address to send email to.

Return:

self - A new mailer with the recipient already added.

Throws:


address

Add an email address to the recipient list.

public function address(string $address, string $name = ''): self

Parameters:

ParameterTypeDescription
$addressstringThe email address.
$namestringThe recipient's name (optional).

Return:

self - Return the Mailer class instance.


addresses

Add multiple addresses using the selected mail client’s bulk recipient method. NovaMailer accepts a comma-separated string, a list of addresses, or a map from email address to display name. For consistent recipient handling across clients, call address() for each recipient.

public function addresses(array|string $address): self

Parameters:

ParameterTypeDescription
$addressarray<string,string>&#124;string[]&#124;stringAddresses in a format supported by the selected client.

Return:

self - The current mailer.

Example:

// With NovaMailer.
$mailer->addresses(['[email protected]' => 'John', '[email protected]' => 'Deo']);

header

Add a custom header to the email.

The header is specified by a key (the header name), and the optional value (the header's value). If no value is provided, the header will be added without a value.

public function header(string $key, ?string $value = null): self

Parameters:

ParameterTypeDescription
$keystringThe header name.
$valuestring&#124;nullThe header value (optional).

Return:

self - Return the Mailer class instance.


reply

Add a reply-to address.

public function reply(string $address, string $name = ''): self

Parameters:

ParameterTypeDescription
$addressstringThe email address.
$namestringThe recipient's name (optional).

Return:

self - Return the Mailer class instance.


notification

Set the notification address for read and delivery receipts.

This method allows you to specify an email address where notifications should be sent regarding the status of the email, such as delivery or read receipts.

public function notification(string $address): self

Parameters:

ParameterTypeDescription
$addressstringThe email address to receive the notification.

Return:

self - Return the Mailer class instance.


cc

Add a carbon copy (Cc) recipient.

public function cc(string $address, string $name = ''): self

Parameters:

ParameterTypeDescription
$addressstringThe email address.
$namestringThe recipient's name (optional).

Return:

self - Return the Mailer class instance.


bcc

Add a blind carbon copy (Bcc) recipient.

public function bcc(string $address, string $name = ''): self

Parameters:

ParameterTypeDescription
$addressstringThe email address.
$namestringThe recipient's name (optional).

Return:

self - Return the Mailer class instance.


from

Set the email sender's address.

public function from(string $address, string $name = '', bool $auto = true): self

Parameters:

ParameterTypeDescription
$addressstringThe email address.
$namestringThe sender's name (optional).
$autoboolWhether to enable the client’s automatic sender handling (default: true).

Return:

self - Return the Mailer class instance.


body

Set the message body. An AbstractEmailTemplate also supplies the subject, plain-text alternative, and attachments.

public function body(Luminova\Base\AbstractEmailTemplate|string $message): self

Parameters:

ParameterTypeDescription
$messageLuminova\Base\AbstractEmailTemplate&#124;stringThe body content of the email.

Return:

self - Return the Mailer class instance.


text

Sets the text alternative body of the email message.

public function text(string $message): self

Parameters:

ParameterTypeDescription
$messagestringThe alternative body content of the email.

Return:

self - Return the Mailer class instance.


subject

Sets the subject of the email message.

public function subject(string $subject): self

Parameters:

ParameterTypeDescription
$subjectstringThe subject of the email.

Return:

self - Return the Mailer class instance.


options

Set SMTP stream options.

Pass stream context options to a client that supports SMTPOptions, such as NovaMailer or PHPMailer.

public function options(array $smtpOptions): self

Parameters:

ParameterTypeDescription
$smtpOptionsarray<array-key, mixed>An associative array of stream context options.

Return:

self - Return the Mailer class instance.

Example:

$mailer->options([
    'ssl' => [
        'cafile' => '/path/to/ca-bundle.pem',
    ]
]);

priority

Set the message priority on clients that support it. Common values are 1 for high, 3 for normal, and 5 for low.

public function priority(int $priority): self

Parameters:

ParameterTypeDescription
$priorityintSet the message priority on clients that support it. Common values are 1 for high, 3 for normal, and 5 for low.

Return:

self - The current mailer.


xMailer

Set the software name used for the X-Mailer header.

public function xMailer(string $xMailer): self

Parameters:

ParameterTypeDescription
$xMailerstringSet the software name used for the X-Mailer header.

Return:

self - The current mailer.


organization

Set the sending organization for clients that support the Organization header, such as NovaMailer.

public function organization(string $org): self

Parameters:

ParameterTypeDescription
$orgstringSet the sending organization for clients that support the Organization header, such as NovaMailer.

Return:

self - The current mailer.


charset

Set the message character set, such as UTF-8.

public function charset(string $charset): self

Parameters:

ParameterTypeDescription
$charsetstringSet the message character set, such as UTF-8.

Return:

self - The current mailer.


timeout

Set the timeout in seconds on clients that honor the Timeout property.

public function timeout(int $timeout): self

Parameters:

ParameterTypeDescription
$timeoutintSet the timeout in seconds on clients that honor the Timeout property.

Return:

self - The current mailer.


attachment

Add an attachment from a path on the filesystem.

public function attachment(
    string $path,
    string $name = '',
    string $encoding = 'base64',
    string $type = '',
    string $disposition = 'attachment'
): self

Parameters:

ParameterTypeDescription
$pathstringPath to the attachment
$namestringOverrides the attachment name
$encodingstringTransfer encoding. Defaults to base64.
$typestringMIME type, such as image/jpeg. An empty value lets the client infer it.
$dispositionstringDisposition to use

Return:

self - Return the Mailer class instance.

Throws:


send

Send the prepared email through the selected client. If you supply a message, it is applied before sending. Client send exceptions are logged and return false in production. Outside production they are rethrown or wrapped in a MailerException.

public function send(Luminova\Base\AbstractEmailTemplate|string|null $message = null): bool

Parameters:

ParameterTypeDescription
$messageLuminova\Base\AbstractEmailTemplate&#124;string&#124;nullOptionally pass message body in send method.

Return:

bool - True if the email was sent successfully, false otherwise.

Throws: