Skip to content

Commit

Permalink
Merge pull request #6 from BBrunekreeft/master
Browse files Browse the repository at this point in the history
Enable scheduling of emails
  • Loading branch information
nterms committed Jan 19, 2016
2 parents 5d54e1e + 5077f79 commit fda0bea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions MailQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function process()

$success = true;

$items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound)->all();
$items = Queue::find()->where(['and', ['sent_time' => NULL], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound)->all();
// dd($items);
if(!empty($items)) {
foreach($items as $item) {
Expand All @@ -115,4 +115,4 @@ public function process()

return $success;
}
}
}
8 changes: 5 additions & 3 deletions Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class Message extends \yii\swiftmailer\Message
{
/**
* Enqueue the message storing it in database.
*
*
* @param timestamp $time_to_send
* @return boolean true on success, false otherwise
*/
public function queue()
public function queue($time_to_send = time())
{
$item = new Queue();

Expand All @@ -34,6 +35,7 @@ public function queue()
$item->charset = $this->getCharset();
$item->subject = $this->getSubject();
$item->attempts = 0;
$item->time_to_send = date('Y-m-d H:i:s', $time_to_send);

$parts = $this->getSwiftMessage()->getChildren();
// if message has no parts, use message
Expand Down Expand Up @@ -61,4 +63,4 @@ public function queue()

return $item->save();
}
}
}
18 changes: 18 additions & 0 deletions migrations/m160118_081152_add_timetosend_column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use yii\db\Migration;

class m160118_081152_add_timetosend_column extends Migration
{
public function up()
{
$this->addColumn('mail_queue', 'time_to_send', $this->dateTime()->notNull());
$this->createIndex('IX_time_to_send', 'mail_queue', 'time_to_send');
}

public function down()
{
$this->dropIndex('IX_time_to_send', 'mail_queue');
$this->dropColumn('mail_queue', 'time_to_send');
}
}
4 changes: 3 additions & 1 deletion models/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @property integer $attempts
* @property integer $last_attempt_time
* @property integer $sent_time
* @property string $time_to_send
*/
class Queue extends ActiveRecord
{
Expand Down Expand Up @@ -58,6 +59,7 @@ public function rules()
{
return [
[['created_at', 'attempts', 'last_attempt_time', 'sent_time'], 'integer'],
[['time_to_send'], 'required'],
[['to', 'cc', 'bcc', 'subject', 'html_body', 'text_body', 'charset'], 'safe'],
];
}
Expand Down Expand Up @@ -106,4 +108,4 @@ public function toMessage()

return null;
}
}
}

0 comments on commit fda0bea

Please sign in to comment.