Set 'List-Help' header for watchlist emails
authorKunal Mehta <legoktm@gmail.com>
Tue, 7 Jul 2015 18:44:25 +0000 (11:44 -0700)
committerAddshore <addshorewiki@gmail.com>
Wed, 19 Aug 2015 09:37:45 +0000 (09:37 +0000)
Added an option to UserMailer::sendMail() to allow adding arbitrary
headers to emails.

Bug: T58315
Change-Id: I01a60430bf39f6bd104269b7246767f016eb9cd5

includes/mail/EmailNotification.php
includes/mail/UserMailer.php

index 6c9972c..01b6afa 100644 (file)
  * Visit the documentation pages under http://meta.wikipedia.com/Enotif
  */
 class EmailNotification {
+
+       /**
+        * Notification is due to user's user talk being edited
+        */
+       const USER_TALK = 'user_talk';
+       /**
+        * Notification is due to a watchlisted page being edited
+        */
+       const WATCHLIST = 'watchlist';
+       /**
+        * Notification because user is notified for all changes
+        */
+       const ALL_CHANGES = 'all_changes';
+
        protected $subject, $body, $replyto, $from;
        protected $timestamp, $summary, $minorEdit, $oldid, $composed_common, $pageStatus;
        protected $mailTargets = array();
@@ -236,7 +250,7 @@ class EmailNotification {
                                && $this->canSendUserTalkEmail( $editor, $title, $minorEdit )
                        ) {
                                $targetUser = User::newFromName( $title->getText() );
-                               $this->compose( $targetUser );
+                               $this->compose( $targetUser, self::USER_TALK );
                                $userTalkId = $targetUser->getId();
                        }
 
@@ -252,7 +266,7 @@ class EmailNotification {
                                                && !( $wgBlockDisablesLogin && $watchingUser->isBlocked() )
                                        ) {
                                                if ( Hooks::run( 'SendWatchlistEmailNotification', array( $watchingUser, $title, $this ) ) ) {
-                                                       $this->compose( $watchingUser );
+                                                       $this->compose( $watchingUser, self::WATCHLIST );
                                                }
                                        }
                                }
@@ -266,7 +280,7 @@ class EmailNotification {
                                continue;
                        }
                        $user = User::newFromName( $name );
-                       $this->compose( $user );
+                       $this->compose( $user, self::ALL_CHANGES );
                }
 
                $this->sendMails();
@@ -427,8 +441,9 @@ class EmailNotification {
         *
         * Call sendMails() to send any mails that were queued.
         * @param User $user
+        * @param string $source
         */
-       function compose( $user ) {
+       function compose( $user, $source ) {
                global $wgEnotifImpersonal;
 
                if ( !$this->composed_common ) {
@@ -438,7 +453,7 @@ class EmailNotification {
                if ( $wgEnotifImpersonal ) {
                        $this->mailTargets[] = MailAddress::newFromUser( $user );
                } else {
-                       $this->sendPersonalised( $user );
+                       $this->sendPersonalised( $user, $source );
                }
        }
 
@@ -458,10 +473,11 @@ class EmailNotification {
         * Returns true if the mail was sent successfully.
         *
         * @param User $watchingUser
+        * @param string $source
         * @return bool
         * @private
         */
-       function sendPersonalised( $watchingUser ) {
+       function sendPersonalised( $watchingUser, $source ) {
                global $wgContLang, $wgEnotifUseRealName;
                // From the PHP manual:
                //   Note: The to parameter cannot be an address in the form of
@@ -482,8 +498,14 @@ class EmailNotification {
                                $wgContLang->userTime( $this->timestamp, $watchingUser ) ),
                        $this->body );
 
+               $headers = array();
+               if ( $source === self::WATCHLIST ) {
+                       $headers['List-Help'] = 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Watchlist';
+               }
+
                return UserMailer::send( $to, $this->from, $this->subject, $body, array(
                        'replyTo' => $this->replyto,
+                       'headers' => $headers,
                ) );
        }
 
index bf4b96b..7c5f18d 100644 (file)
@@ -105,6 +105,7 @@ class UserMailer {
         * @param array $options:
         *              'replyTo' MailAddress
         *              'contentType' string default 'text/plain; charset=UTF-8'
+        *              'headers' array Extra headers to set
         *
         * Previous versions of this function had $replyto as the 5th argument and $contentType
         * as the 6th. These are still supported for backwards compatability, but deprecated.
@@ -116,9 +117,11 @@ class UserMailer {
        public static function send( $to, $from, $subject, $body, $options = array() ) {
                global $wgSMTP, $wgEnotifMaxRecips, $wgAdditionalMailParams, $wgAllowHTMLEmail;
                $contentType = 'text/plain; charset=UTF-8';
+               $headers = array();
                if ( is_array( $options ) ) {
                        $replyto = isset( $options['replyTo'] ) ? $options['replyTo'] : null;
                        $contentType = isset( $options['contentType'] ) ? $options['contentType'] : $contentType;
+                       $headers = isset( $options['headers'] ) ? $options['headers'] : $headers;
                } else {
                        // Old calling style
                        wfDeprecated( __METHOD__ . ' with $replyto as 5th parameter', '1.26' );