MDL-44467 core: always send email from noreplyaddress
authorSimey Lameze <simey@moodle.com>
Tue, 4 Oct 2016 02:27:05 +0000 (10:27 +0800)
committerAdrian Greeve <adrian@moodle.com>
Fri, 28 Oct 2016 05:04:09 +0000 (13:04 +0800)
12 files changed:
admin/tool/messageinbound/classes/manager.php
enrol/paypal/ipn.php
lang/en/auth.php
lib/authlib.php
lib/classes/task/send_failed_login_notifications_task.php
lib/moodlelib.php
lib/tests/messagelib_test.php
message/tests/messagelib_test.php
mod/forum/lib.php
mod/forum/settings.php
mod/forum/upgrade.txt
user/edit.php

index a352f7c..fb0c048 100644 (file)
@@ -930,7 +930,7 @@ class manager {
         $userfrom->customheaders[] = 'In-Reply-To: ' . $messageid;
 
         // The message will be sent from the intended user.
-        $eventdata->userfrom            = \core_user::get_support_user();
+        $eventdata->userfrom            = \core_user::get_noreply_user();
         $eventdata->userto              = $USER;
         $eventdata->subject             = $this->get_reply_subject($this->currentmessagedata->envelope->subject);
         $eventdata->fullmessage         = get_string('invalidrecipientdescription', 'tool_messageinbound', $this->currentmessagedata);
index 809cdb7..76b25bc 100644 (file)
@@ -263,7 +263,7 @@ if (strlen($result) > 0) {
             $eventdata->modulename        = 'moodle';
             $eventdata->component         = 'enrol_paypal';
             $eventdata->name              = 'paypal_enrolment';
-            $eventdata->userfrom          = empty($teacher) ? core_user::get_support_user() : $teacher;
+            $eventdata->userfrom          = empty($teacher) ? core_user::get_noreply_user() : $teacher;
             $eventdata->userto            = $user;
             $eventdata->subject           = get_string("enrolmentnew", 'enrol', $shortname);
             $eventdata->fullmessage       = get_string('welcometocoursetext', '', $a);
index f540be7..0078fae 100644 (file)
@@ -71,6 +71,8 @@ $string['emailupdatemessage'] = 'Dear {$a->fullname},
 
 You have requested a change of your email address for your user account at {$a->site}. Please open the following URL in your browser in order to confirm this change.
 
+If you have any questions please contact support on: {$a->supportemail}
+
 {$a->url}';
 $string['emailupdatesuccess'] = 'Email address of user <em>{$a->fullname}</em> was successfully updated to <em>{$a->email}</em>.';
 $string['emailupdatetitle'] = 'Confirmation of email update at {$a->site}';
index 2d7405e..a6fa7a0 100644 (file)
@@ -762,8 +762,7 @@ function login_lock_account($user) {
         $oldforcelang = force_current_language($user->lang);
 
         $site = get_site();
-        $supportuser = core_user::get_support_user();
-
+        $noreplyuser = core_user::get_noreply_user();
         $data = new stdClass();
         $data->firstname = $user->firstname;
         $data->lastname  = $user->lastname;
@@ -777,7 +776,7 @@ function login_lock_account($user) {
 
         if ($message) {
             // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-            email_to_user($user, $supportuser, $subject, $message);
+            email_to_user($user, $noreplyuser, $subject, $message);
         }
 
         force_current_language($oldforcelang);
index a155bae..6f7e123 100644 (file)
@@ -174,7 +174,7 @@ class send_failed_login_notifications_task extends scheduled_task {
             mtrace('Emailing admins about '. $count .' failed login attempts');
             foreach ($recip as $admin) {
                 // Emailing the admins directly rather than putting these through the messaging system.
-                email_to_user($admin, \core_user::get_support_user(), $subject, $body);
+                email_to_user($admin, \core_user::get_noreply_user(), $subject, $body);
             }
         }
 
index b9c10ae..8253d0e 100644 (file)
@@ -5685,6 +5685,13 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '',
         return true;
     }
 
+    // Check from address and prevent emails to be sent from support email address.
+    $supportuser = core_user::get_support_user();
+    if ($from->email == $supportuser->email) {
+        debugging('Support user email address should not be used for email sending.', DEBUG_NORMAL);
+        return false;
+    }
+
     if (email_should_be_diverted($user->email)) {
         $subject = "[DIVERTED {$user->email}] $subject";
         $user = clone($user);
@@ -5739,28 +5746,31 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '',
     $temprecipients = array();
     $tempreplyto = array();
 
-    $supportuser = core_user::get_support_user();
-
     // Make up an email address for handling bounces.
     if (!empty($CFG->handlebounces)) {
         $modargs = 'B'.base64_encode(pack('V', $user->id)).substr(md5($user->email), 0, 16);
         $mail->Sender = generate_email_processing_address(0, $modargs);
     } else {
-        $mail->Sender = $supportuser->email;
+        $mail->Sender = $CFG->noreplyaddress;
     }
 
-    if (!empty($CFG->emailonlyfromnoreplyaddress)) {
+    // Email will be sent using no reply address.
+    if ($CFG->emailonlyfromnoreplyaddress == true) {
         $usetrueaddress = false;
         if (empty($replyto) && $from->maildisplay) {
             $replyto = $from->email;
             $replytoname = fullname($from);
         }
+    } else {
+        // Use user's email address (if allowed).
+        $usetrueaddress = true;
     }
 
     if (is_string($from)) { // So we can pass whatever we want if there is need.
         $mail->From     = $CFG->noreplyaddress;
         $mail->FromName = $from;
     } else if ($usetrueaddress and $from->maildisplay) {
+        // If noreplyaddress is set to false, use users email address as from address.
         $mail->From     = $from->email;
         $mail->FromName = fullname($from);
     } else {
@@ -6005,7 +6015,7 @@ function setnew_password_and_mail($user, $fasthash = false) {
 
     $site  = get_site();
 
-    $supportuser = core_user::get_support_user();
+    $noreplyuser = core_user::get_noreply_user();
 
     $newpassword = generate_password();
 
@@ -6024,7 +6034,7 @@ function setnew_password_and_mail($user, $fasthash = false) {
     $subject = format_string($site->fullname) .': '. (string)new lang_string('newusernewpasswordsubj', '', $a, $lang);
 
     // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-    return email_to_user($user, $supportuser, $subject, $message);
+    return email_to_user($user, $noreplyuser, $subject, $message);
 
 }
 
@@ -6038,7 +6048,7 @@ function reset_password_and_mail($user) {
     global $CFG;
 
     $site  = get_site();
-    $supportuser = core_user::get_support_user();
+    $noreplyuser = core_user::get_noreply_user();
 
     $userauth = get_auth_plugin($user->auth);
     if (!$userauth->can_reset_password() or !is_enabled_auth($user->auth)) {
@@ -6068,7 +6078,7 @@ function reset_password_and_mail($user) {
     unset_user_preference('create_password', $user); // Prevent cron from generating the password.
 
     // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-    return email_to_user($user, $supportuser, $subject, $message);
+    return email_to_user($user, $noreplyuser, $subject, $message);
 }
 
 /**
@@ -6081,7 +6091,7 @@ function send_confirmation_email($user) {
     global $CFG;
 
     $site = get_site();
-    $supportuser = core_user::get_support_user();
+    $noreplyuser = core_user::get_noreply_user();
 
     $data = new stdClass();
     $data->firstname = fullname($user);
@@ -6099,7 +6109,7 @@ function send_confirmation_email($user) {
     $user->mailformat = 1;  // Always send HTML version as well.
 
     // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-    return email_to_user($user, $supportuser, $subject, $message, $messagehtml);
+    return email_to_user($user, $noreplyuser, $subject, $message, $messagehtml);
 }
 
 /**
@@ -6113,7 +6123,7 @@ function send_password_change_confirmation_email($user, $resetrecord) {
     global $CFG;
 
     $site = get_site();
-    $supportuser = core_user::get_support_user();
+    $noreplyuser = core_user::get_noreply_user();
     $pwresetmins = isset($CFG->pwresettime) ? floor($CFG->pwresettime / MINSECS) : 30;
 
     $data = new stdClass();
@@ -6129,7 +6139,7 @@ function send_password_change_confirmation_email($user, $resetrecord) {
     $subject = get_string('emailresetconfirmationsubject', '', format_string($site->fullname));
 
     // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-    return email_to_user($user, $supportuser, $subject, $message);
+    return email_to_user($user, $noreplyuser, $subject, $message);
 
 }
 
@@ -6143,7 +6153,7 @@ function send_password_change_info($user) {
     global $CFG;
 
     $site = get_site();
-    $supportuser = core_user::get_support_user();
+    $noreplyuser = core_user::get_noreply_user();
     $systemcontext = context_system::instance();
 
     $data = new stdClass();
@@ -6158,7 +6168,7 @@ function send_password_change_info($user) {
         $message = get_string('emailpasswordchangeinfodisabled', '', $data);
         $subject = get_string('emailpasswordchangeinfosubject', '', format_string($site->fullname));
         // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-        return email_to_user($user, $supportuser, $subject, $message);
+        return email_to_user($user, $noreplyuser, $subject, $message);
     }
 
     if ($userauth->can_change_password() and $userauth->change_password_url()) {
@@ -6179,7 +6189,7 @@ function send_password_change_info($user) {
     }
 
     // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
-    return email_to_user($user, $supportuser, $subject, $message);
+    return email_to_user($user, $noreplyuser, $subject, $message);
 
 }
 
index ecc3dc5..f5641dd 100644 (file)
@@ -36,11 +36,11 @@ class core_messagelib_testcase extends advanced_testcase {
         set_config($disableprovidersetting, 1, 'message');
         $preferences = get_message_output_default_preferences();
         $this->assertTrue($preferences->$disableprovidersetting == 1);
-
+        $noreplyuser = core_user::get_noreply_user();
         $message = new stdClass();
         $message->component         = 'moodle';
         $message->name              = 'instantmessage';
-        $message->userfrom          = get_admin();
+        $message->userfrom          = $noreplyuser->email;
         $message->userto            = $this->getDataGenerator()->create_user();;
         $message->subject           = 'message subject 1';
         $message->fullmessage       = 'message body';
index bbdaf87..351a337 100644 (file)
@@ -160,7 +160,6 @@ class core_message_messagelib_testcase extends advanced_testcase {
         // Noreply user being valid user.
         core_user::reset_internal_users();
         $CFG->noreplyuserid = $user3->id;
-        $noreplyuser = core_user::get_noreply_user();
         list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts();
         $this->assertCount(0, $onlinecontacts);
         $this->assertCount(1, $offlinecontacts);
index ab4a4ae..5ec8dce 100644 (file)
@@ -853,8 +853,8 @@ function forum_cron() {
                     $eventdata->set_additional_content('email', $additionalcontent);
                 }
 
-                // If forum_replytouser is not set then send mail using the noreplyaddress.
-                if (empty($CFG->forum_replytouser)) {
+                // If replytouser is not set then send mail using the noreplyaddress.
+                if (empty($CFG->noreplyaddress)) {
                     $eventdata->userfrom = core_user::get_noreply_user();
                 }
 
index d2980f1..9dc0762 100644 (file)
@@ -29,9 +29,6 @@ if ($ADMIN->fulltree) {
     $settings->add(new admin_setting_configselect('forum_displaymode', get_string('displaymode', 'forum'),
                        get_string('configdisplaymode', 'forum'), FORUM_MODE_NESTED, forum_get_layout_modes()));
 
-    $settings->add(new admin_setting_configcheckbox('forum_replytouser', get_string('replytouser', 'forum'),
-                       get_string('configreplytouser', 'forum'), 1));
-
     // Less non-HTML characters than this is short
     $settings->add(new admin_setting_configtext('forum_shortpost', get_string('shortpost', 'forum'),
                        get_string('configshortpost', 'forum'), 300, PARAM_INT));
index 6705414..566a67f 100644 (file)
@@ -2,6 +2,8 @@ This files describes API changes in /mod/forum/*,
 information provided here is intended especially for developers.
 
 === 3.2 ===
+ * The setting $CFG->forum_replytouser has been deprecated in favour of a centralized noreplyaddress setting.
+   Please use $CFG->noreplyaddress setting instead.
  * The following functions have been finally deprecated and should no longer be used.
   - forum_count_unrated_posts
   - forum_tp_count_discussion_read_records
index 044522a..8a18c41 100644 (file)
@@ -258,17 +258,20 @@ if ($usernew = $userform->get_data()) {
         $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
         $tempuser->email = $usernew->preference_newemail;
 
+        $supportuser = core_user::get_support_user();
+
         $a = new stdClass();
         $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
         $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
         $a->fullname = fullname($tempuser, true);
+        $a->supportemail = $supportuser->email;
 
         $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
         $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
 
         // Email confirmation directly rather than using messaging so they will definitely get an email.
-        $supportuser = core_user::get_support_user();
-        if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
+        $noreplyuser = core_user::get_noreply_user();
+        if (!$mailresults = email_to_user($tempuser, $noreplyuser, $emailupdatetitle, $emailupdatemessage)) {
             die("could not send email!");
         }
     }