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

Revision 8082, 43.9 kB (checked in by westi, 3 weeks ago)

Updated phpdoc for external libs. See #7038 props jacobsantos.

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