2017 © Pedro Peláez
 

yii2-extension phpmailer

Phpmailer extension for the Yii framework

image

gulltour/phpmailer

Phpmailer extension for the Yii framework

  • Monday, November 13, 2017
  • by weijiansdlx
  • Repository
  • 1 Watchers
  • 0 Stars
  • 88 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 21 % Grown

The README.md

yii2-phpmailer

Phpmailer extension for the Yii framework, (*1)

yii2-phpmailer

PHPMailer integration for Yii 2 framework

WARNING !!!

Breaking backwards compatibility changes were introduced in Yii2 Framework (see https://github.com/yiisoft/yii2/issues/4071)., (*2)

If you updated after 26.06.2014 your should fix your application config and calls to mail functions (replace mail with mailer)., (*3)

In configuration file you should replace:, (*4)

    'components' => [
        'mail' => [
        ...

with, (*5)

    'components' => [
        'mailer' => [
        ...

In your code replace:, (*6)

Yii::$app->mail->... with Yii::$app->mailer->..., (*7)

(or Yii::$app->getMail()... with Yii::$app->getMailer()... respectively)., (*8)

===, (*9)

This extension adds integration of popular PHPMailer library - a "full-featured email creation and transfer class for PHP" - to Yii 2 framework., (*10)

Although extension classes implement yii\mail\MailerInterface and yii\mail\MessageInterface, some methods of Yii 2 BaseMailer and BaseMessage are overriden - mainly because of PHPMailer-specific issues., (*11)

Nevertheless - the behavior of the extension should remain expected and predictable and comply interfaces mentioned above (I believe so). If not - feel free to report issues., (*12)

REQUIREMENTS

You should generally follow Yii 2 requirements. The minimum is that your Web server supports PHP 5.4.0., (*13)

INSTALLATION

Install via Composer

If you do not have Composer, you may install it by following the instructions at getcomposer.org., (*14)

During the extension installation PHPMailer library will be also installed., (*15)

You have two options here - to install latest stable release of PHPMailer (as of now it is 5.2.8 - many significant issues were solved and improvements made in this release) or install latest development version from dev-master., (*16)

So, either run, (*17)

  • for PHPMailer stable:
php composer.phar require --prefer-dist gulltour/phpmailer "~1.0.0"
  • for dev:
php composer.phar require --prefer-dist gulltour/phpmailer "~1.0.0"

or add respectively:, (*18)

"gulltour/phpmailer": "~1.0.0"

or, (*19)

"gulltour/phpmailer": "~1.0.0"

to the require section of your composer.json., (*20)

Note: this can be affected by 'minimum-stability' settings in your project root composer.json file. If you see error trying to install 'dev-master' branch of PHPMailer you should explicitly point to it, adding, (*21)

"phpmailer/phpmailer": "*"

to the require section of composer.json in your project root., (*22)

Install from an Archive File

You may install extension manually. For that purpose you have to download archive file and extract it into vendor/zyx directory of your project., (*23)

You'll also need to download PHPMailer library and extract it to vendor/phpmailer directory of your project., (*24)

Note: due to naming agreement 3rd-party extensions and libraries are kept under vendor directory., (*25)

One of the benefits of installing via Composer is automation of autoload setup. If you install extension and PHPMailer library from archive files, you'll have to setup autoload paths manually., (*26)

Note: currently PHPMailer does not support namespaces (as its minimum requirements is php 5.0), but provides an SPL-compatible autoloader, and..., (*27)

the preferred way of loading the library - just require '/path/to/PHPMailerAutoload.php'; and everything should work, (*28)

For further information refer to PHPMailer documentation., (*29)

CONFIGURING

To use this extension, you should add some settings in your application configuration file. It may be like the following:, (*30)

return [
//....
    'components' => [
        'mailer' => [
            'class'            => 'gulltour\phpmailer\Mailer',
            'viewPath'         => '@common/mail',
            'useFileTransport' => false,
            'config'           => [
                'mailer'     => 'smtp',
                'host'       => 'smtp.yandex.ru',
                'port'       => '465',
                'smtpsecure' => 'ssl',
                'smtpauth'   => true,
                'username'   => 'mysmtplogin@example.ru',
                'password'   => 'mYsmTpPassword',
            ],
        ],
    ],
];

This is the tipical configuration for sending email via smtp. If you are familiar with PHPMailer, you can see that 'config' array holds settings, similar to corresponding PHPMailer properties. They will be populated when Mailer is initialized., (*31)

You can for example define, (*32)

'mailer' => 'mail' or, (*33)

'mailer' => 'sendmail', 'sendmail' => '/path/to/sendmail' - just like in PHPMailer., (*34)

You may also configure some default message settings for your application in 'messageConfig' array. They will be populated at the moment of message creation., (*35)

For example, you may predefine default contents of 'From' email field - so you will not have to set 'From' it every time when composing message:, (*36)

    ...
    'mailer' => [
        ...
        'messageConfig'    => [
         'from' => ['noreply@example.com' => 'My Example Site']
        ],
        ...
    ],
    ...

USAGE

Example of simple usage:, (*37)

Yii::$app->mailer->compose()
     ->setFrom(['noreply@example.com' => 'My Example Site'])
     ->setTo([$form->email => $form->name])
     ->setSubject($form->subject)
     ->setTextBody($form->text)
     ->send();

Example of sending html emails rendering view and layout (assumed that default 'From' is set in application configuration file):, (*38)

Yii::$app->mailer->compose('passwordResetToken', ['user'       => $user,
                                                'title'      => Yii::t('app', 'Password reset'),
                                                'htmlLayout' => 'layouts/html-reset'])
                ->setTo($this->email)
                ->setSubject(Yii::t('app', 'Password reset for ') . Yii::$app->params['siteName'])
                ->send();

Note: possibility to set 'htmlLayout' dynamically when composing message is not the native behavior of BaseMailer. This feature was introduced in this specific extension and is experimental., (*39)

For more examples of usage you may refer to documentation on Yii 2 BaseMailer and BaseMessage and PHPDoc blocks in the extension files., (*40)

Additionally some Wiki articles are available., (*41)

The Versions

13/11 2017

dev-master

9999999-dev https://github.com/weijiansdlx/yii2-phpmailer

Phpmailer extension for the Yii framework

  Sources   Download

BSD 3-Clause

The Requires

 

mail email yii2 mailer phpmailer

13/11 2017

1.0.2

1.0.2.0 https://github.com/weijiansdlx/yii2-phpmailer

Phpmailer extension for the Yii framework

  Sources   Download

BSD 3-Clause

The Requires

 

mail email yii2 mailer phpmailer

22/08 2017

1.0.1

1.0.1.0 https://github.com/weijiansdlx/yii2-phpmailer

Phpmailer extension for the Yii framework

  Sources   Download

BSD 3-Clause

The Requires

 

mail email yii2 mailer phpmailer

22/08 2017

1.0.0

1.0.0.0 https://github.com/weijiansdlx/yii2-phpmailer

Phpmailer extension for the Yii framework

  Sources   Download

BSD 3-Clause

The Requires

 

mail email yii2 mailer phpmailer