-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8827 from touhidurabir/i8734_main
#8734 Added bulk mail sending job class and optimized bulk mail sending
- Loading branch information
Showing
7 changed files
with
170 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @file classes/core/PKPQueueDatabaseConnector.php | ||
* | ||
* Copyright (c) 2014-2023 Simon Fraser University | ||
* Copyright (c) 2000-2023 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class PKPQueueDatabaseConnector | ||
* @ingroup core | ||
* | ||
* @brief Registers the database queue connector | ||
*/ | ||
|
||
namespace PKP\core; | ||
|
||
use Illuminate\Queue\Connectors\DatabaseConnector as IlluminateQueueDatabaseConnector; | ||
use Illuminate\Queue\DatabaseQueue; | ||
use PKP\config\Config; | ||
|
||
class PKPQueueDatabaseConnector extends IlluminateQueueDatabaseConnector | ||
{ | ||
/** | ||
* Establish a queue connection. | ||
* | ||
* @param array $config | ||
* @return \Illuminate\Contracts\Queue\Queue | ||
*/ | ||
public function connect(array $config) | ||
{ | ||
return new DatabaseQueue( | ||
$this->connections->connection($config['connection'] ?? null), | ||
$config['table'], | ||
Config::getVar('queues', 'default_queue', 'queue'), | ||
$config['retry_after'] ?? 60, | ||
$config['after_commit'] ?? null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/** | ||
* @file jobs/bulk/BulkEmailSender.php | ||
* | ||
* Copyright (c) 2014-2023 Simon Fraser University | ||
* Copyright (c) 2000-2023 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class BulkEmailSender | ||
* @ingroup jobs | ||
* | ||
* @brief Job to send bulk emails | ||
*/ | ||
|
||
namespace PKP\jobs\bulk; | ||
|
||
use APP\facades\Repo; | ||
use Illuminate\Bus\Batchable; | ||
use Illuminate\Support\Facades\Mail; | ||
use PKP\mail\Mailable; | ||
use PKP\jobs\BaseJob; | ||
|
||
class BulkEmailSender extends BaseJob | ||
{ | ||
use Batchable; | ||
|
||
/** | ||
* The user ids to send email | ||
*/ | ||
protected array $userIds; | ||
|
||
/** | ||
* The associated context id | ||
*/ | ||
protected int $contextId; | ||
|
||
/** | ||
* Mail subject | ||
*/ | ||
protected string $subject; | ||
|
||
/** | ||
* Mail body | ||
*/ | ||
protected string $body; | ||
|
||
/** | ||
* From email to send mail | ||
*/ | ||
protected object|array|string $fromEmail; | ||
|
||
/** | ||
* From name to send mail | ||
*/ | ||
protected mixed $fromName; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(array $userIds, int $contextId, string $subject, string $body, object|array|string $fromEmail, mixed $fromName) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->userIds = $userIds; | ||
$this->contextId = $contextId; | ||
$this->subject = $subject; | ||
$this->body = $body; | ||
$this->fromEmail = $fromEmail; | ||
$this->fromName = $fromName; | ||
} | ||
|
||
public function handle() | ||
{ | ||
$users = Repo::user() | ||
->getCollector() | ||
->filterByContextIds([$this->contextId]) | ||
->filterByUserIds($this->userIds) | ||
->getMany(); | ||
|
||
foreach ($users as $user) { | ||
$mailable = new Mailable(); | ||
$mailable | ||
->from($this->fromEmail, $this->fromName) | ||
->to($user->getEmail(), $user->getFullName()) | ||
->subject($this->subject) | ||
->body($this->body); | ||
|
||
Mail::send($mailable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.