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' ), $this->getUser()->getName() ),
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->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
120 $out->addHTML( $this->userForm( $this->mTarget
) );
124 $this->mTargetObj
= $ret;
126 $form = new HTMLForm( $this->getFormFields(), $this->getContext() );
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( $this->msg( 'emailpage' ) );
139 $result = $form->show();
141 if( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
142 $out->setPageTitle( $this->msg( '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.
222 protected function userForm( $name ) {
224 $string = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' ) ) .
225 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
226 Xml
::openElement( 'fieldset' ) .
227 Html
::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
228 Xml
::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
229 Xml
::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
230 Xml
::closeElement( 'fieldset' ) .
231 Xml
::closeElement( 'form' ) . "\n";
236 * Really send a mail. Permissions should have been checked using
237 * getPermissionsError(). It is probably also a good
238 * idea to check the edit token and ping limiter in advance.
240 * @return Mixed: Status object, or potentially a String on error
241 * or maybe even true on success if anything uses the EmailUser hook.
243 public static function submit( $data ) {
244 global $wgUser, $wgUserEmailUseReplyTo;
246 $target = self
::getTarget( $data['Target'] );
247 if( !$target instanceof User
) {
248 return wfMsgExt( $target . 'text', 'parse' );
250 $to = new MailAddress( $target );
251 $from = new MailAddress( $wgUser );
252 $subject = $data['Subject'];
253 $text = $data['Text'];
255 // Add a standard footer and trim up trailing newlines
256 $text = rtrim( $text ) . "\n\n-- \n";
259 array( 'content', 'parsemag' ),
260 array( $from->name
, $to->name
)
264 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
268 if( $wgUserEmailUseReplyTo ) {
269 // Put the generic wiki autogenerated address in the From:
270 // header and reserve the user for Reply-To.
272 // This is a bit ugly, but will serve to differentiate
273 // wiki-borne mails from direct mails and protects against
274 // SPF and bounce problems with some mailers (see below).
275 global $wgPasswordSender, $wgPasswordSenderName;
276 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
279 // Put the sending user's e-mail address in the From: header.
281 // This is clean-looking and convenient, but has issues.
282 // One is that it doesn't as clearly differentiate the wiki mail
283 // from "directly" sent mails.
285 // Another is that some mailers (like sSMTP) will use the From
286 // address as the envelope sender as well. For open sites this
287 // can cause mails to be flunked for SPF violations (since the
288 // wiki server isn't an authorized sender for various users'
289 // domains) as well as creating a privacy issue as bounces
290 // containing the recipient's e-mail address may get sent to
296 $status = UserMailer
::send( $to, $mailFrom, $subject, $text, $replyTo );
298 if( !$status->isGood() ) {
301 // if the user requested a copy of this mail, do this now,
302 // unless they are emailing themselves, in which case one
303 // copy of the message is sufficient.
304 if ( $data['CCMe'] && $to != $from ) {
310 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
311 $ccStatus = UserMailer
::send( $from, $from, $cc_subject, $text );
312 $status->merge( $ccStatus );
315 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );