Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/codexten/yii-mailqueue
Yii mailqueue for Yii 2.0 or higher
https://github.com/codexten/yii-mailqueue
codexten yii2
Last synced: 11 days ago
JSON representation
Yii mailqueue for Yii 2.0 or higher
- Host: GitHub
- URL: https://github.com/codexten/yii-mailqueue
- Owner: codexten
- Created: 2019-02-18T10:18:08.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-16T09:27:14.000Z (almost 6 years ago)
- Last Synced: 2024-05-05T19:43:35.889Z (8 months ago)
- Topics: codexten, yii2
- Language: PHP
- Size: 24.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yii-mailqueue
Yii mailqueue for Yii 2.0 or higherEmail queue component for yii2 that works with [yii2-swiftmailer](http://www.yiiframework.com/doc-2.0/ext-swiftmailer-index.html)
Installation
------------The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist nterms/yii2-mailqueue "*"
```or add
```
"nterms/yii2-mailqueue": "*"
```to the require section of your `composer.json` file.
Configuration
-------------
Once the extension is installed, add following code to your application configuration :```php
return [
//....
'components' => [
'mailqueue' => [
'class' => 'nterms\mailqueue\MailQueue',
'table' => '{{%mail_queue}}',
'mailsPerRound' => 10,
'maxAttempts' => 3,
],
],
];
```Following properties are available for customizing the mail queue behavior.
- `table`: Name of the database table to store emails added to the queue.
- `mailsPerRound`: Number of emails to send at a time.
- `maxAttempts`: Maximum number of sending attempts per email.Updating database schema
------------------------Apply the database migration to create the table required to store the mail queue messages. To do this,
add following code to `/config/console.php`:```php
return [
//....
'components' => [
'mailqueue' => [
'class' => 'nterms\mailqueue\MailQueue',
'table' => '{{%mail_queue}}',
],
],
];
```then run `yii migrate` command in command line:
```
php yii migrate/up --migrationPath=@vendor/nterms/yii2-mailqueue/migrations/
```Processing the mail queue
-------------------------Now calling `process()` on `Yii::$app->mailqueue` will process the message queue and send out the emails.
In one of your controller actions:```php
public function actionSend()
{
Yii::$app->mailqueue->process();
}```
Most preferably this could be a console command (eg: mail/send) which can be triggered by a CRON job.
Setting the CRON job
--------------------Set a CRON job to run console command:
```
*/10 * * * * php /var/www/html/myapp/yii mailqueue/process
```
Usage
-----You can then send an email to the queue as follows:
```php
Yii::$app->mailqueue->compose('contact/html')
->setFrom('[email protected]')
->setTo($form->email)
->setSubject($form->subject)
->setTextBody($form->body)
->queue();
```While `nterms\mailqueue\MailQueue` extends from `yii\swiftmailer\Mailer`, you can replace it with this extension by adding
`yii2-swiftmailer` configuations directly to `mailqueue` configurations as follows:```php
return [
//....
'components' => [
'mailqueue' => [
'class' => 'nterms\mailqueue\MailQueue',
'table' => '{{%mail_queue}}',
'mailsPerRound' => 10,
'maxAttempts' => 3,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
],
];
```And use following code for directly sending emails as you ususally do with `yii2-swiftmailer`:
```php
Yii::$app->mailqueue->compose('contact/html')
->setFrom('[email protected]')
->setTo($form->email)
->setSubject($form->subject)
->setTextBody($form->body)
->send();
```License
-------[MIT](LICENSE)