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() {
42 'default' => $wgUser->getSkin()->link(
43 $wgUser->getUserPage(),
44 htmlspecialchars( $wgUser->getName() )
46 'label-message' => 'emailfrom',
47 'id' => 'mw-emailuser-sender',
52 'default' => $wgUser->getSkin()->link(
53 $this->mTargetObj
->getUserPage(),
54 htmlspecialchars( $this->mTargetObj
->getName() )
56 'label-message' => 'emailto',
57 'id' => 'mw-emailuser-recipient',
61 'default' => $this->mTargetObj
->getName(),
65 'default' => wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) ),
66 'label-message' => 'emailsubject',
75 'label-message' => 'emailmessage',
80 'label-message' => 'emailccme',
81 'default' => $wgUser->getBoolOption( 'ccmeonemails' ),
86 public function execute( $par ) {
87 global $wgRequest, $wgOut, $wgUser;
90 $this->outputHeader();
91 $wgOut->addModuleStyles( 'mediawiki.special' );
92 $this->mTarget
= is_null( $par )
93 ?
$wgRequest->getVal( 'wpTarget', $wgRequest->getVal( 'target', '' ) )
95 // error out if sending user cannot do this
96 $error = self
::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
102 $wgOut->permissionRequired( 'sendemail' );
104 case 'blockedemailuser':
105 $wgOut->blockedPage();
107 case 'actionthrottledtext':
108 $wgOut->rateLimited();
111 case 'usermaildisabled':
112 $wgOut->showErrorPage( $error, "{$error}text" );
116 list( $title, $msg, $params ) = $error;
117 $wgOut->showErrorPage( $title, $msg, $params );
120 // Got a valid target user name? Else ask for one.
121 $ret = self
::getTarget( $this->mTarget
);
122 if( !$ret instanceof User
) {
123 if( $this->mTarget
!= '' ) {
124 $ret = ( $ret == 'notarget' ) ?
'emailnotarget' : ( $ret . 'text' );
125 $wgOut->addHTML( '<p class="error">' . wfMessage( $ret )->parse() . '</p>' );
127 $wgOut->addHTML( self
::userForm( $this->mTarget
) );
131 $this->mTargetObj
= $ret;
133 $form = new HTMLForm( $this->getFormFields() );
134 $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
135 $form->setSubmitText( wfMsg( 'emailsend' ) );
136 $form->setTitle( $this->getTitle() );
137 $form->setSubmitCallback( array( __CLASS__
, 'submit' ) );
138 $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
141 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
145 $wgOut->setPageTitle( wfMsg( 'emailpage' ) );
146 $result = $form->show();
148 if( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
149 $wgOut->setPageTitle( wfMsg( 'emailsent' ) );
150 $wgOut->addWikiMsg( 'emailsenttext' );
151 if ( $status->successCount
> 1 ) $wgOut->addWikiMsg( 'emailyougotcopy' );
152 } elseif ( $result instanceof Status
) {
153 if ( $status->successCount
== 0 ) {
154 $wgOut->setPageTitle( wfMsg( 'emailnotsent' ) );
155 $wgOut->addWikiMsg( $result->getWikiText( 'emailfailed' ) );
156 } elseif ( $status->failCount
) {
157 $wgOut->setPageTitle( wfMsg( 'emailsent' ) );
158 $wgOut->addWikiMsg( 'emailsenttext' );
159 $wgOut->addWikiMsg( $result->getWikiText( 'emailccfailed' ) );
162 $wgOut->returnToMain( false, $this->mTargetObj
->getUserPage() );
166 * Validate target User
168 * @param $target String: target user name
169 * @return User object on success or a string on error
171 public static function getTarget( $target ) {
172 if ( $target == '' ) {
173 wfDebug( "Target is empty.\n" );
177 $nu = User
::newFromName( $target );
178 if( !$nu instanceof User ||
!$nu->getId() ) {
179 wfDebug( "Target is invalid user.\n" );
181 } elseif ( !$nu->isEmailConfirmed() ) {
182 wfDebug( "User has no valid email.\n" );
184 } elseif ( !$nu->canReceiveEmail() ) {
185 wfDebug( "User does not allow user emails.\n" );
186 return 'nowikiemail';
193 * Check whether a user is allowed to send email
195 * @param $user User object
196 * @param $editToken String: edit token
197 * @return null on success or string on error
199 public static function getPermissionsError( $user, $editToken ) {
200 global $wgEnableEmail, $wgEnableUserEmail;
201 if( !$wgEnableEmail ||
!$wgEnableUserEmail ) {
202 return 'usermaildisabled';
205 if( !$user->isAllowed( 'sendemail' ) ) {
209 if( !$user->isEmailConfirmed() ) {
210 return 'mailnologin';
213 if( $user->isBlockedFromEmailuser() ) {
214 wfDebug( "User is blocked from sending e-mail.\n" );
215 return "blockedemailuser";
218 if( $user->pingLimiter( 'emailuser' ) ) {
219 wfDebug( "Ping limiter triggered.\n" );
220 return 'actionthrottledtext';
224 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
225 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
234 * Form to ask for target user name.
236 * @param $name String: user name submitted.
237 * @return String: form asking for user name.
239 static function userForm( $name ) {
240 $string = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => '', 'id' => 'askusername' ) ) .
241 Xml
::openElement( 'fieldset' ) .
242 Html
::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
243 Xml
::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
244 Xml
::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
245 Xml
::closeElement( 'fieldset' ) .
246 Xml
::closeElement( 'form' ) . "\n";
251 * Really send a mail. Permissions should have been checked using
252 * getPermissionsError(). It is probably also a good
253 * idea to check the edit token and ping limiter in advance.
255 * @return Mixed: Status object, or potentially a String on error
256 * or maybe even true on success if anything uses the EmailUser hook.
258 public static function submit( $data ) {
259 global $wgUser, $wgUserEmailUseReplyTo;
261 $target = self
::getTarget( $data['Target'] );
262 if( !$target instanceof User
) {
263 return wfMsgExt( $target . 'text', 'parse' );
265 $to = new MailAddress( $target );
266 $from = new MailAddress( $wgUser );
267 $subject = $data['Subject'];
268 $text = $data['Text'];
270 // Add a standard footer and trim up trailing newlines
271 $text = rtrim( $text ) . "\n\n-- \n";
274 array( 'content', 'parsemag' ),
275 array( $from->name
, $to->name
)
279 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
283 if( $wgUserEmailUseReplyTo ) {
284 // Put the generic wiki autogenerated address in the From:
285 // header and reserve the user for Reply-To.
287 // This is a bit ugly, but will serve to differentiate
288 // wiki-borne mails from direct mails and protects against
289 // SPF and bounce problems with some mailers (see below).
290 global $wgPasswordSender, $wgPasswordSenderName;
291 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
294 // Put the sending user's e-mail address in the From: header.
296 // This is clean-looking and convenient, but has issues.
297 // One is that it doesn't as clearly differentiate the wiki mail
298 // from "directly" sent mails.
300 // Another is that some mailers (like sSMTP) will use the From
301 // address as the envelope sender as well. For open sites this
302 // can cause mails to be flunked for SPF violations (since the
303 // wiki server isn't an authorized sender for various users'
304 // domains) as well as creating a privacy issue as bounces
305 // containing the recipient's e-mail address may get sent to
311 $status = UserMailer
::send( $to, $mailFrom, $subject, $text, $replyTo );
313 if( !$status->isGood() ) {
316 $status->successCount++
;
318 // if the user requested a copy of this mail, do this now,
319 // unless they are emailing themselves, in which case one
320 // copy of the message is sufficient.
321 if ( $data['CCMe'] && $to != $from ) {
327 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
328 $ccStatus = UserMailer
::send( $from, $from, $cc_subject, $text );
329 if ( $ccStatus->isGood() ) {
330 $ccStatus->successCount++
;
332 $ccStatus->failCount++
;
334 $status->merge( $ccStatus );
337 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );