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' => $this->getSkin()->link(
43 $wgUser->getUserPage(),
44 htmlspecialchars( $wgUser->getName() )
46 'label-message' => 'emailfrom',
47 'id' => 'mw-emailuser-sender',
52 'default' => $this->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 $wgOut->returnToMain( false, $this->mTargetObj
->getUserPage() );
156 * Validate target User
158 * @param $target String: target user name
159 * @return User object on success or a string on error
161 public static function getTarget( $target ) {
162 if ( $target == '' ) {
163 wfDebug( "Target is empty.\n" );
167 $nu = User
::newFromName( $target );
168 if( !$nu instanceof User ||
!$nu->getId() ) {
169 wfDebug( "Target is invalid user.\n" );
171 } elseif ( !$nu->isEmailConfirmed() ) {
172 wfDebug( "User has no valid email.\n" );
174 } elseif ( !$nu->canReceiveEmail() ) {
175 wfDebug( "User does not allow user emails.\n" );
176 return 'nowikiemail';
183 * Check whether a user is allowed to send email
185 * @param $user User object
186 * @param $editToken String: edit token
187 * @return null on success or string on error
189 public static function getPermissionsError( $user, $editToken ) {
190 global $wgEnableEmail, $wgEnableUserEmail;
191 if( !$wgEnableEmail ||
!$wgEnableUserEmail ) {
192 return 'usermaildisabled';
195 if( !$user->isAllowed( 'sendemail' ) ) {
199 if( !$user->isEmailConfirmed() ) {
200 return 'mailnologin';
203 if( $user->isBlockedFromEmailuser() ) {
204 wfDebug( "User is blocked from sending e-mail.\n" );
205 return "blockedemailuser";
208 if( $user->pingLimiter( 'emailuser' ) ) {
209 wfDebug( "Ping limiter triggered.\n" );
210 return 'actionthrottledtext';
214 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
215 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
224 * Form to ask for target user name.
226 * @param $name String: user name submitted.
227 * @return String: form asking for user name.
229 static function userForm( $name ) {
230 $string = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => '', 'id' => 'askusername' ) ) .
231 Xml
::openElement( 'fieldset' ) .
232 Html
::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
233 Xml
::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
234 Xml
::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
235 Xml
::closeElement( 'fieldset' ) .
236 Xml
::closeElement( 'form' ) . "\n";
241 * Really send a mail. Permissions should have been checked using
242 * getPermissionsError(). It is probably also a good
243 * idea to check the edit token and ping limiter in advance.
245 * @return Mixed: Status object, or potentially a String on error
246 * or maybe even true on success if anything uses the EmailUser hook.
248 public static function submit( $data ) {
249 global $wgUser, $wgUserEmailUseReplyTo;
251 $target = self
::getTarget( $data['Target'] );
252 if( !$target instanceof User
) {
253 return wfMsgExt( $target . 'text', 'parse' );
255 $to = new MailAddress( $target );
256 $from = new MailAddress( $wgUser );
257 $subject = $data['Subject'];
258 $text = $data['Text'];
260 // Add a standard footer and trim up trailing newlines
261 $text = rtrim( $text ) . "\n\n-- \n";
264 array( 'content', 'parsemag' ),
265 array( $from->name
, $to->name
)
269 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
273 if( $wgUserEmailUseReplyTo ) {
274 // Put the generic wiki autogenerated address in the From:
275 // header and reserve the user for Reply-To.
277 // This is a bit ugly, but will serve to differentiate
278 // wiki-borne mails from direct mails and protects against
279 // SPF and bounce problems with some mailers (see below).
280 global $wgPasswordSender, $wgPasswordSenderName;
281 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
284 // Put the sending user's e-mail address in the From: header.
286 // This is clean-looking and convenient, but has issues.
287 // One is that it doesn't as clearly differentiate the wiki mail
288 // from "directly" sent mails.
290 // Another is that some mailers (like sSMTP) will use the From
291 // address as the envelope sender as well. For open sites this
292 // can cause mails to be flunked for SPF violations (since the
293 // wiki server isn't an authorized sender for various users'
294 // domains) as well as creating a privacy issue as bounces
295 // containing the recipient's e-mail address may get sent to
301 $status = UserMailer
::send( $to, $mailFrom, $subject, $text, $replyTo );
303 if( !$status->isGood() ) {
306 // if the user requested a copy of this mail, do this now,
307 // unless they are emailing themselves, in which case one
308 // copy of the message is sufficient.
309 if ( $data['CCMe'] && $to != $from ) {
315 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
316 $ccStatus = UserMailer
::send( $from, $from, $cc_subject, $text );
317 $status->merge( $ccStatus );
320 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );