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 $emf = wfMsg( "emailfrom" );
102 $senderLink = $skin->makeLinkObj(
103 $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) );
104 $emt = wfMsg( "emailto" );
105 $recipientLink = $skin->makeLinkObj(
106 $this->target
->getUserPage(), htmlspecialchars( $this->target
->getName() ) );
107 $emr = wfMsg( "emailsubject" );
108 $emm = wfMsg( "emailmessage" );
109 $ems = wfMsg( "emailsend" );
110 $emc = wfMsg( "emailccme" );
111 $encSubject = htmlspecialchars( $this->subject
);
113 $titleObj = SpecialPage
::getTitleFor( "Emailuser" );
114 $action = $titleObj->escapeLocalURL( "target=" .
115 urlencode( $this->target
->getName() ) . "&action=submit" );
116 $token = htmlspecialchars( $wgUser->editToken() );
119 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
120 <table border='0' id='mailheader'><tr>
121 <td align='right'>{$emf}:</td>
122 <td align='left'><strong>{$senderLink}</strong></td>
124 <td align='right'>{$emt}:</td>
125 <td align='left'><strong>{$recipientLink}</strong></td>
127 <td align='right'>{$emr}:</td>
129 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
133 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
134 <textarea id=\"wpText\" name=\"wpText\" rows='20' cols='80' style=\"width: 100%;\">" . htmlspecialchars( $this->text
) .
136 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
137 <input type='submit' name=\"wpSend\" value=\"{$ems}\" accesskey=\"s\" />
138 <input type='hidden' name='wpEditToken' value=\"$token\" />
144 * Really send a mail. Permissions should have been checked using
145 * EmailUserForm::getPermissionsError. It is probably also a good idea to
146 * check the edit token and ping limiter in advance.
148 function doSubmit() {
149 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
151 $to = new MailAddress( $this->target
);
152 $from = new MailAddress( $wgUser );
153 $subject = $this->subject
;
155 // Add a standard footer and trim up trailing newlines
156 $this->text
= rtrim($this->text
) . "\n\n---\n" . wfMsgExt( 'emailuserfooter',
157 array( 'content', 'parsemag' ), array( $from->name
, $to->name
) );
159 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text
) ) ) {
161 if( $wgUserEmailUseReplyTo ) {
162 // Put the generic wiki autogenerated address in the From:
163 // header and reserve the user for Reply-To.
165 // This is a bit ugly, but will serve to differentiate
166 // wiki-borne mails from direct mails and protects against
167 // SPF and bounce problems with some mailers (see below).
168 global $wgPasswordSender;
169 $mailFrom = new MailAddress( $wgPasswordSender );
172 // Put the sending user's e-mail address in the From: header.
174 // This is clean-looking and convenient, but has issues.
175 // One is that it doesn't as clearly differentiate the wiki mail
176 // from "directly" sent mails.
178 // Another is that some mailers (like sSMTP) will use the From
179 // address as the envelope sender as well. For open sites this
180 // can cause mails to be flunked for SPF violations (since the
181 // wiki server isn't an authorized sender for various users'
182 // domains) as well as creating a privacy issue as bounces
183 // containing the recipient's e-mail address may get sent to
189 $mailResult = UserMailer
::send( $to, $mailFrom, $subject, $this->text
, $replyTo );
191 if( WikiError
::isError( $mailResult ) ) {
196 // if the user requested a copy of this mail, do this now,
197 // unless they are emailing themselves, in which case one copy of the message is sufficient.
198 if ($this->cc_me
&& $to != $from) {
199 $cc_subject = wfMsg('emailccsubject', $this->target
->getName(), $subject);
200 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text
) ) ) {
201 $ccResult = UserMailer
::send( $from, $from, $cc_subject, $this->text
);
202 if( WikiError
::isError( $ccResult ) ) {
203 // At this stage, the user's CC mail has failed, but their
204 // original mail has succeeded. It's unlikely, but still, what to do?
205 // We can either show them an error, or we can say everything was fine,
206 // or we can say we sort of failed AND sort of succeeded. Of these options,
207 // simply saying there was an error is probably best.
213 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text
) );
219 function showSuccess( &$user = null ) {
222 if ( is_null($user) )
223 $user = $this->target
;
225 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
226 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
228 $wgOut->returnToMain( false, $user->getUserPage() );
231 function getTarget() {
232 return $this->target
;
235 static function userEmailEnabled() {
236 global $wgEnableEmail, $wgEnableUserEmail;
237 return $wgEnableEmail && $wgEnableUserEmail;
240 static function validateEmailTarget ( $target ) {
241 if ( "" == $target ) {
242 wfDebug( "Target is empty.\n" );
246 $nt = Title
::newFromURL( $target );
247 if ( is_null( $nt ) ) {
248 wfDebug( "Target is invalid title.\n" );
252 $nu = User
::newFromName( $nt->getText() );
253 if( is_null( $nu ) ||
!$nu->canReceiveEmail() ) {
254 wfDebug( "Target is invalid user or can't receive.\n" );
260 static function getPermissionsError ( $user, $editToken ) {
261 if( !$user->canSendEmail() ) {
262 wfDebug( "User can't send.\n" );
263 return "mailnologin";
266 if( $user->isBlockedFromEmailuser() ) {
267 wfDebug( "User is blocked from sending e-mail.\n" );
268 return "blockedemailuser";
271 if( $user->pingLimiter( 'emailuser' ) ) {
272 wfDebug( "Ping limiter triggered.\n" );
273 return 'actionthrottledtext';
276 if( !$user->matchEditToken( $editToken ) ) {
277 wfDebug( "Matching edit token failed.\n" );
278 return 'sessionfailure';
284 static function newFromURL( $target, $text, $subject, $cc_me )
286 $nt = Title
::newFromURL( $target );
287 $nu = User
::newFromName( $nt->getText() );
288 return new EmailUserForm( $nu, $text, $subject, $cc_me );