1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Test output mail configuration page
|
|
|
19 |
*
|
|
|
20 |
* @copyright 2019 Victor Deniz <victor@moodle.com>, based on Michael Milette <michael.milette@tngconsulting.ca> code
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
require_once(__DIR__ . '/../config.php');
|
|
|
25 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
26 |
|
|
|
27 |
// This is an admin page.
|
|
|
28 |
admin_externalpage_setup('testoutgoingmailconf');
|
|
|
29 |
|
|
|
30 |
$headingtitle = get_string('testoutgoingmailconf', 'admin');
|
|
|
31 |
$homeurl = new moodle_url('/admin/category.php', array('category' => 'email'));
|
|
|
32 |
$returnurl = new moodle_url('/admin/testoutgoingconf.php');
|
|
|
33 |
|
|
|
34 |
$form = new core_admin\form\testoutgoingmailconf_form(null, ['returnurl' => $returnurl]);
|
|
|
35 |
if ($form->is_cancelled()) {
|
|
|
36 |
redirect($homeurl);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
// Display the page.
|
|
|
40 |
echo $OUTPUT->header();
|
|
|
41 |
echo $OUTPUT->heading($headingtitle);
|
|
|
42 |
|
|
|
43 |
// Displaying noemailever warning.
|
|
|
44 |
if (!empty($CFG->noemailever)) {
|
|
|
45 |
$msg = get_string('noemaileverwarning', 'admin');
|
|
|
46 |
echo $OUTPUT->notification($msg, \core\output\notification::NOTIFY_ERROR);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$data = $form->get_data();
|
|
|
50 |
if ($data) {
|
|
|
51 |
$emailuser = new stdClass();
|
|
|
52 |
$emailuser->email = $data->recipient;
|
|
|
53 |
$emailuser->id = -99;
|
|
|
54 |
|
|
|
55 |
// Get the user who will send this email (From:).
|
|
|
56 |
$emailuserfrom = $USER;
|
|
|
57 |
if ($data->from) {
|
|
|
58 |
if (!$userfrom = \core_user::get_user_by_email($data->from)) {
|
|
|
59 |
$userfrom = \core_user::get_user_by_username($data->from);
|
|
|
60 |
}
|
|
|
61 |
if (!$userfrom && validate_email($data->from)) {
|
|
|
62 |
$dummyuser = \core_user::get_user(\core_user::NOREPLY_USER);
|
|
|
63 |
$dummyuser->id = -1;
|
|
|
64 |
$dummyuser->email = $data->from;
|
|
|
65 |
$dummyuser->firstname = $data->from;
|
|
|
66 |
$emailuserfrom = $dummyuser;
|
|
|
67 |
} else if ($userfrom) {
|
|
|
68 |
$emailuserfrom = $userfrom;
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// Get the date the email will be sent.
|
|
|
73 |
$timestamp = userdate(time(), get_string('strftimedatetimeaccurate', 'core_langconfig'));
|
|
|
74 |
|
|
|
75 |
// Build the email subject.
|
|
|
76 |
$subjectparams = new stdClass();
|
|
|
77 |
$subjectparams->site = format_string($SITE->fullname, true, ['context' => context_system::instance()]);
|
|
|
78 |
if (isset($data->additionalsubject)) {
|
|
|
79 |
$subjectparams->additional = format_string($data->additionalsubject);
|
|
|
80 |
}
|
|
|
81 |
$subjectparams->time = $timestamp;
|
|
|
82 |
|
|
|
83 |
$subject = get_string('testoutgoingmailconf_subject', 'admin', $subjectparams);
|
|
|
84 |
$messagetext = get_string('testoutgoingmailconf_message', 'admin', $timestamp);
|
|
|
85 |
|
|
|
86 |
// Manage Moodle debugging options.
|
|
|
87 |
$debuglevel = $CFG->debug;
|
|
|
88 |
$debugdisplay = $CFG->debugdisplay;
|
|
|
89 |
$debugsmtp = $CFG->debugsmtp ?? null; // This might not be set as it's optional.
|
|
|
90 |
$CFG->debugdisplay = true;
|
|
|
91 |
$CFG->debugsmtp = true;
|
|
|
92 |
$CFG->debug = 15;
|
|
|
93 |
|
|
|
94 |
// Send test email.
|
|
|
95 |
ob_start();
|
|
|
96 |
$success = email_to_user($emailuser, $emailuserfrom, $subject, $messagetext);
|
|
|
97 |
$smtplog = ob_get_contents();
|
|
|
98 |
ob_end_clean();
|
|
|
99 |
|
|
|
100 |
// Restore Moodle debugging options.
|
|
|
101 |
$CFG->debug = $debuglevel;
|
|
|
102 |
$CFG->debugdisplay = $debugdisplay;
|
|
|
103 |
|
|
|
104 |
// Restore the debugsmtp config, if it was set originally.
|
|
|
105 |
unset($CFG->debugsmtp);
|
|
|
106 |
if (!is_null($debugsmtp)) {
|
|
|
107 |
$CFG->debugsmtp = $debugsmtp;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if ($success) {
|
|
|
111 |
$msgparams = new stdClass();
|
|
|
112 |
$msgparams->fromemail = $emailuserfrom->email;
|
|
|
113 |
$msgparams->toemail = $emailuser->email;
|
|
|
114 |
$msg = get_string('testoutgoingmailconf_sentmail', 'admin', $msgparams);
|
|
|
115 |
$notificationtype = 'notifysuccess';
|
|
|
116 |
} else {
|
|
|
117 |
$notificationtype = 'notifyproblem';
|
|
|
118 |
// No communication between Moodle and the SMTP server - no error output.
|
|
|
119 |
if (trim($smtplog) == false) {
|
|
|
120 |
$msg = get_string('testoutgoingmailconf_errorcommunications', 'admin');
|
|
|
121 |
} else {
|
|
|
122 |
$msg = $smtplog;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
// Show result.
|
|
|
127 |
echo $OUTPUT->notification($msg, $notificationtype);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$form->display();
|
|
|
131 |
echo $OUTPUT->footer();
|