8 * Constructor for Special:Emailuser.
10 function wfSpecialEmailuser( $par ) {
11 global $wgRequest, $wgUser, $wgOut;
13 if ( !EmailUserForm
::userEmailEnabled() ) {
14 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
18 $action = $wgRequest->getVal( 'action' );
19 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
20 $targetUser = EmailUserForm
::validateEmailTarget( $target );
22 if ( !( $targetUser instanceof User
) ) {
23 $wgOut->showErrorPage( $targetUser.'title', $targetUser.'text' );
27 $form = new EmailUserForm( $targetUser,
28 $wgRequest->getText( 'wpText' ),
29 $wgRequest->getText( 'wpSubject' ),
30 $wgRequest->getBool( 'wpCCMe' ) );
31 if ( $action == 'success' ) {
36 $error = EmailUserForm
::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
39 case 'blockedemailuser':
40 $wgOut->blockedPage();
42 case 'actionthrottledtext':
43 $wgOut->rateLimited();
45 case 'sessionfailure':
49 $wgOut->showErrorPage( 'mailnologin', 'mailnologintext' );
54 if ( "submit" == $action && $wgRequest->wasPosted() ) {
55 $result = $form->doSubmit();
57 if ( !is_null( $result ) ) {
58 $wgOut->addHTML( wfMsg( "usermailererror" ) .
59 ' ' . htmlspecialchars( $result->getMessage() ) );
61 $titleObj = SpecialPage
::getTitleFor( "Emailuser" );
62 $encTarget = wfUrlencode( $form->getTarget()->getName() );
63 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
71 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
72 * @ingroup SpecialPage
78 var $cc_me; // Whether user requested to be sent a separate copy of their email.
83 function EmailUserForm( $target, $text, $subject, $cc_me ) {
84 $this->target
= $target;
86 $this->subject
= $subject;
87 $this->cc_me
= $cc_me;
91 global $wgOut, $wgUser;
92 $skin = $wgUser->getSkin();
94 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
95 $wgOut->addWikiMsg( "emailpagetext" );
97 if ( $this->subject
=== "" ) {
98 $this->subject
= wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) );
101 $titleObj = SpecialPage
::getTitleFor( "Emailuser" );
102 $action = $titleObj->getLocalURL( "target=" .
103 urlencode( $this->target
->getName() ) . "&action=submit" );
106 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) .
107 Xml
::openElement( 'fieldset' ) .
108 Xml
::element( 'legend', null, wfMsgExt( 'email-legend', 'parsemag' ) ) .
109 Xml
::openElement( 'table', array( 'class' => 'mw-emailuser-table' ) ) .
111 <td class='mw-label'>" .
112 Xml
::label( wfMsg( 'emailfrom' ), 'emailfrom' ) .
114 <td class='mw-input' id='mw-emailuser-sender'>" .
115 $skin->link( $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) ) .
119 <td class='mw-label'>" .
120 Xml
::label( wfMsg( 'emailto' ), 'emailto' ) .
122 <td class='mw-input' id='mw-emailuser-recipient'>" .
123 $skin->link( $this->target
->getUserPage(), htmlspecialchars( $this->target
->getName() ) ) .
127 <td class='mw-label'>" .
128 Xml
::label( wfMsg( 'emailsubject' ), 'wpSubject' ) .
130 <td class='mw-input'>" .
131 Xml
::input( 'wpSubject', 60, $this->subject
, array( 'type' => 'text', 'maxlength' => 200 ) ) .
135 <td class='mw-label'>" .
136 Xml
::label( wfMsg( 'emailmessage' ), 'wpText' ) .
138 <td class='mw-input'>" .
139 Xml
::textarea( 'wpText', $this->text
, 80, 20, array( 'id' => 'wpText' ) ) .
144 <td class='mw-input'>" .
145 Xml
::checkLabel( wfMsg( 'emailccme' ), 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) .
150 <td class='mw-submit'>" .
151 Xml
::submitButton( wfMsg( 'emailsend' ), array( 'name' => 'wpSend', 'accesskey' => 's' ) ) .
154 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
155 Xml
::closeElement( 'table' ) .
156 Xml
::closeElement( 'fieldset' ) .
157 Xml
::closeElement( 'form' )
162 * Really send a mail. Permissions should have been checked using
163 * EmailUserForm::getPermissionsError. It is probably also a good idea to
164 * check the edit token and ping limiter in advance.
166 function doSubmit() {
167 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
169 $to = new MailAddress( $this->target
);
170 $from = new MailAddress( $wgUser );
171 $subject = $this->subject
;
173 // Add a standard footer and trim up trailing newlines
174 $this->text
= rtrim($this->text
) . "\n\n---\n" . wfMsgExt( 'emailuserfooter',
175 array( 'content', 'parsemag' ), array( $from->name
, $to->name
) );
177 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text
) ) ) {
179 if( $wgUserEmailUseReplyTo ) {
180 // Put the generic wiki autogenerated address in the From:
181 // header and reserve the user for Reply-To.
183 // This is a bit ugly, but will serve to differentiate
184 // wiki-borne mails from direct mails and protects against
185 // SPF and bounce problems with some mailers (see below).
186 global $wgPasswordSender;
187 $mailFrom = new MailAddress( $wgPasswordSender );
190 // Put the sending user's e-mail address in the From: header.
192 // This is clean-looking and convenient, but has issues.
193 // One is that it doesn't as clearly differentiate the wiki mail
194 // from "directly" sent mails.
196 // Another is that some mailers (like sSMTP) will use the From
197 // address as the envelope sender as well. For open sites this
198 // can cause mails to be flunked for SPF violations (since the
199 // wiki server isn't an authorized sender for various users'
200 // domains) as well as creating a privacy issue as bounces
201 // containing the recipient's e-mail address may get sent to
207 $mailResult = UserMailer
::send( $to, $mailFrom, $subject, $this->text
, $replyTo );
209 if( WikiError
::isError( $mailResult ) ) {
214 // if the user requested a copy of this mail, do this now,
215 // unless they are emailing themselves, in which case one copy of the message is sufficient.
216 if ($this->cc_me
&& $to != $from) {
217 $cc_subject = wfMsg('emailccsubject', $this->target
->getName(), $subject);
218 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text
) ) ) {
219 $ccResult = UserMailer
::send( $from, $from, $cc_subject, $this->text
);
220 if( WikiError
::isError( $ccResult ) ) {
221 // At this stage, the user's CC mail has failed, but their
222 // original mail has succeeded. It's unlikely, but still, what to do?
223 // We can either show them an error, or we can say everything was fine,
224 // or we can say we sort of failed AND sort of succeeded. Of these options,
225 // simply saying there was an error is probably best.
231 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text
) );
237 function showSuccess( &$user = null ) {
240 if ( is_null($user) )
241 $user = $this->target
;
243 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
244 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
246 $wgOut->returnToMain( false, $user->getUserPage() );
249 function getTarget() {
250 return $this->target
;
253 static function userEmailEnabled() {
254 global $wgEnableEmail, $wgEnableUserEmail;
255 return $wgEnableEmail && $wgEnableUserEmail;
258 static function validateEmailTarget ( $target ) {
259 if ( "" == $target ) {
260 wfDebug( "Target is empty.\n" );
264 $nt = Title
::newFromURL( $target );
265 if ( is_null( $nt ) ) {
266 wfDebug( "Target is invalid title.\n" );
270 $nu = User
::newFromName( $nt->getText() );
271 if( is_null( $nu ) ||
!$nu->canReceiveEmail() ) {
272 wfDebug( "Target is invalid user or can't receive.\n" );
278 static function getPermissionsError ( $user, $editToken ) {
279 if( !$user->canSendEmail() ) {
280 wfDebug( "User can't send.\n" );
281 return "mailnologin";
284 if( $user->isBlockedFromEmailuser() ) {
285 wfDebug( "User is blocked from sending e-mail.\n" );
286 return "blockedemailuser";
289 if( $user->pingLimiter( 'emailuser' ) ) {
290 wfDebug( "Ping limiter triggered.\n" );
291 return 'actionthrottledtext';
294 if( !$user->matchEditToken( $editToken ) ) {
295 wfDebug( "Matching edit token failed.\n" );
296 return 'sessionfailure';
302 static function newFromURL( $target, $text, $subject, $cc_me )
304 $nt = Title
::newFromURL( $target );
305 $nu = User
::newFromName( $nt->getText() );
306 return new EmailUserForm( $nu, $text, $subject, $cc_me );