PHP Luminova: Sending Email with Luminova Mailer
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/phpmailerThen 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
Mailerconstructor 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
- Class namespace:
Luminova\Components\Email\Mailer - This class implements: Luminova\Interface\LazyObjectInterface
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:
| Parameter | Type | Description |
|---|---|---|
$interface | MailerInterface|string|null | Client instance or class name. Defaults to null, which resolves the client through the application kernel. |
Throws:
- Luminova\Exceptions\MailerExceptionThe supplied class is unavailable or the client does not implement
MailerInterface.
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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$interface | Luminova\Interface\MailerInterface|string|null | The 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(): ?MailerInterfaceReturn:
?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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$html | bool | Whether 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$mail | bool | Whether 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | Email address to send email to. |
Return:
self - A new mailer with the recipient already added.
Throws:
- Luminova\Exceptions\MailerException - Throws if error occurred while sending email.
address
Add an email address to the recipient list.
public function address(string $address, string $name = ''): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | The email address. |
$name | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | array<string,string>|string[]|string | Addresses 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The header name. |
$value | string|null | The header value (optional). |
Return:
self - Return the Mailer class instance.
reply
Add a reply-to address.
public function reply(string $address, string $name = ''): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | The email address. |
$name | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | The 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 = ''): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | The email address. |
$name | string | The 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 = ''): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | The email address. |
$name | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$address | string | The email address. |
$name | string | The sender's name (optional). |
$auto | bool | Whether 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$message | Luminova\Base\AbstractEmailTemplate|string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$subject | string | The 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$smtpOptions | array<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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$priority | int | Set 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$xMailer | string | Set 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$org | string | Set 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$charset | string | Set 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): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$timeout | int | Set 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'
): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Path to the attachment |
$name | string | Overrides the attachment name |
$encoding | string | Transfer encoding. Defaults to base64. |
$type | string | MIME type, such as image/jpeg. An empty value lets the client infer it. |
$disposition | string | Disposition to use |
Return:
self - Return the Mailer class instance.
Throws:
- Luminova\Exceptions\MailerException - Throws if error occurred while sending email.
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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$message | Luminova\Base\AbstractEmailTemplate|string|null | Optionally pass message body in send method. |
Return:
bool - True if the email was sent successfully, false otherwise.
Throws:
- Luminova\Exceptions\MailerException - Throws if error occurred while sending email.