PHP code example of ez-php / mail

1. Go to this page and download the library: Download ez-php/mail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ez-php / mail example snippets


use EzPhp\Mail\MailServiceProvider;

$app->register(MailServiceProvider::class);

return [
    'driver'       => getenv('MAIL_DRIVER') ?: 'null',
    'host'         => getenv('MAIL_HOST') ?: '127.0.0.1',
    'port'         => (int) (getenv('MAIL_PORT') ?: 587),
    'username'     => getenv('MAIL_USERNAME') ?: '',
    'password'     => getenv('MAIL_PASSWORD') ?: '',
    'encryption'   => getenv('MAIL_ENCRYPTION') ?: 'tls',
    'from_address' => getenv('MAIL_FROM_ADDRESS') ?: '',
    'from_name'    => getenv('MAIL_FROM_NAME') ?: '',
    'log_path'     => getenv('MAIL_LOG_PATH') ?: '',
    'timeout'      => (int) (getenv('MAIL_TIMEOUT') ?: 30), // smtp, mailgun, sendgrid
];

use EzPhp\Mail\Mail;
use EzPhp\Mail\Mailable;

Mail::send(
    (new Mailable())
        ->to('[email protected]', 'Alice')
        ->subject('Welcome!')
        ->text('Hello Alice, welcome aboard.')
        ->html('<p>Hello Alice, <strong>welcome aboard.</strong></p>')
);

// Inline
$mail = (new Mailable())
    ->to('[email protected]', 'Bob')
    ->from('[email protected]', 'My App')   // overrides driver default
    ->subject('Your Invoice')
    ->text('Please find your invoice attached.')
    ->html('<p>Please find your invoice <strong>attached</strong>.</p>')
    ->attach('/path/to/invoice.pdf', 'Invoice-2026-01.pdf');

// Extended
class InvoiceMail extends Mailable
{
    public function __construct(User $user, string $invoicePath)
    {
        $this->to($user->email, $user->name)
             ->subject('Your Invoice')
             ->text('Please find your invoice attached.')
             ->attach($invoicePath);
    }
}

use EzPhp\Mail\Mail;
use EzPhp\Mail\Mailable;
use EzPhp\Mail\MailerInterface;

// Arrange
$spy = new class implements MailerInterface {
    public array $sent = [];
    public function send(Mailable $mailable): void { $this->sent[] = $mailable; }
};
Mail::setMailer($spy);

// Act
Mail::send((new Mailable())->to('[email protected]')->subject('Hi')->text('body'));

// Assert
assert(count($spy->sent) === 1);

// Teardown
Mail::resetMailer();

use EzPhp\Mail\Job\SendMailableJob;
use EzPhp\Mail\Mailable;

$queue->push(new SendMailableJob(
    (new Mailable())
        ->to($user->email, $user->name)
        ->subject('Welcome!')
        ->text('Hello, welcome aboard.')
));
bash
composer