10 function wfSpecialEmailuser( $par ) {
11 global $wgRequest, $wgUser, $wgOut;
13 $action = $wgRequest->getVal( 'action' );
14 $target = isset($par) ?
$par : $wgRequest->getVal( 'target' );
15 $targetUser = EmailUserForm
::validateEmailTarget( $target );
17 if ( !( $targetUser instanceof User
) ) {
18 $wgOut->showErrorPage( $targetUser[0], $targetUser[1] );
22 $form = new EmailUserForm( $targetUser,
23 $wgRequest->getText( 'wpText' ),
24 $wgRequest->getText( 'wpSubject' ),
25 $wgRequest->getBool( 'wpCCMe' ) );
26 if ( $action == 'success' ) {
31 $error = EmailUserForm
::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
33 switch ( $error[0] ) {
34 case 'blockedemailuser':
35 $wgOut->blockedPage();
37 case 'actionthrottledtext':
38 $wgOut->rateLimited();
40 case 'sessionfailure':
44 $wgOut->showErrorPage( $error[0], $error[1] );
50 if ( "submit" == $action && $wgRequest->wasPosted() ) {
51 $result = $form->doSubmit();
53 if ( !is_null( $result ) ) {
54 $wgOut->addHTML( wfMsg( "usermailererror" ) .
55 ' ' . htmlspecialchars( $result->getMessage() ) );
57 $titleObj = SpecialPage
::getTitleFor( "Emailuser" );
58 $encTarget = wfUrlencode( $form->getTarget()->getName() );
59 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
67 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
68 * @ingroup SpecialPage
74 var $cc_me; // Whether user requested to be sent a separate copy of their email.
79 function EmailUserForm( $target, $text, $subject, $cc_me ) {
80 $this->target
= $target;
82 $this->subject
= $subject;
83 $this->cc_me
= $cc_me;
87 global $wgOut, $wgUser;
88 $skin = $wgUser->getSkin();
90 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
91 $wgOut->addWikiMsg( "emailpagetext" );
93 if ( $this->subject
=== "" ) {
94 $this->subject
= wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) );
97 $emf = wfMsg( "emailfrom" );
98 $senderLink = $skin->makeLinkObj(
99 $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) );
100 $emt = wfMsg( "emailto" );
101 $recipientLink = $skin->makeLinkObj(
102 $this->target
->getUserPage(), htmlspecialchars( $this->target
->getName() ) );
103 $emr = wfMsg( "emailsubject" );
104 $emm = wfMsg( "emailmessage" );
105 $ems = wfMsg( "emailsend" );
106 $emc = wfMsg( "emailccme" );
107 $encSubject = htmlspecialchars( $this->subject
);
109 $titleObj = SpecialPage
::getTitleFor( "Emailuser" );
110 $action = $titleObj->escapeLocalURL( "target=" .
111 urlencode( $this->target
->getName() ) . "&action=submit" );
112 $token = htmlspecialchars( $wgUser->editToken() );
115 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
116 <table border='0' id='mailheader'><tr>
117 <td align='right'>{$emf}:</td>
118 <td align='left'><strong>{$senderLink}</strong></td>
120 <td align='right'>{$emt}:</td>
121 <td align='left'><strong>{$recipientLink}</strong></td>
123 <td align='right'>{$emr}:</td>
125 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
129 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
130 <textarea id=\"wpText\" name=\"wpText\" rows='20' cols='80' style=\"width: 100%;\">" . htmlspecialchars( $this->text
) .
132 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
133 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
134 <input type='hidden' name='wpEditToken' value=\"$token\" />
140 * Really send a mail. Permissions should have been checked using
141 * EmailUserForm::getPermissionsError. It is probably also a good idea to
142 * check the edit token and ping limiter in advance.
144 function doSubmit() {
145 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
147 $to = new MailAddress( $this->target
);
148 $from = new MailAddress( $wgUser );
149 $subject = $this->subject
;
151 // Add a standard footer and trim up trailing newlines
152 $this->text
= rtrim($this->text
) . "\n\n---\n" . wfMsgExt( 'emailuserfooter',
153 array( 'content', 'parsemag' ), array( $from->name
, $to->name
) );
155 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text
) ) ) {
157 if( $wgUserEmailUseReplyTo ) {
158 // Put the generic wiki autogenerated address in the From:
159 // header and reserve the user for Reply-To.
161 // This is a bit ugly, but will serve to differentiate
162 // wiki-borne mails from direct mails and protects against
163 // SPF and bounce problems with some mailers (see below).
164 global $wgPasswordSender;
165 $mailFrom = new MailAddress( $wgPasswordSender );
168 // Put the sending user's e-mail address in the From: header.
170 // This is clean-looking and convenient, but has issues.
171 // One is that it doesn't as clearly differentiate the wiki mail
172 // from "directly" sent mails.
174 // Another is that some mailers (like sSMTP) will use the From
175 // address as the envelope sender as well. For open sites this
176 // can cause mails to be flunked for SPF violations (since the
177 // wiki server isn't an authorized sender for various users'
178 // domains) as well as creating a privacy issue as bounces
179 // containing the recipient's e-mail address may get sent to
185 $mailResult = UserMailer
::send( $to, $mailFrom, $subject, $this->text
, $replyTo );
187 if( WikiError
::isError( $mailResult ) ) {
192 // if the user requested a copy of this mail, do this now,
193 // unless they are emailing themselves, in which case one copy of the message is sufficient.
194 if ($this->cc_me
&& $to != $from) {
195 $cc_subject = wfMsg('emailccsubject', $this->target
->getName(), $subject);
196 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text
) ) ) {
197 $ccResult = UserMailer
::send( $from, $from, $cc_subject, $this->text
);
198 if( WikiError
::isError( $ccResult ) ) {
199 // At this stage, the user's CC mail has failed, but their
200 // original mail has succeeded. It's unlikely, but still, what to do?
201 // We can either show them an error, or we can say everything was fine,
202 // or we can say we sort of failed AND sort of succeeded. Of these options,
203 // simply saying there was an error is probably best.
209 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text
) );
215 function showSuccess( &$user = null ) {
218 if ( is_null($user) )
219 $user = $this->target
;
221 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
222 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
224 $wgOut->returnToMain( false, $user->getUserPage() );
227 function getTarget() {
228 return $this->target
;
231 static function validateEmailTarget ( $target ) {
232 global $wgEnableEmail, $wgEnableUserEmail;
234 if( !( $wgEnableEmail && $wgEnableUserEmail ) )
235 return array( "nosuchspecialpage", "nospecialpagetext" );
237 if ( "" == $target ) {
238 wfDebug( "Target is empty.\n" );
239 return array( "notargettitle", "notargettext" );
242 $nt = Title
::newFromURL( $target );
243 if ( is_null( $nt ) ) {
244 wfDebug( "Target is invalid title.\n" );
245 return array( "notargettitle", "notargettext" );
248 $nu = User
::newFromName( $nt->getText() );
249 if( is_null( $nu ) ||
!$nu->canReceiveEmail() ) {
250 wfDebug( "Target is invalid user or can't receive.\n" );
251 return array( "noemailtitle", "noemailtext" );
256 static function getPermissionsError ( $user, $editToken ) {
257 if( !$user->canSendEmail() ) {
258 wfDebug( "User can't send.\n" );
259 return array( "mailnologin", "mailnologintext" );
262 if( $user->isBlockedFromEmailuser() ) {
263 wfDebug( "User is blocked from sending e-mail.\n" );
264 return array( "blockedemailuser", "" );
267 if( $user->pingLimiter( 'emailuser' ) ) {
268 wfDebug( "Ping limiter triggered.\n" );
269 return array( 'actionthrottledtext', '' );
272 if( !$user->matchEditToken( $editToken ) ) {
273 wfDebug( "Matching edit token failed.\n" );
274 return array( 'sessionfailure', '' );
280 static function newFromURL( $target, $text, $subject, $cc_me )
282 $nt = Title
::newFromURL( $target );
283 $nu = User
::newFromName( $nt->getText() );
284 return new EmailUserForm( $nu, $text, $subject, $cc_me );