PHP code example of vancil / flint-mail

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

    

vancil / flint-mail example snippets


'packages' => [
    \Vancil\FlintMail\FlintMail::class,
],

namespace App\Mail;

use Vancil\FlintMail\Mailable;

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

    public function build(): void
    {
        $this->to($this->user->email, $this->user->name)
             ->subject('Welcome to ' . config('app.name'))
             ->view('emails.welcome', ['user' => $this->user]);
    }
}

// Synchronous — sends immediately
(new WelcomeEmail($user))->send();

// Queued — dispatched to Flint's queue worker
(new WelcomeEmail($user))->queue();
(new WelcomeEmail($user))->queue('emails'); // named queue

$this->to('[email protected]', 'User')         // recipient
     ->from('[email protected]', 'Sender')   // override default from
     ->replyTo('[email protected]')
     ->subject('Hello')
     ->cc('[email protected]', 'Manager')
     ->cc('[email protected]')
     ->bcc('[email protected]')
     ->html('<p>Hello <b>world</b></p>')       // raw HTML body
     ->text('Hello world')                     // plain text fallback
     ->view('emails.welcome', ['user' => $u]) // Spark template as HTML
     ->attach('/path/to/file.pdf', 'Invoice.pdf', 'application/pdf');

use Vancil\FlintMail\Mailer;

class NotificationController
{
    public function __construct(private readonly Mailer $mailer) {}

    public function notify(Request $request): Response
    {
        $this->mailer
            ->to('[email protected]', 'User')
            ->subject('New notification')
            ->html('<p>You have a new message.</p>')
            ->cc('[email protected]')
            ->send();

        // Or queue it:
        $this->mailer
            ->to('[email protected]')
            ->subject('Report')
            ->attach('/tmp/report.pdf', 'Monthly Report.pdf')
            ->queue('reports');

        return Response::redirect('/');
    }
}

return [
    'driver' => env('MAIL_DRIVER', 'log'),

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name'    => env('MAIL_FROM_NAME', 'Flint'),
    ],

    'mailgun'  => ['key' => env('MAILGUN_SECRET'), 'domain' => env('MAILGUN_DOMAIN'), 'region' => 'us'],
    'postmark' => ['token' => env('POSTMARK_TOKEN')],
    'ses'      => ['key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1')],
    'sendgrid' => ['key' => env('SENDGRID_API_KEY')],
];

$this->view('emails.welcome', ['user' => $user]);

namespace App\Mail\Drivers;

use Vancil\FlintMail\Drivers\DriverInterface;
use Vancil\FlintMail\MailMessage;

class ResendDriver implements DriverInterface
{
    public function send(MailMessage $message): void
    {
        // $message->to, ->toName, ->subject, ->htmlBody, ->textBody
        // ->from, ->fromName, ->replyTo, ->cc, ->bcc, ->attachments
    }
}
bash
php flint mail:install
php flint migrate
bash
php flint make:mail WelcomeEmail
bash
php flint queue:work
php flint queue:work --queue=emails   # process a named queue