PHP code example of myth / postal

1. Go to this page and download the library: Download myth/postal 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/ */

    

myth / postal example snippets




use Myth\Postal\Email;

$email = (new Email())
    ->from('[email protected]', 'Your Name')
    ->to('[email protected]')
    ->subject('Welcome aboard')
    ->html('<p>Glad to have you with us.</p>');

$result = service('mailer')->send($email);

if ($result->success) {
    log_message('info', 'Sent message ' . $result->messageId);
}



service('mailer')->mailer('ses')->send($email);   // use the 'ses' mailer for this send



use Myth\Postal\Mailable;

class WelcomeEmail extends Mailable
{
    public function __construct(private User $user)
    {
        parent::__construct();
    }

    protected function build(): void
    {
        $this->to($this->user->email)
             ->subject('Welcome aboard')
             ->html(view('emails/welcome', ['user' => $this->user]));
    }
}

(new WelcomeEmail($user))->send();



$email = service('email');
$email->setTo('[email protected]');
$email->setSubject('Hello');
$email->setMessage('<p>Hi there.</p>');
$email->send();



public string $default = match (ENVIRONMENT) {
    'production' => 'ses',
    'testing'    => 'null',
    default      => 'log',
};

public array $mailers = [
    'smtp' => [
        'transport'  => 'smtp',
        'host'       => 'localhost',
        'port'       => 587,
        'encryption' => 'tls',
        'username'   => '',
        'password'   => '',
    ],
    'log'  => ['transport' => 'log'],
    'null' => ['transport' => 'null'],
];



use Myth\Postal\Mailer;

$fake = Mailer::fake();

(new WelcomeEmail($user))->send();

$fake->assertSent(WelcomeEmail::class);
$fake->assertSentTo($user->email);
$fake->assertSent(fn ($message) => str_contains($message->subject, 'Welcome'));
$fake->assertSentCount(1);
bash
php spark make:mailable WelcomeEmail