Last active
June 25, 2018 05:49
-
-
Save michael-milette/a957554001cac4876138490aa8d72d33 to your computer and use it in GitHub Desktop.
Send email message to Moodle Administrators with optional attachments
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
<?php | |
// EmailMoodleAdmin 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. | |
// | |
// EmailMoodleAdmin 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/>. | |
/** | |
* Version information for EmailMoodleAdmin (also called Email Moodle Admin). | |
* | |
* @copyright 2015-2018 TNG Consulting Inc. - www.tngconsulting.ca | |
* @author Michael Milette | |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
* | |
* NOTE: You may need to modify the path of the "require(...);" line depending on where you put this script. | |
*/ | |
define('CLI_SCRIPT', true); | |
require(__DIR__.'/../../../config.php'); | |
// This is where the file location is set. Change location as needed. | |
$options = getopt('h', array('subject:', 'body:', 'attach:', 'alladmins', 'help', 'debug')); | |
// Display help. | |
if (isset($options['help']) || isset($options['h'])) { | |
$help = 'EmailMoodleAdmin v1.0 - by Michael Milette (https://www.tngconsulting.ca/) - June 2018' . PHP_EOL; | |
$help .= 'Purpose: Send an email to Moodle\'s administrators.' . PHP_EOL; | |
$help .= 'Example: php emailmoodleadmin.php --body "body.txt" --attach "error.log"' . PHP_EOL; | |
$help .= PHP_EOL; | |
$help .= 'Parameters:' . PHP_EOL; | |
$help .= PHP_EOL; | |
$help .= "--help|-h (optional) Display's this information." . PHP_EOL; | |
$help .= "--alladmins (optional) Sends to all admins. Default is just to main admin." . PHP_EOL; | |
$help .= "--subject (optional) Subject line of the message. Make sure to use quotes." . PHP_EOL; | |
$help .= "--body (required) Name of file containing the body of the message." . PHP_EOL; | |
$help .= "--attach (optional) Full path and filename of attachment." . PHP_EOL; | |
$help .= " Can only be located in tmp/temp or moodledata folder." . PHP_EOL; | |
$help .= "--debug (optional) Debug mode. Doesn't actually send emails." . PHP_EOL; | |
fwrite(STDERR, $help .PHP_EOL); | |
exit(1); | |
} | |
// Debug mode. | |
$debug = isset($options['debug']); | |
// All admins. | |
$alladmins = isset($options['alladmins']); | |
// Get the subject of the email. | |
$subject = empty($options['subject']) ? '[' . $SITE->shortname . ']' : $options['subject']; | |
// Get the content for the email body. | |
if (empty($options['body'])) { | |
echo "No body content to send."; | |
exit(1); | |
} | |
$message = @file_get_contents($options['body']); | |
// The purpose of this script is primarily to send utilitatian info (like logs) to administrators. | |
// Comment out the next line if you want to send rich HTML emails. | |
$message = '<pre>'.htmlspecialchars($message).'</pre>'; | |
// Determine if there is a file to attach. | |
$attach = empty($options['attach']) ? '' : $options['attach']; | |
if (empty($attach)) { // No attachment. | |
$fileLocation = ''; | |
$filename = ''; | |
} else { // Get the path and filename of the attachment. | |
// Standardize direction of slashes in hopes of a better match with moodledata folder. | |
$dataroot = str_replace('\\', '/', $CFG->dataroot); | |
$fileLocation = realpath($attach); | |
$fileLocation = str_replace('\\', '/', $fileLocation); | |
// If the file is relative to the moodledata folder. | |
if (stripos($fileLocation, $dataroot . '/') === 0) { | |
// Make path to find relative to moodledata. | |
$fileLocation = substr($fileLocation, strlen($CFG->dataroot . '/')); | |
} | |
$filename = basename($attach); | |
} | |
// Always send emails from the noreply user. | |
$emailFrom = core_user::get_noreply_user(); | |
// Get To: users. | |
if ($alladmins) { // Add Moodle Administrators. | |
$admins = get_admins(); | |
} else { // Only main Moodle Administrator. | |
$admins = [get_admin()]; | |
} | |
// Send email message to the desired Moodle administrators. | |
if (!empty($admins)) { // For each admin... | |
foreach ($admins as $admin) { | |
if ($debug) { // Debug mode - Does not actually send emails. | |
// Just display what we got. | |
echo "=== email_to_user:" . PHP_EOL; | |
print_r($admin); | |
print_r($emailFrom); | |
print_r("SUBJECT: $subject" . PHP_EOL); | |
print_r("TEXT MESSAGE: ". html_to_text($message) . PHP_EOL); | |
print_r("HTML MESSAGE: $message" . PHP_EOL); | |
print_r("FILE LOCATION: $fileLocation" . PHP_EOL); | |
print_r("FILENAME: $filename" . PHP_EOL.PHP_EOL); | |
} else { // Actually send the email. | |
email_to_user($admin, $emailFrom, $subject, html_to_text($message), $message, $fileLocation, $filename); | |
} | |
} | |
} | |
/* Function: makefakeuser() | |
* Purpose: Create a fake user object that we can use to send emails through email_to_user(). | |
* @param (string) $fullname - Full name of the user. | |
* @param (string) $email - Email address of the user. | |
* @return (object) - Moodle user object. | |
*/ | |
function makefakeuser($fullname, $email) { | |
$user = new stdClass(); | |
$user->email = $email; | |
$user->firstname = $fullname; | |
$user->lastname = ''; | |
$user->maildisplay = true; | |
$user->mailformat = 0; // 0 (zero) text-only emails, 1 (one) for HTML/Text emails. | |
$user->id = -99; | |
$user->firstnamephonetic = ''; | |
$user->lastnamephonetic = ''; | |
$user->middlename = ''; | |
$user->alternatename = ''; | |
return $user; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment