root/trunk/wp-includes/class-phpmailer.php

Revision 1519, 54.8 kB (checked in by donncha, 6 days ago)

WP Merge

  • Property svn:eol-style set to native
Line 
1 <?php
2 /*~ class.phpmailer.php
3 .---------------------------------------------------------------------------.
4 |  Software: PHPMailer - PHP email class                                    |
5 |   Version: 2.0.2                                                          |
6 |   Contact: via sourceforge.net support pages (also www.codeworxtech.com)  |
7 |      Info: http://phpmailer.sourceforge.net                               |
8 |   Support: http://sourceforge.net/projects/phpmailer/                     |
9 | ------------------------------------------------------------------------- |
10 |    Author: Andy Prevost (project admininistrator)                         |
11 |    Author: Brent R. Matzelle (original founder)                           |
12 | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved.               |
13 | Copyright (c) 2001-2003, Brent R. Matzelle                                |
14 | ------------------------------------------------------------------------- |
15 |   License: Distributed under the Lesser General Public License (LGPL)     |
16 |            http://www.gnu.org/copyleft/lesser.html                        |
17 | This program is distributed in the hope that it will be useful - WITHOUT  |
18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
19 | FITNESS FOR A PARTICULAR PURPOSE.                                         |
20 | ------------------------------------------------------------------------- |
21 | We offer a number of paid services (www.codeworxtech.com):                |
22 | - Web Hosting on highly optimized fast and secure servers                 |
23 | - Technology Consulting                                                   |
24 | - Oursourcing (highly qualified programmers and graphic designers)        |
25 '---------------------------------------------------------------------------'
26  */
27 /**
28  * PHPMailer - PHP email transport class
29  * @package PHPMailer
30  * @author Andy Prevost
31  * @copyright 2004 - 2008 Andy Prevost
32  */
33
34 class PHPMailer {
35
36   /////////////////////////////////////////////////
37   // PROPERTIES, PUBLIC
38   /////////////////////////////////////////////////
39
40   /**
41    * Email priority (1 = High, 3 = Normal, 5 = low).
42    * @var int
43    */
44   var $Priority          = 3;
45
46   /**
47    * Sets the CharSet of the message.
48    * @var string
49    */
50   var $CharSet           = 'iso-8859-1';
51
52   /**
53    * Sets the Content-type of the message.
54    * @var string
55    */
56   var $ContentType        = 'text/plain';
57
58   /**
59    * Sets the Encoding of the message. Options for this are "8bit",
60    * "7bit", "binary", "base64", and "quoted-printable".
61    * @var string
62    */
63   var $Encoding          = '8bit';
64
65   /**
66    * Holds the most recent mailer error message.
67    * @var string
68    */
69   var $ErrorInfo         = '';
70
71   /**
72    * Sets the From email address for the message.
73    * @var string
74    */
75   var $From              = 'root@localhost';
76
77   /**
78    * Sets the From name of the message.
79    * @var string
80    */
81   var $FromName          = 'Root User';
82
83   /**
84    * Sets the Sender email (Return-Path) of the message.  If not empty,
85    * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
86    * @var string
87    */
88   var $Sender            = '';
89
90   /**
91    * Sets the Subject of the message.
92    * @var string
93    */
94   var $Subject           = '';
95
96   /**
97    * Sets the Body of the message.  This can be either an HTML or text body.
98    * If HTML then run IsHTML(true).
99    * @var string
100    */
101   var $Body              = '';
102
103   /**
104    * Sets the text-only body of the message.  This automatically sets the
105    * email to multipart/alternative.  This body can be read by mail
106    * clients that do not have HTML email capability such as mutt. Clients
107    * that can read HTML will view the normal Body.
108    * @var string
109    */
110   var $AltBody           = '';
111
112   /**
113    * Sets word wrapping on the body of the message to a given number of
114    * characters.
115    * @var int
116    */
117   var $WordWrap          = 0;
118
119   /**
120    * Method to send mail: ("mail", "sendmail", or "smtp").
121    * @var string
122    */
123   var $Mailer            = 'mail';
124
125   /**
126    * Sets the path of the sendmail program.
127    * @var string
128    */
129   var $Sendmail          = '/usr/sbin/sendmail';
130
131   /**
132    * Path to PHPMailer plugins.  This is now only useful if the SMTP class
133    * is in a different directory than the PHP include path.
134    * @var string
135    */
136   var $PluginDir         = '';
137
138   /**
139    * Holds PHPMailer version.
140    * @var string
141    */
142   var $Version           = "2.0.2";
143
144   /**
145    * Sets the email address that a reading confirmation will be sent.
146    * @var string
147    */
148   var $ConfirmReadingTo  = '';
149
150   /**
151    * Sets the hostname to use in Message-Id and Received headers
152    * and as default HELO string. If empty, the value returned
153    * by SERVER_NAME is used or 'localhost.localdomain'.
154    * @var string
155    */
156   var $Hostname          = '';
157
158   /**
159    * Sets the message ID to be used in the Message-Id header.
160    * If empty, a unique id will be generated.
161    * @var string
162    */
163   var $MessageID         = '';
164
165   /////////////////////////////////////////////////
166   // PROPERTIES FOR SMTP
167   /////////////////////////////////////////////////
168
169   /**
170    * Sets the SMTP hosts.  All hosts must be separated by a
171    * semicolon.  You can also specify a different port
172    * for each host by using this format: [hostname:port]
173    * (e.g. "smtp1.example.com:25;smtp2.example.com").
174    * Hosts will be tried in order.
175    * @var string
176    */
177   var $Host        = 'localhost';
178
179   /**
180    * Sets the default SMTP server port.
181    * @var int
182    */
183   var $Port        = 25;
184
185   /**
186    * Sets the SMTP HELO of the message (Default is $Hostname).
187    * @var string
188    */
189   var $Helo        = '';
190
191   /**
192    * Sets connection prefix.
193    * Options are "", "ssl" or "tls"
194    * @var string
195    */
196   var $SMTPSecure = "";
197
198   /**
199    * Sets SMTP authentication. Utilizes the Username and Password variables.
200    * @var bool
201    */
202   var $SMTPAuth     = false;
203
204   /**
205    * Sets SMTP username.
206    * @var string
207    */
208   var $Username     = '';
209
210   /**
211    * Sets SMTP password.
212    * @var string
213    */
214   var $Password     = '';
215
216   /**
217    * Sets the SMTP server timeout in seconds. This function will not
218    * work with the win32 version.
219    * @var int
220    */
221   var $Timeout      = 10;
222
223   /**
224    * Sets SMTP class debugging on or off.
225    * @var bool
226    */
227   var $SMTPDebug    = false;
228
229   /**
230    * Prevents the SMTP connection from being closed after each mail
231    * sending.  If this is set to true then to close the connection
232    * requires an explicit call to SmtpClose().
233    * @var bool
234    */
235   var $SMTPKeepAlive = false;
236
237   /**
238    * Provides the ability to have the TO field process individual
239    * emails, instead of sending to entire TO addresses
240    * @var bool
241    */
242   var $SingleTo = false;
243
244   /////////////////////////////////////////////////
245   // PROPERTIES, PRIVATE
246   /////////////////////////////////////////////////
247
248   var $smtp            = NULL;
249   var $to              = array();
250   var $cc              = array();
251   var $bcc             = array();
252   var $ReplyTo         = array();
253   var $attachment      = array();
254   var $CustomHeader    = array();
255   var $message_type    = '';
256   var $boundary        = array();
257   var $language        = array();
258   var $error_count     = 0;
259   var $LE              = "\n";
260   var $sign_key_file   = "";
261   var $sign_key_pass   = "";
262
263   /////////////////////////////////////////////////
264   // METHODS, VARIABLES
265   /////////////////////////////////////////////////
266
267   /**
268    * Sets message type to HTML.
269    * @param bool $bool
270    * @return void
271    */
272   function IsHTML($bool) {
273     if($bool == true) {
274       $this->ContentType = 'text/html';
275     } else {
276       $this->ContentType = 'text/plain';
277     }
278   }
279
280   /**
281    * Sets Mailer to send message using SMTP.
282    * @return void
283    */
284   function IsSMTP() {
285     $this->Mailer = 'smtp';
286   }
287
288   /**
289    * Sets Mailer to send message using PHP mail() function.
290    * @return void
291    */
292   function IsMail() {
293     $this->Mailer = 'mail';
294   }
295
296   /**
297    * Sets Mailer to send message using the $Sendmail program.
298    * @return void
299    */
300   function IsSendmail() {
301     $this->Mailer = 'sendmail';
302   }
303
304   /**
305    * Sets Mailer to send message using the qmail MTA.
306    * @return void
307    */
308   function IsQmail() {
309     $this->Sendmail = '/var/qmail/bin/sendmail';
310     $this->Mailer = 'sendmail';
311   }
312
313   /////////////////////////////////////////////////
314   // METHODS, RECIPIENTS
315   /////////////////////////////////////////////////
316
317   /**
318    * Adds a "To" address.
319    * @param string $address
320    * @param string $name
321    * @return void
322    */
323   function AddAddress($address, $name = '') {
324     $cur = count($this->to);
325     $this->to[$cur][0] = trim($address);
326     $this->to[$cur][1] = $name;
327   }
328
329   /**
330    * Adds a "Cc" address. Note: this function works
331    * with the SMTP mailer on win32, not with the "mail"
332    * mailer.
333    * @param string $address
334    * @param string $name
335    * @return void
336    */
337   function AddCC($address, $name = '') {
338     $cur = count($this->cc);
339     $this->cc[$cur][0] = trim($address);
340     $this->cc[$cur][1] = $name;
341   }
342
343   /**
344    * Adds a "Bcc" address. Note: this function works
345    * with the SMTP mailer on win32, not with the "mail"
346    * mailer.
347    * @param string $address
348    * @param string $name
349    * @return void
350    */
351   function AddBCC($address, $name = '') {
352     $cur = count($this->bcc);
353     $this->bcc[$cur][0] = trim($address);
354     $this->bcc[$cur][1] = $name;
355   }
356
357   /**
358    * Adds a "Reply-To" address.
359    * @param string $address
360    * @param string $name
361    * @return void
362    */
363   function AddReplyTo($address, $name = '') {
364     $cur = count($this->ReplyTo);
365     $this->ReplyTo[$cur][0] = trim($address);
366     $this->ReplyTo[$cur][1] = $name;
367   }
368
369   /////////////////////////////////////////////////
370   // METHODS, MAIL SENDING
371   /////////////////////////////////////////////////
372
373   /**
374    * Creates message and assigns Mailer. If the message is
375    * not sent successfully then it returns false.  Use the ErrorInfo
376    * variable to view description of the error.
377    * @return bool
378    */
379   function Send() {
380     $header = '';
381     $body = '';
382     $result = true;
383
384     if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
385       $this->SetError($this->Lang('provide_address'));
386       return false;
387     }
388
389     /* Set whether the message is multipart/alternative */
390     if(!empty($this->AltBody)) {
391       $this->ContentType = 'multipart/alternative';
392     }
393
394     $this->error_count = 0; // reset errors
395     $this->SetMessageType();
396     $header .= $this->CreateHeader();
397     $body = $this->CreateBody();
398
399     if($body == '') {
400       return false;
401     }
402
403     /* Choose the mailer */
404     switch($this->Mailer) {
405       case 'sendmail':
406         $result = $this->SendmailSend($header, $body);
407         break;
408       case 'smtp':
409         $result = $this->SmtpSend($header, $body);
410         break;
411       case 'mail':
412         $result = $this->MailSend($header, $body);
413         break;
414       default:
415         $result = $this->MailSend($header, $body);
416         break;
417         //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
418         //$result = false;
419         //break;
420     }
421
422     return $result;
423   }
424
425   /**
426    * Sends mail using the $Sendmail program.
427    * @access private
428    * @return bool
429    */
430   function SendmailSend($header, $body) {
431     if ($this->Sender != '') {
432       $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
433     } else {
434       $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
435     }
436
437     if(!@$mail = popen($sendmail, 'w')) {
438       $this->SetError($this->Lang('execute') . $this->Sendmail);
439       return false;
440     }
441
442     fputs($mail, $header);
443     fputs($mail, $body);
444
445     $result = pclose($mail);
446     if (version_compare(phpversion(), '4.2.3') == -1) {
447       $result = $result >> 8 & 0xFF;
448     }
449     if($result != 0) {
450       $this->SetError($this->Lang('execute') . $this->Sendmail);
451       return false;
452     }
453     return true;
454   }
455
456   /**
457    * Sends mail using the PHP mail() function.
458    * @access private
459    * @return bool
460    */
461   function MailSend($header, $body) {
462
463     $to = '';
464     for($i = 0; $i < count($this->to); $i++) {
465       if($i != 0) { $to .= ', '; }
466       $to .= $this->AddrFormat($this->to[$i]);
467     }
468
469     $toArr = split(',', $to);
470
471     $params = sprintf("-oi -f %s", $this->Sender);
472     if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
473       $old_from = ini_get('sendmail_from');
474       ini_set('sendmail_from', $this->Sender);
475       if ($this->SingleTo === true && count($toArr) > 1) {
476         foreach ($toArr as $key => $val) {
477           $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
478         }
479       } else {
480         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
481       }
482     } else {
483       if ($this->SingleTo === true && count($toArr) > 1) {
484         foreach ($toArr as $key => $val) {
485           $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
486         }
487       } else {
488         $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
489       }
490     }
491
492     if (isset($old_from)) {
493       ini_set('sendmail_from', $old_from);
494     }
495
496     if(!$rt) {
497       $this->SetError($this->Lang('instantiate'));
498       return false;
499     }
500
501     return true;
502   }
503
504   /**
505    * Sends mail via SMTP using PhpSMTP (Author:
506    * Chris Ryan).  Returns bool.  Returns false if there is a
507    * bad MAIL FROM, RCPT, or DATA input.
508    * @access private
509    * @return bool
510    */
511   function SmtpSend($header, $body) {
512     include_once($this->PluginDir . 'class-smtp.php');
513     $error = '';
514     $bad_rcpt = array();
515
516     if(!$this->SmtpConnect()) {
517       return false;
518     }
519
520     $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
521     if(!$this->smtp->Mail($smtp_from)) {
522       $error = $this->Lang('from_failed') . $smtp_from;
523       $this->SetError($error);
524       $this->smtp->Reset();
525       return false;
526     }
527
528     /* Attempt to send attach all recipients */
529     for($i = 0; $i < count($this->to); $i++) {
530       if(!$this->smtp->Recipient($this->to[$i][0])) {
531         $bad_rcpt[] = $this->to[$i][0];
532       }
533     }
534     for($i = 0; $i < count($this->cc); $i++) {
535       if(!$this->smtp->Recipient($this->cc[$i][0])) {
536         $bad_rcpt[] = $this->cc[$i][0];
537       }
538     }
539     for($i = 0; $i < count($this->bcc); $i++) {
540       if(!$this->smtp->Recipient($this->bcc[$i][0])) {
541         $bad_rcpt[] = $this->bcc[$i][0];
542       }
543     }
544
545     if(count($bad_rcpt) > 0) { // Create error message
546       for($i = 0; $i < count($bad_rcpt); $i++) {
547         if($i != 0) {
548           $error .= ', ';
549         }
550         $error .= $bad_rcpt[$i];
551       }
552       $error = $this->Lang('recipients_failed') . $error;
553       $this->SetError($error);
554       $this->smtp->Reset();
555       return false;
556     }
557
558     if(!$this->smtp->Data($header . $body)) {
559       $this->SetError($this->Lang('data_not_accepted'));
560       $this->smtp->Reset();
561       return false;
562     }
563     if($this->SMTPKeepAlive == true) {
564       $this->smtp->Reset();
565     } else {
566       $this->SmtpClose();
567     }
568
569     return true;
570   }
571
572   /**
573    * Initiates a connection to an SMTP server.  Returns false if the
574    * operation failed.
575    * @access private
576    * @return bool
577    */
578   function SmtpConnect() {
579     if($this->smtp == NULL) {
580       $this->smtp = new SMTP();
581     }
582
583     $this->smtp->do_debug = $this->SMTPDebug;
584     $hosts = explode(';', $this->Host);
585     $index = 0;
586     $connection = ($this->smtp->Connected());
587
588     /* Retry while there is no connection */
589     while($index < count($hosts) && $connection == false) {
590       $hostinfo = array();
591       if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {
592         $host = $hostinfo[1];
593         $port = $hostinfo[2];
594       } else {
595         $host = $hosts[$index];
596         $port = $this->Port;
597       }
598
599       if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) {
600         if ($this->Helo != '') {
601    &nb