Skip to content

bakaphp/incubator-mailer

 
 

Repository files navigation

Phalcon\Incubator\Mailer

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'   => '[email protected]',
    'password'   => 'your_password',
    'from'       => [
        'email' => '[email protected]',
        'name'  => 'YOUR FROM NAME',
    ],
];

Sendmail

$config = [
    'driver'    => 'sendmail',
    'sendmail'  => '/usr/sbin/sendmail -bs',
    'from'      => [
        'email' => '[email protected]',
        'name'  => 'YOUR FROM NAME',
    ],
];

Send message

createMessage()

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

$message = $mailer->createMessage()
        ->to('[email protected]', 'OPTIONAL NAME')
        ->subject('Hello world!')
        ->content('Hello world!');

// Set the Cc addresses of this message.
$message->cc('[email protected]');

// Set the Bcc addresses of this message.
$message->bcc('[email protected]');

// 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('[email protected]', 'OPTIONAL NAME')
        ->subject('Hello world!');

// Set the Cc addresses of this message.
$message->cc('[email protected]');

// Set the Bcc addresses of this message.
$message->bcc('[email protected]');

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

Events

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

Packages

No packages published

Languages

  • PHP 99.8%
  • Other 0.2%