3 * Implements Special:Emailuser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page that allows users to send e-mails to other users
27 * @ingroup SpecialPage
29 class SpecialEmailUser
extends UnlistedSpecialPage
{
32 public function __construct() {
33 parent
::__construct( 'Emailuser' );
36 protected function getFormFields() {
41 'default' => Linker
::link(
42 $this->getUser()->getUserPage(),
43 htmlspecialchars( $this->getUser()->getName() )
45 'label-message' => 'emailfrom',
46 'id' => 'mw-emailuser-sender',
51 'default' => Linker
::link(
52 $this->mTargetObj
->getUserPage(),
53 htmlspecialchars( $this->mTargetObj
->getName() )
55 'label-message' => 'emailto',
56 'id' => 'mw-emailuser-recipient',
60 'default' => $this->mTargetObj
->getName(),
64 'default' => wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) ),
65 'label-message' => 'emailsubject',
74 'label-message' => 'emailmessage',
79 'label-message' => 'emailccme',
80 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
85 public function execute( $par ) {
87 $this->outputHeader();
88 $out = $this->getOutput();
89 $out->addModuleStyles( 'mediawiki.special' );
90 $this->mTarget
= is_null( $par )
91 ?
$this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
93 // error out if sending user cannot do this
94 $error = self
::getPermissionsError( $this->getUser(), $this->getRequest()->getVal( 'wpEditToken' ) );
100 throw new PermissionsError( 'sendemail' );
101 case 'blockedemailuser':
102 throw new UserBlockedError( $this->getUser()->mBlock
);
103 case 'actionthrottledtext':
104 throw new ThrottledError
;
106 case 'usermaildisabled':
107 throw new ErrorPageError( $error, "{$error}text" );
110 list( $title, $msg, $params ) = $error;
111 throw new ErrorPageError( $title, $msg, $params );
113 // Got a valid target user name? Else ask for one.
114 $ret = self
::getTarget( $this->mTarget
);
115 if( !$ret instanceof User
) {
116 if( $this->mTarget
!= '' ) {
117 $ret = ( $ret == 'notarget' ) ?
'emailnotarget' : ( $ret . 'text' );
118 $out->addHTML( '<p class="error">' . wfMessage( $ret )->parse() . '</p>' );
120 $out->addHTML( self
::userForm( $this->mTarget
) );
124 $this->mTargetObj
= $ret;
126 $form = new HTMLForm( $this->getFormFields() );
127 $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
128 $form->setSubmitText( wfMsg( 'emailsend' ) );
129 $form->setTitle( $this->getTitle() );
130 $form->setSubmitCallback( array( __CLASS__
, 'submit' ) );
131 $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
134 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
138 $out->setPageTitle( wfMsg( 'emailpage' ) );
139 $result = $form->show();
141 if( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
142 $out->setPageTitle( wfMsg( 'emailsent' ) );
143 $out->addWikiMsg( 'emailsenttext' );
144 $out->returnToMain( false, $this->mTargetObj
->getUserPage() );
149 * Validate target User
151 * @param $target String: target user name
152 * @return User object on success or a string on error
154 public static function getTarget( $target ) {
155 if ( $target == '' ) {
156 wfDebug( "Target is empty.\n" );
160 $nu = User
::newFromName( $target );
161 if( !$nu instanceof User ||
!$nu->getId() ) {
162 wfDebug( "Target is invalid user.\n" );
164 } elseif ( !$nu->isEmailConfirmed() ) {
165 wfDebug( "User has no valid email.\n" );
167 } elseif ( !$nu->canReceiveEmail() ) {
168 wfDebug( "User does not allow user emails.\n" );
169 return 'nowikiemail';
176 * Check whether a user is allowed to send email
178 * @param $user User object
179 * @param $editToken String: edit token
180 * @return null on success or string on error
182 public static function getPermissionsError( $user, $editToken ) {
183 global $wgEnableEmail, $wgEnableUserEmail;
184 if( !$wgEnableEmail ||
!$wgEnableUserEmail ) {
185 return 'usermaildisabled';
188 if( !$user->isAllowed( 'sendemail' ) ) {
192 if( !$user->isEmailConfirmed() ) {
193 return 'mailnologin';
196 if( $user->isBlockedFromEmailuser() ) {
197 wfDebug( "User is blocked from sending e-mail.\n" );
198 return "blockedemailuser";
201 if( $user->pingLimiter( 'emailuser' ) ) {
202 wfDebug( "Ping limiter triggered.\n" );
203 return 'actionthrottledtext';
207 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
208 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
217 * Form to ask for target user name.
219 * @param $name String: user name submitted.
220 * @return String: form asking for user name.
223 function userForm( $name ) {
225 $string = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' ) ) .
226 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
227 Xml
::openElement( 'fieldset' ) .
228 Html
::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
229 Xml
::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
230 Xml
::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
231 Xml
::closeElement( 'fieldset' ) .
232 Xml
::closeElement( 'form' ) . "\n";
237 * Really send a mail. Permissions should have been checked using
238 * getPermissionsError(). It is probably also a good
239 * idea to check the edit token and ping limiter in advance.
241 * @return Mixed: Status object, or potentially a String on error
242 * or maybe even true on success if anything uses the EmailUser hook.
244 public static function submit( $data ) {
245 global $wgUser, $wgUserEmailUseReplyTo;
247 $target = self
::getTarget( $data['Target'] );
248 if( !$target instanceof User
) {
249 return wfMsgExt( $target . 'text', 'parse' );
251 $to = new MailAddress( $target );
252 $from = new MailAddress( $wgUser );
253 $subject = $data['Subject'];
254 $text = $data['Text'];
256 // Add a standard footer and trim up trailing newlines
257 $text = rtrim( $text ) . "\n\n-- \n";
260 array( 'content', 'parsemag' ),
261 array( $from->name
, $to->name
)
265 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
269 if( $wgUserEmailUseReplyTo ) {
270 // Put the generic wiki autogenerated address in the From:
271 // header and reserve the user for Reply-To.
273 // This is a bit ugly, but will serve to differentiate
274 // wiki-borne mails from direct mails and protects against
275 // SPF and bounce problems with some mailers (see below).
276 global $wgPasswordSender, $wgPasswordSenderName;
277 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
280 // Put the sending user's e-mail address in the From: header.
282 // This is clean-looking and convenient, but has issues.
283 // One is that it doesn't as clearly differentiate the wiki mail
284 // from "directly" sent mails.
286 // Another is that some mailers (like sSMTP) will use the From
287 // address as the envelope sender as well. For open sites this
288 // can cause mails to be flunked for SPF violations (since the
289 // wiki server isn't an authorized sender for various users'
290 // domains) as well as creating a privacy issue as bounces
291 // containing the recipient's e-mail address may get sent to
297 $status = UserMailer
::send( $to, $mailFrom, $subject, $text, $replyTo );
299 if( !$status->isGood() ) {
302 // if the user requested a copy of this mail, do this now,
303 // unless they are emailing themselves, in which case one
304 // copy of the message is sufficient.
305 if ( $data['CCMe'] && $to != $from ) {
311 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
312 $ccStatus = UserMailer
::send( $from, $from, $cc_subject, $text );
313 $status->merge( $ccStatus );
316 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );