Coder Social home page Coder Social logo

incubator-mailer's Introduction

Phalcon\Incubator\Mailer

Discord Packagist Version PHP from Packagist codecov Packagist

Usage examples of the mailer wrapper over PHPMailer 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 () use ($config) {
        $view = new Phalcon\Mvc\View\Simple();
        $view->setViewsDir($config->application->viewsDir);

        return $view;
    },
    true
);

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

    $view->registerEngines([
        '.volt'  => function ($view) {
            $volt = new Phalcon\Mvc\View\Engine\Volt($view, $this->di);
            $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

https://docs.phalcon.io/5.0/en/events

All events are callables with at least 2 arguments

Look for each event down below for more informations of the third argument

- mailer:beforeCreateMessage
    function (Phalcon\Events\Event $event, Phalcon\Incubator\Mailer\Manager $manager, null) {};

- mailer:afterCreateMessage
    function (Phalcon\Events\Event $event, Phalcon\Incubator\Mailer\Manager $manager, Phalcon\Incubator\Mailer\Message $message) {};

- mailer:beforeSend
    function (Phalcon\Events\Event $event, Phalcon\Incubator\Mailer\Message $message, null) {};

- mailer:afterSend
    2 arguments, the number of sent mails and an array of emails representing the failed recipients

    function (Phalcon\Events\Event $event, Phalcon\Incubator\Mailer\Message $message, [int $count, array $failedRecipients]) {};

- mailer:beforeAttachFile
    function (Phalcon\Events\Event $event, Phalcon\Incubator\Mailer\Message $message, null) {};

- mailer:afterAttachFile
    1 argument, an array with the attachment informations

    function (Phalcon\Events\Event $event, Phalcon\Incubator\Mailer\Message $message, array $attachment) {};

    0: string (path of the file or encoded data)
    1: string (name of the attachment)
    2: string (basename of the attachment)
    3: string (encoding)
    4: string (MIME type)
    5: bool (false -> encoded data, true -> from a file)
    6: string (disposition of the mail)
    7: string|0 (if from a file, name of the file)

incubator-mailer's People

Contributors

dependabot[bot] avatar jeckerson avatar jenovateurs avatar niden avatar rayanlevert avatar yannux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.