mtangoo / yii2-symfonymailer

Yii 2 Symfony mailer extension.

Home Page:https://www.yiiframework.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Yii Mailer Library - Symfony Mailer Extension


This extension provides a Symfony Mailer mail solution for Yii framework 2.0.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiisoft/yii2-symfonymailer

or add

"yiisoft/yii2-symfonymailer": "~3.0.0"

to the require section of your composer.json.

Usage

To use this extension, simply add the following code in your application configuration:

return [
    //....
    'components' => [
        'mailer' => [
            'class' => \yii\symfonymailer\Mailer::class,            
            'transport' => [
                'scheme' => 'smtps',
                'host' => '',
                'username' => '',
                'password' => '',
                'port' => 465,
                'dsn' => 'native://default',
            ],
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure transport
            // for the mailer to send real emails.
            'useFileTransport' => false,
        ],
    ],
];

or

return [
    //....
    'components' => [
        'mailer' => [
            'class' => \yii\symfonymailer\Mailer::class,            
            'transport' => [
                'dsn' => 'smtp://user:pass@smtp.example.com:25',
            ],
        ],
    ],
];

You can then send an email as follows:

Yii::$app->mailer->compose('contact/html')
     ->setFrom('from@domain.com')
     ->setTo($form->email)
     ->setSubject($form->subject)
     ->send();

Migrating from yiisoft/yii2-swiftmailer

To migrate from the deprecated yiisoft/yii2-swiftmailer to this extension you need to update the application config.

Swiftmailer default transport was the SendmailTransport, while with this extension it will default to a NullTransport (sends no mail). You can use the swiftmailer default like the following:

'mailer' => [
    'class' => yii\symfonymailer\Mailer::class,
    'transport' => [
        'dsn' => 'sendmail://default',
    ],
],

With this extension, you do not have ability of directly setting timeout for example which with former extension could be set on configuration. The reason is, the underlying symfony extension defines its classes as final thereby discouraging inheritance and pushing towards composition. To achieve timeout (and other transport configurations), you will need to define factory class. below is a sample example for SMTP transport.

namespace app\utils; //file is in utils folder of your application

use Symfony\Component\Messenger\Transport\TransportFactoryInterface;

class CustomSmtpFactory implements TransportFactoryInterface {
    public function __construct(private TransportFactoryInterface $factory, private float $timeout)
    {
        $this->timeout = 120;
    }

    public function create(Dsn $dsn): TransportInterface
    {
        $result = $this->factory->create($dsn);
        if ($result instanceof SmtpTransport) {
            //Setup timeout to this or 
            $result->getStream()->setTimeout($this->timeout);
        }
        return $result;
    }

    public function supports(Dsn $dsn): bool {
        return $this->factory->supports($dsn);
    }
}

then in configuration, set the factory

   'mailer' => [
       'class' => yii\symfonymailer\Mailer::class,
       'transportFactory' => app\utils\CustomSmtpFactory::class,
        'transport' => [
            'scheme' => 'smtp',
            //other settings
        ],
   ],

Security implications of the DSN

While the DSN might seem like a simple way to allow user configurable mailer settings it should be noted that the sendmail transport allows for execution of local executables. If you need to have a user configurable DSN (which is easier to build and more powerful to use than creating a GUI) you should probably disable the sendmail transport. Any user who has the power to configure a DSN essentially has shell access to wherever the code is running.

About

Yii 2 Symfony mailer extension.

https://www.yiiframework.com/

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:PHP 100.0%