thedigitalproblemsolver/incubator-mailer

Phalcon 5 Incubator Mailer Adapters

1.0.1 2022-11-13 16:46 UTC

This package is auto-updated.

Last update: 2024-04-13 23:20:29 UTC


README

Discord Packagist Version PHP from Packagist codecov Packagist

Usage examples of the mailer wrapper over SwiftMailer for Phalcon:

Configure

SMTP

$config = [
    'driver'     => 'smtp',
    'host'       => 'smtp.gmail.com',
    'port'       => 465,
    'encryption' => 'ssl',
    'username'   => 'example@gmail.com',
    'password'   => 'your_password',
    'from'       => [
        'email' => 'example@gmail.com',
        'name'  => 'YOUR FROM NAME',
    ],
];

Sendmail

$config = [
    'driver'    => 'sendmail',
    'sendmail'  => '/usr/sbin/sendmail -bs',
    'from'      => [
        'email' => 'example@gmail.com',
        'name'  => 'YOUR FROM NAME',
    ],
];

Send message

createMessage()

$mailer = new \Phalcon\Incubator\Mailer\Manager($config);

$message = $mailer->createMessage()
        ->to('example_to@gmail.com', 'OPTIONAL NAME')
        ->subject('Hello world!')
        ->content('Hello world!');

// Set the Cc addresses of this message.
$message->cc('example_cc@gmail.com');

// Set the Bcc addresses of this message.
$message->bcc('example_bcc@gmail.com');

// Send message
$message->send();

createMessageFromView()

/**
    To create message with View, you need to define in the DI the component simple View. 
*/
$this->di->set(
    'simple',
    function () {
        $view = new Phalcon\Mvc\View\Simple();

        $view->setViewsDir($config->application->viewsDir);

        return $view;
    },
    true
);

$this->di->setShared('view', function () {
    $view = new Phalcon\Mvc\View();
    $view->setDI($this);
    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines([
        '.volt'  => function ($view) {

            $volt = new Phalcon\Mvc\View\Engine\Volt($view, $this);

            $volt->setOptions([
                'path' => $config->application->cacheDir,
                'separator' => '_'
            ]);

            return $volt;
        },
        '.phtml' => Phalcon\Mvc\View\Engine\Php::class

    ]);

    return $view;
});

$mailer = new \Phalcon\Incubator\Mailer\Manager($config);

// view relative to the folder viewsDir (REQUIRED)
$viewPath = 'email/example_message';

// Set variables to views (OPTIONAL)
$params = [ 
    'var1' => 'VAR VALUE 1',
    'var2' => 'VAR VALUE 2',
    // ...
    'varN' => 'VAR VALUE N',
];

$message = $mailer->createMessageFromView($viewPath, $params)
        ->to('example_to@gmail.com', 'OPTIONAL NAME')
        ->subject('Hello world!');

// Set the Cc addresses of this message.
$message->cc('example_cc@gmail.com');

// Set the Bcc addresses of this message.
$message->bcc('example_bcc@gmail.com');

// Send message
$message->send();

Events

  • mailer:beforeCreateMessage
  • mailer:afterCreateMessage
  • mailer:beforeSend
  • mailer:afterSend
  • mailer:beforeAttachFile
  • mailer:afterAttachFile