forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp#8734 Added bulk mail sending job class and optimized bulk mail se…
…nding
- Loading branch information
1 parent
be10907
commit b955992
Showing
4 changed files
with
143 additions
and
52 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
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,96 @@ | ||
<?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\Support\Facades\Mail; | ||
use PKP\mail\Mailable; | ||
use PKP\jobs\BaseJob; | ||
|
||
class BulkEmailSender extends BaseJob | ||
{ | ||
/** | ||
* Number of emails to send in each job | ||
* | ||
* @var int | ||
*/ | ||
public const EMAILS_PER_JOB = 100; | ||
|
||
/** | ||
* 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); | ||
} | ||
} | ||
} |