Index: trunk/phase3/includes/UserMailer.php |
— | — | @@ -163,29 +163,52 @@ |
164 | 164 | |
165 | 165 | wfDebug( __METHOD__ . ': sending mail to ' . implode( ', ', $to ) . "\n" ); |
166 | 166 | |
167 | | - $dest = array(); |
| 167 | + # Make sure we have at least one address |
| 168 | + $has_address = false; |
168 | 169 | foreach ( $to as $u ) { |
169 | 170 | if ( $u->address ) { |
170 | | - $dest[] = $u->address; |
| 171 | + $has_address = true; |
| 172 | + break; |
171 | 173 | } |
172 | 174 | } |
173 | | - if ( count( $dest ) == 0 ) { |
| 175 | + if ( !$has_address ) { |
174 | 176 | return Status::newFatal( 'user-mail-no-addy' ); |
175 | 177 | } |
176 | 178 | |
| 179 | + # Forge email headers |
| 180 | + # ------------------- |
| 181 | + # |
| 182 | + # WARNING |
| 183 | + # |
| 184 | + # DO NOT add To: or Subject: headers at this step. They need to be |
| 185 | + # handled differently depending upon the mailer we are going to use. |
| 186 | + # |
| 187 | + # To: |
| 188 | + # PHP mail() first argument is the mail receiver. The argument is |
| 189 | + # used as a recipient destination and as a To header. |
| 190 | + # |
| 191 | + # PEAR mailer has a recipient argument which is only used to |
| 192 | + # send the mail. If no To header is given, PEAR will set it to |
| 193 | + # to 'undisclosed-recipients:'. |
| 194 | + # |
| 195 | + # NOTE: To: is for presentation, the actual recipient is specified |
| 196 | + # by the mailer using the Rcpt-To: header. |
| 197 | + # |
| 198 | + # Subject: |
| 199 | + # PHP mail() second argument to pass the subject, passing a Subject |
| 200 | + # as an additional header will result in a duplicate header. |
| 201 | + # |
| 202 | + # PEAR mailer should be passed a Subject header. |
| 203 | + # |
| 204 | + # -- hashar 20120218 |
| 205 | + |
177 | 206 | $headers['From'] = $from->toString(); |
178 | 207 | $headers['Return-Path'] = $from->address; |
179 | | - if ( count( $to ) == 1 ) { |
180 | | - $headers['To'] = $to[0]->toString(); |
181 | | - } else { |
182 | | - $headers['To'] = 'undisclosed-recipients:;'; |
183 | | - } |
184 | 208 | |
185 | 209 | if ( $replyto ) { |
186 | 210 | $headers['Reply-To'] = $replyto->toString(); |
187 | 211 | } |
188 | 212 | |
189 | | - $headers['Subject'] = self::quotedPrintable( $subject ); |
190 | 213 | $headers['Date'] = date( 'r' ); |
191 | 214 | $headers['MIME-Version'] = '1.0'; |
192 | 215 | $headers['Content-type'] = ( is_null( $contentType ) ? |
— | — | @@ -203,6 +226,10 @@ |
204 | 227 | } |
205 | 228 | |
206 | 229 | if ( is_array( $wgSMTP ) ) { |
| 230 | + # |
| 231 | + # PEAR MAILER |
| 232 | + # |
| 233 | + |
207 | 234 | if ( function_exists( 'stream_resolve_include_path' ) ) { |
208 | 235 | $found = stream_resolve_include_path( 'Mail.php' ); |
209 | 236 | } else { |
— | — | @@ -224,9 +251,20 @@ |
225 | 252 | } |
226 | 253 | |
227 | 254 | wfDebug( "Sending mail via PEAR::Mail\n" ); |
228 | | - $chunks = array_chunk( $dest, $wgEnotifMaxRecips ); |
| 255 | + |
| 256 | + $headers['Subject'] = self::quotedPrintable( $subject ); |
| 257 | + |
| 258 | + # When sending only to one recipient, shows it its email using To: |
| 259 | + if ( count( $to ) == 1 ) { |
| 260 | + $headers['To'] = $to[0]->toString(); |
| 261 | + } |
| 262 | + |
| 263 | + # Split jobs since SMTP servers tends to limit the maximum |
| 264 | + # number of possible recipients. |
| 265 | + $chunks = array_chunk( $to, $wgEnotifMaxRecips ); |
229 | 266 | foreach ( $chunks as $chunk ) { |
230 | 267 | $status = self::sendWithPear( $mail_object, $chunk, $headers, $body ); |
| 268 | + # FIXME : some chunks might be sent while others are not! |
231 | 269 | if ( !$status->isOK() ) { |
232 | 270 | wfRestoreWarnings(); |
233 | 271 | return $status; |
— | — | @@ -235,6 +273,10 @@ |
236 | 274 | wfRestoreWarnings(); |
237 | 275 | return Status::newGood(); |
238 | 276 | } else { |
| 277 | + # |
| 278 | + # PHP mail() |
| 279 | + # |
| 280 | + |
239 | 281 | # Line endings need to be different on Unix and Windows due to |
240 | 282 | # the bug described at http://trac.wordpress.org/ticket/2603 |
241 | 283 | if ( wfIsWindows() ) { |
— | — | @@ -244,8 +286,9 @@ |
245 | 287 | $endl = "\n"; |
246 | 288 | } |
247 | 289 | |
248 | | - # Subject header is unneeded since it an argument of mail() |
249 | | - unset( $headers['Subject'] ); |
| 290 | + if( count($to) > 1 ) { |
| 291 | + $headers['To'] = 'undisclosed-recipients:;'; |
| 292 | + } |
250 | 293 | $headers = self::arrayToHeaderString( $headers, $endl ); |
251 | 294 | |
252 | 295 | wfDebug( "Sending mail via internal mail() function\n" ); |
— | — | @@ -256,7 +299,7 @@ |
257 | 300 | set_error_handler( 'UserMailer::errorHandler' ); |
258 | 301 | |
259 | 302 | $safeMode = wfIniGetBool( 'safe_mode' ); |
260 | | - foreach ( $dest as $recip ) { |
| 303 | + foreach ( $to as $recip ) { |
261 | 304 | if ( $safeMode ) { |
262 | 305 | $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers ); |
263 | 306 | } else { |