navatech/yii2-email-manager

This package is abandoned and no longer maintained. The author suggests using the phuongdev89/yii2-email-manager package instead.

Yii2 Email Manager

Installs: 1 944

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 4

Forks: 5

Type:yii2-extension

1.2.0 2021-04-08 08:04 UTC

This package is auto-updated.

Last update: 2023-01-09 17:09:55 UTC


README

Composer

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist navatech/yii2-email-manager "@dev"

or add

"navatech/yii2-email-manager": "@dev"

to the require section of your composer.json file.

Migration

Migration run

php yii migrate --migrationPath=@vendor/navatech/yii2-email-manager/src/migrations

Configuration

Simple configuration:

    'components' => [
        'mailer'        => [
            'class'            => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'host'             => 'smtp.gmail.com',
            'username'         => 'test@gmail.com',
            'password'         => '12345678',
            'port'             => '587',
            'encryption'       => 'TLS',
        ],
        'emailManager' => [
            'class' => '\navatech\email\EmailManager',
            'defaultTransport' => 'yiiMailer',
            'resendAfter'      => 5,//resend after 5 mins if stuck
            'tryTime'          => 3,//max try time resend
            'transports' => [
                'yiiMailer' => [
                    'class' => '\navatech\email\transports\YiiMailer',
                ],
                /*
                'mailGun' => [ //Not required
                    'class'  => '\navatech\email\transports\MailGun',
                    'apiKey' => 'xxx',
                    'domain' => 'our-domain.net',
                ],
                */
            ],
        ],
    ],
    'modules' => [
        'mailer'   => [
            'class'         => 'navatech\email\Module',
            'cleanAfter'    => 30//clean after days
        ],
    ]

Advanced config

First you need navatech/yii2-setting installed, create 5 records on Setting module:

  • smtp_host (value: smtp.gmail.com)
  • smtp_user (value: test@gmail.com)
  • smtp_password (value: 12345678)
  • smtp_port (value: 587)
  • smtp_encryption (value: TLS)
    'components' => [
        'mailer'        => [
            'class'            => '\navatech\email\swiftmailer\Mailer',
        ],
        'emailManager'  => [
            'class'            => '\navatech\email\components\EmailManager',
            'defaultTransport' => 'yiiMailer',
            'resendAfter'      => 5,//resend after 5 mins if stuck
            'tryTime'          => 3,//max try time resend
            'transports'       => [
                'yiiMailer' => [
                    'class' => '\navatech\email\transports\YiiMailer',
                ],
                /*
                'mailGun' => [
                    'class'  => '\navatech\email\transports\MailGun',
                    'apiKey' => 'xxx',
                    'domain' => 'our-domain.net',
                ],
                */
            ],
        ],
    ]
    'modules' => [
        'mailer'   => [
            'class'         => 'navatech\email\Module',
            'cleanAfter'    => 30//clean after days
      ],
    ]

Add command to the list of the available commands. Put it into console app configuration:

    'controllerMap' => [
        'email' => '\navatech\email\commands\EmailController',
    ],

Add email sending daemon into crontab, can be via lockrun or run-one utils:

    */5 * * * * php /your/site/path/yii email/spool-daemon

OR, if you will use cboden/ratchet

    */5 * * * * php /your/site/path/yii email/loop-daemon

Usage

##Backend Access this url:

http://backend.yourdomain.com/mailer

or

http://backend.yourdomain.com/index.php?r=mailer

##Simple usage

    // obtain component instance
    $emailManager = EmailManager::getInstance();
    // direct send via default transport
    $emailManager->send('from@example.com', 'to@example.com', 'test subject', 'test email');
    // queue send via default transport
    $emailManager->queue('from@example.com', 'to@example.com', 'test subject', 'test email');
    // direct send via selected transport
    $emailManager->transports['mailGun']->send('from@example.com', 'to@example.com', 'test subject', 'test email');

##Advanced usage Create a shortcut name welcome_email. Example:

Welcome {{fullname}},
Thanks for registered at {{url}}.
Your username: <b>{{username}}</b>
Your phone: <b>{{phone}}</b>

Send/Queue welcome email when done:

    // use shortcuts
    $user = new User();
    $user->fullname = "Test ABC";
    $user->username = "testabc";
    $user->email = "test@gmail.com";
    $user->phone = "0123456789";
    ...
    if($user->save()) {
        EmailTemplate::findByShortcut('welcome_email')->queue($user->email, ['fullname' => $user->fullname, 'username' => $user->username, 'ur' => 'http://domain.com', 'phone' => $user->phone]);
    }