Skip to content

Commit

Permalink
Move email sending to task
Browse files Browse the repository at this point in the history
  • Loading branch information
mackensen committed Sep 3, 2023
1 parent ed4af53 commit dd4eca6
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 22 deletions.
98 changes: 98 additions & 0 deletions classes/task/email_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Defines adhoc task for sending emails.
* @package block_clampmail
* @copyright 2023 Lafayette College ITS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_clampmail\task;

/**
* Adhoc task for sending emails.
*
* @package block_clampmail
* @copyright 2023 Lafayette College ITS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class email_task extends \core\task\adhoc_task {
/**
* Return the name of the component.
*
* @return string The name of the component.
*/
public function get_component() {
return 'block_clampmail';
}

/**
* Execute the task
*/
public function execute() {
$data = $this->get_custom_data();

// Abort if no one to email.
if(empty($data->mailto)) {
mtrace("No users to email");
return;
}

// Send emails.
foreach ($data->mailto as $user) {
$success = email_to_user($user, $data->sender, $data->subject,
$data->messagetext, $data->messagehtml, $data->file, $data->filename, false, $data->replyto);
if (!$success) {
$this->failed($user, $data->sender, $data->subject);
}
}

if (!empty($data->receipt)) {
email_to_user($data->sender, $data->sender, $data->subject,
$data->messagetext, $data->messagehtml, $data->file, $data->filename, false, $data->replyto);
}

if (!empty($data->actualfile)) {
unlink($data->actualfile);
}
}

/**
* Notify the sender of a failed email.
*
* @param stdClass $to A {@link $USER} object
* @param stdClass $from A {@link $USER} object
* @param string $subject plain text subject line of the email
*/
protected function failed($to, $from, $subject) {
$message = new \core\message\message();
$message->component = 'block_clampmail';
$message->name = 'emaildeliveryfailure';
$message->userfrom = \core_user::get_noreply_user();
$message->userto = $from;
$message->subject = get_string('no_email_subject', 'block_clampmail');
$message->fullmessage = get_string('no_email_body', 'block_clampmail',
[
'subject' => $subject,
'firstname' => $to->firstname,
'lastname' => $to->lastname,
]
);
$message->fullmessageformat = FORMAT_PLAIN;
message_send($message);
}
}
2 changes: 1 addition & 1 deletion classes/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function get_users($courseid, $groupmode) {
$users = array();

$fieldsapi = \core_user\fields::for_userpic();
$fields = $fieldsapi->get_sql('u', false, '', '', false)->selects . ',u.mailformat, u.maildisplay, u.emailstop';
$fields = $fieldsapi->get_sql('u', false, '', '', false)->selects . ',u.username, u.mailformat, u.maildisplay, u.emailstop';

$usersfromdb = get_enrolled_users(
$context, '', 0, $fields, "", 0, 0, true
Expand Down
28 changes: 28 additions & 0 deletions db/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Message provider for block_clampmail.
*
* @package block_clampmail
* @copyright 2023 Collaborative Liberal Arts Moodle Project
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$messageproviders = array (
// Failed email delivery.
'emaildeliveryfailure' => array(),
);
37 changes: 19 additions & 18 deletions email.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,25 @@
$data->messagetext = format_text_email($data->message, $data->format);
$data->messagehtml = format_text($data->message, $data->format);

// Send emails.
foreach (explode(',', $data->mailto) as $userid) {
$success = email_to_user($everyone[$userid], $user, $subject,
$data->messagetext, $data->messagehtml, $file, $filename, false, $user->email);

if (!$success) {
$warnings[] = get_string("no_email", 'block_clampmail', $everyone[$userid]);
}
}

if ($data->receipt) {
email_to_user($USER, $user, $subject,
$data->messagetext, $data->messagehtml, $file, $filename, false, $user->email);
}

if (!empty($actualfile)) {
unlink($actualfile);
}
// Users to email.
$userstoemail = array_intersect_key($everyone, explode(',', $data->mailto));

// Prepare the task.
$task = new block_clampmail\task\email_task();
$task->set_custom_data(
array(
'actualfile' => $actualfile,
'file' => $file,
'filename' => $filename,
'mailto' => $userstoemail,
'messagetext' => $data->messagetext,
'messagehtml' => $data->messagehtml,
'replyto' => $user->email,
'sender' => $user,
'subject' => $subject
)
);
\core\task\manager::queue_adhoc_task($task);
}
}
$email = $data;
Expand Down
4 changes: 3 additions & 1 deletion lang/en/block_clampmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@
$string['maximumupload'] = 'Maximum attachment size';
$string['maximumupload_desc'] = 'Maximum attachment size for an individual email.';
$string['message'] = 'Message';
$string['messageprovider:emailfaileddelivery'] = 'Notification of failed email delivery.';
$string['missing_recipient'] = 'The recipient with the id {$a} is no longer enrolled in this course.';
$string['newsignature'] = 'New signature';
$string['no_alternates'] = 'No alternate emails found for {$a->fullname}. Continue to make one.';
$string['no_course'] = 'Invalid course with id of {$a}';
$string['no_drafts'] = 'You have no email drafts.';
$string['no_email'] = 'Could not email {$a->firstname} {$a->lastname}.';
$string['no_email_subject'] = 'Failed to send email';
$string['no_email_body'] = 'Could not send "{$a->subject}" to {$a->firstname} {$a->lastname}.';
$string['no_filter'] = 'No filter';
$string['no_group'] = 'Not in a group';
$string['no_log'] = 'You have no email history yet.';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2023081401; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2023090200; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2022041900; // Requires this Moodle version.
$plugin->component = 'block_clampmail'; // Full name of the plugin (used for diagnostics).
$plugin->maturity = MATURITY_STABLE; // The current stability of this version (used for diagnostics).
$plugin->release = 'v4.0.1'; // The release name of this version (used for diagnostics).
$plugin->release = 'v4.0.2'; // The release name of this version (used for diagnostics).

0 comments on commit dd4eca6

Please sign in to comment.