navatech / yii2-email-manager
Yii2 Email Manager
Installs: 1 944
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 5
Type:yii2-extension
Requires
- php: >=5.6.0
- baibaratsky/yii2-serialized-attributes-behavior: ~1.1
- mailgun/mailgun-php: ^3.4.0
- navatech/yii2-navatech-base: ^1.0
- navatech/yii2-roxymce: ^2.0
- php-http/guzzle6-adapter: *
- react/event-loop: ~0.4
- yiisoft/yii2: ~2.0.4
- yiisoft/yii2-swiftmailer: ~2.0.0 | ~2.1.0
- yiisoft/yii2-twig: ^2.4.1
Suggests
- cboden/ratchet: to use loop instead of for-cycle in daemon
- navatech/yii2-setting: 1.0.*
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]);
}