3 * Modification information for LGPL compliance
4 * r56732 - 2012-09-26 14:08:00 -0700 (Wed, 26 Sep 2012) - rbacon
5 * - PHPMailer has a bug that allows an invalid connection to flood the server with errors in an infinite loop
7 * r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
9 * r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
11 * r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
13 * r54045 - 2010-01-26 12:25:05 -0800 (Tue, 26 Jan 2010) - roger - merge from Kobe rev: 53336 - 54021
15 * r53116 - 2009-12-09 17:24:37 -0800 (Wed, 09 Dec 2009) - mitani - Merge Kobe into Windex Revision 51633 to 53087
17 * r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
19 * r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
21 * r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
23 * r43691 - 2009-01-29 15:25:53 -0800 (Thu, 29 Jan 2009) - faissah - 27521 : Update to phpmailer version 2.3.
25 * r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
27 * r39785 - 2008-09-12 15:49:45 -0700 (Fri, 12 Sep 2008) - faissah - Update to PHPmailer 2.2.1
29 * r24320 - 2007-07-13 16:42:12 -0700 (Fri, 13 Jul 2007) - chris - Email 2.0 - enabling Gmail SMTP over SSL sends.
31 * r11652 - 2006-02-21 18:24:06 -0800 (Tue, 21 Feb 2006) - chris - Bug 4719: updating PHPMailer classes for security (DDoS)
33 * include/phpmailer (everything)
34 * include/SugarPHPMailer.php (adding our constructor)
35 * modules/Email/Email.php (to use the new constructor)
37 * r2414 - 2005-01-14 18:30:52 -0800 (Fri, 14 Jan 2005) - clint - Change Issues to Bugs. --Clint
39 * r915 - 2004-10-08 15:31:10 -0700 (Fri, 08 Oct 2004) - julian - E-mail notification feature + new admin console
43 .---------------------------------------------------------------------------.
44 | Software: PHPMailer - PHP email class |
46 | Site: https://code.google.com/a/apache-extras.org/p/phpmailer/ |
47 | ------------------------------------------------------------------------- |
48 | Admin: Jim Jagielski (project admininistrator) |
49 | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
50 | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net |
51 | : Jim Jagielski (jimjag) jimjag@gmail.com |
52 | Founder: Brent R. Matzelle (original founder) |
53 | Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved. |
54 | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
55 | Copyright (c) 2001-2003, Brent R. Matzelle |
56 | ------------------------------------------------------------------------- |
57 | License: Distributed under the Lesser General Public License (LGPL) |
58 | http://www.gnu.org/copyleft/lesser.html |
59 | This program is distributed in the hope that it will be useful - WITHOUT |
60 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
61 | FITNESS FOR A PARTICULAR PURPOSE. |
62 '---------------------------------------------------------------------------'
66 * PHPMailer - PHP SMTP email transport class
67 * NOTE: Designed for use with PHP version 5 and up
69 * @author Andy Prevost
70 * @author Marcus Bointon
71 * @copyright 2004 - 2008 Andy Prevost
72 * @author Jim Jagielski
73 * @copyright 2010 - 2012 Jim Jagielski
74 * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
79 * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
80 * commands except TURN which will always return a not implemented
81 * error. SMTP also provides some utility methods for sending mail
83 * original author: Chris Ryan
91 public $SMTP_PORT = 25;
94 * SMTP reply line ending
97 public $CRLF = "\r\n";
100 * Sets whether debugging is turned on
103 public $do_debug; // the level of debug to perform
106 * Sets VERP use on/off (default is off)
109 public $do_verp = false;
112 * Sets the SMTP PHPMailer Version number
115 public $Version = '5.2.1';
117 /////////////////////////////////////////////////
118 // PROPERTIES, PRIVATE AND PROTECTED
119 /////////////////////////////////////////////////
121 private $smtp_conn; // the socket to the server
122 private $error; // error if any on the last call
123 private $helo_rply; // the reply the server sent to us for HELO
126 * Initialize the class so that the data is in a known state.
130 public function __construct() {
131 $this->smtp_conn = 0;
133 $this->helo_rply = null;
138 /////////////////////////////////////////////////
139 // CONNECTION FUNCTIONS
140 /////////////////////////////////////////////////
143 * Connect to the server specified on the port specified.
144 * If the port is not specified use the default SMTP_PORT.
145 * If tval is specified then a connection will try and be
146 * established with the server for that number of seconds.
147 * If tval is not specified the default is 30 seconds to
148 * try on the connection.
150 * SMTP CODE SUCCESS: 220
151 * SMTP CODE FAILURE: 421
155 public function Connect($host, $port = 0, $tval = 30) {
156 // set the error val to null so there is no confusion
159 // make sure we are __not__ connected
160 if($this->connected()) {
161 // already connected, generate error
162 $this->error = array("error" => "Already connected to a server");
167 $port = $this->SMTP_PORT;
170 // connect to the smtp server
171 $this->smtp_conn = @fsockopen($host, // the host of the server
172 $port, // the port to use
173 $errno, // error number if any
174 $errstr, // error message if any
175 $tval); // give up after ? secs
176 // verify we connected properly
177 if(empty($this->smtp_conn)) {
178 $GLOBALS['log']->fatal("SMTP -> ERROR: Failed to connect to server. Code: $errno Reply: $errstr ");
179 if($this->do_debug >= 1) {
180 echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
185 // SMTP server can take longer to respond, give longer timeout for first read
186 // Windows does not have support for this timeout function
187 if(substr(PHP_OS, 0, 3) != "WIN")
188 socket_set_timeout($this->smtp_conn, $tval, 0);
190 // get any announcement
191 $announce = $this->get_lines();
193 if($this->do_debug >= 2) {
194 echo "SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />';
201 * Initiate a TLS communication with the server.
203 * SMTP CODE 220 Ready to start TLS
204 * SMTP CODE 501 Syntax error (no parameters allowed)
205 * SMTP CODE 454 TLS not available due to temporary reason
207 * @return bool success
209 public function StartTLS() {
210 $this->error = null; # to avoid confusion
212 if(!$this->connected()) {
213 $this->error = array("error" => "Called StartTLS() without being connected");
217 fputs($this->smtp_conn,"STARTTLS" . $this->CRLF);
219 $rply = $this->get_lines();
220 $code = substr($rply,0,3);
222 if($this->do_debug >= 2) {
223 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
228 array("error" => "STARTTLS not accepted from server",
229 "smtp_code" => $code,
230 "smtp_msg" => substr($rply,4));
231 if($this->do_debug >= 1) {
232 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
237 // Begin encrypted connection
238 if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
246 * Performs SMTP authentication. Must be run after running the
247 * Hello() method. Returns true if successfully authenticated.
251 public function Authenticate($username, $password) {
252 // Start authentication
253 fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
255 $rply = $this->get_lines();
256 $code = substr($rply,0,3);
259 $GLOBALS['log']->fatal("SMTP -> ERROR:AUTH not accepted from server. Code: $code Reply: $rply");
260 if($this->do_debug >= 1) {
261 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
266 // Send encoded username
267 fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
269 $rply = $this->get_lines();
270 $code = substr($rply,0,3);
273 $GLOBALS['log']->fatal("SMTP -> ERROR:Username not accepted from server. Code: $code Reply: $rply ");
274 if($this->do_debug >= 1) {
275 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
280 // Send encoded password
281 $password = from_html($password);
282 fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
284 $rply = $this->get_lines();
285 $code = substr($rply,0,3);
288 $GLOBALS['log']->fatal("SMTP -> ERROR:Password not accepted from server. Code: $code Reply: $rply ");
289 if($this->do_debug >= 1) {
290 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
299 * Returns true if connected to a server otherwise false
303 public function Connected() {
304 if(!empty($this->smtp_conn)) {
305 $sock_status = socket_get_status($this->smtp_conn);
306 if($sock_status["eof"]) {
307 // the socket is valid but we are not connected
308 if($this->do_debug >= 1) {
309 echo "SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected";
314 return true; // everything looks good
320 * Closes the socket and cleans up the state of the class.
321 * It is not considered good to use this function without
322 * first trying to use QUIT.
326 public function Close() {
327 $this->error = null; // so there is no confusion
328 $this->helo_rply = null;
329 if(!empty($this->smtp_conn)) {
330 // close the connection and cleanup
331 fclose($this->smtp_conn);
332 $this->smtp_conn = 0;
336 /////////////////////////////////////////////////
338 /////////////////////////////////////////////////
341 * Issues a data command and sends the msg_data to the server
342 * finializing the mail transaction. $msg_data is the message
343 * that is to be send with the headers. Each header needs to be
344 * on a single line followed by a <CRLF> with the message headers
345 * and the message body being seperated by and additional <CRLF>.
347 * Implements rfc 821: DATA <CRLF>
349 * SMTP CODE INTERMEDIATE: 354
352 * SMTP CODE SUCCESS: 250
353 * SMTP CODE FAILURE: 552,554,451,452
354 * SMTP CODE FAILURE: 451,554
355 * SMTP CODE ERROR : 500,501,503,421
359 public function Data($msg_data) {
360 $this->error = null; // so no confusion is caused
362 if(!$this->connected()) {
363 $this->error = array(
364 "error" => "Called Data() without being connected");
368 fputs($this->smtp_conn,"DATA" . $this->CRLF);
370 $rply = $this->get_lines();
371 $code = substr($rply,0,3);
373 if($this->do_debug >= 2) {
374 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
379 array("error" => "DATA command not accepted from server",
380 "smtp_code" => $code,
381 "smtp_msg" => substr($rply,4));
382 if($this->do_debug >= 1) {
383 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
388 /* the server is ready to accept data!
389 * according to rfc 821 we should not send more than 1000
391 * characters on a single line so we will break the data up
392 * into lines by \r and/or \n then if needed we will break
393 * each of those into smaller lines to fit within the limit.
394 * in addition we will be looking for lines that start with
395 * a period '.' and append and additional period '.' to that
396 * line. NOTE: this does not count towards limit.
399 // normalize the line breaks so we know the explode works
400 $msg_data = str_replace("\r\n","\n",$msg_data);
401 $msg_data = str_replace("\r","\n",$msg_data);
402 $lines = explode("\n",$msg_data);
404 /* we need to find a good way to determine is headers are
405 * in the msg_data or if it is a straight msg body
406 * currently I am assuming rfc 822 definitions of msg headers
407 * and if the first field of the first line (':' sperated)
408 * does not contain a space then it _should_ be a header
409 * and we can process all lines before a blank "" line as
413 $field = substr($lines[0],0,strpos($lines[0],":"));
415 if(!empty($field) && !strstr($field," ")) {
419 $max_line_length = 998; // used below; set here for ease in change
421 while(list(,$line) = @each($lines)) {
423 if($line == "" && $in_headers) {
426 // ok we need to break this line up into several smaller lines
427 while(strlen($line) > $max_line_length) {
428 $pos = strrpos(substr($line,0,$max_line_length)," ");
430 // Patch to fix DOS attack
432 $pos = $max_line_length - 1;
433 $lines_out[] = substr($line,0,$pos);
434 $line = substr($line,$pos);
436 $lines_out[] = substr($line,0,$pos);
437 $line = substr($line,$pos + 1);
440 /* if processing headers add a LWSP-char to the front of new line
441 * rfc 822 on long msg headers
444 $line = "\t" . $line;
447 $lines_out[] = $line;
449 // send the lines to the server
450 while(list(,$line_out) = @each($lines_out)) {
451 if(strlen($line_out) > 0)
453 if(substr($line_out, 0, 1) == ".") {
454 $line_out = "." . $line_out;
457 fputs($this->smtp_conn,$line_out . $this->CRLF);
461 // message data has been sent
462 fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
464 $rply = $this->get_lines();
465 $code = substr($rply,0,3);
467 if($this->do_debug >= 2) {
468 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
473 array("error" => "DATA not accepted from server",
474 "smtp_code" => $code,
475 "smtp_msg" => substr($rply,4));
476 if($this->do_debug >= 1) {
477 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
485 * Sends the HELO command to the smtp server.
486 * This makes sure that we and the server are in
487 * the same known state.
489 * Implements from rfc 821: HELO <SP> <domain> <CRLF>
491 * SMTP CODE SUCCESS: 250
492 * SMTP CODE ERROR : 500, 501, 504, 421
496 public function Hello($host = '') {
497 $this->error = null; // so no confusion is caused
499 if(!$this->connected()) {
500 $this->error = array(
501 "error" => "Called Hello() without being connected");
505 // if hostname for HELO was not specified send default
507 // determine appropriate default to send to server
511 // Send extended hello first (RFC 2821)
512 if(!$this->SendHello("EHLO", $host)) {
513 if(!$this->SendHello("HELO", $host)) {
522 * Sends a HELO/EHLO command.
526 private function SendHello($hello, $host) {
527 fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
529 $rply = $this->get_lines();
530 $code = substr($rply,0,3);
532 if($this->do_debug >= 2) {
533 echo "SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />';
538 array("error" => $hello . " not accepted from server",
539 "smtp_code" => $code,
540 "smtp_msg" => substr($rply,4));
541 if($this->do_debug >= 1) {
542 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
547 $this->helo_rply = $rply;
553 * Starts a mail transaction from the email address specified in
554 * $from. Returns true if successful or false otherwise. If True
555 * the mail transaction is started and then one or more Recipient
556 * commands may be called followed by a Data command.
558 * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
560 * SMTP CODE SUCCESS: 250
561 * SMTP CODE SUCCESS: 552,451,452
562 * SMTP CODE SUCCESS: 500,501,421
566 public function Mail($from) {
567 $this->error = null; // so no confusion is caused
569 if(!$this->connected()) {
570 $this->error = array(
571 "error" => "Called Mail() without being connected");
575 $useVerp = ($this->do_verp ? "XVERP" : "");
576 fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
578 $rply = $this->get_lines();
579 $code = substr($rply,0,3);
581 if($this->do_debug >= 2) {
582 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
587 array("error" => "MAIL not accepted from server",
588 "smtp_code" => $code,
589 "smtp_msg" => substr($rply,4));
590 if($this->do_debug >= 1) {
591 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
599 * Sends the quit command to the server and then closes the socket
600 * if there is no error or the $close_on_error argument is true.
602 * Implements from rfc 821: QUIT <CRLF>
604 * SMTP CODE SUCCESS: 221
605 * SMTP CODE ERROR : 500
609 public function Quit($close_on_error = true) {
610 $this->error = null; // so there is no confusion
612 if(!$this->connected()) {
613 $this->error = array(
614 "error" => "Called Quit() without being connected");
618 // send the quit command to the server
619 fputs($this->smtp_conn,"quit" . $this->CRLF);
621 // get any good-bye messages
622 $byemsg = $this->get_lines();
624 if($this->do_debug >= 2) {
625 echo "SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />';
631 $code = substr($byemsg,0,3);
633 // use e as a tmp var cause Close will overwrite $this->error
634 $e = array("error" => "SMTP server rejected quit command",
635 "smtp_code" => $code,
636 "smtp_rply" => substr($byemsg,4));
638 if($this->do_debug >= 1) {
639 echo "SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />';
643 if(empty($e) || $close_on_error) {
651 * Sends the command RCPT to the SMTP server with the TO: argument of $to.
652 * Returns true if the recipient was accepted false if it was rejected.
654 * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
656 * SMTP CODE SUCCESS: 250,251
657 * SMTP CODE FAILURE: 550,551,552,553,450,451,452
658 * SMTP CODE ERROR : 500,501,503,421
662 public function Recipient($to) {
663 $this->error = null; // so no confusion is caused
665 if(!$this->connected()) {
666 $this->error = array(
667 "error" => "Called Recipient() without being connected");
671 fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
673 $rply = $this->get_lines();
674 $code = substr($rply,0,3);
676 if($this->do_debug >= 2) {
677 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
680 if($code != 250 && $code != 251) {
681 $GLOBALS['log']->fatal("SMTP -> ERROR: RCPT not accepted from server. Code: $code Reply: $rply ");
682 if($this->do_debug >= 1) {
683 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
691 * Sends the RSET command to abort and transaction that is
692 * currently in progress. Returns true if successful false
695 * Implements rfc 821: RSET <CRLF>
697 * SMTP CODE SUCCESS: 250
698 * SMTP CODE ERROR : 500,501,504,421
702 public function Reset() {
703 $this->error = null; // so no confusion is caused
705 if(!$this->connected()) {
706 $this->error = array(
707 "error" => "Called Reset() without being connected");
711 fputs($this->smtp_conn,"RSET" . $this->CRLF);
713 $rply = $this->get_lines();
714 $code = substr($rply,0,3);
716 if($this->do_debug >= 2) {
717 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
722 array("error" => "RSET failed",
723 "smtp_code" => $code,
724 "smtp_msg" => substr($rply,4));
725 if($this->do_debug >= 1) {
726 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
735 * Starts a mail transaction from the email address specified in
736 * $from. Returns true if successful or false otherwise. If True
737 * the mail transaction is started and then one or more Recipient
738 * commands may be called followed by a Data command. This command
739 * will send the message to the users terminal if they are logged
740 * in and send them an email.
742 * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
744 * SMTP CODE SUCCESS: 250
745 * SMTP CODE SUCCESS: 552,451,452
746 * SMTP CODE SUCCESS: 500,501,502,421
750 public function SendAndMail($from) {
751 $this->error = null; // so no confusion is caused
753 if(!$this->connected()) {
754 $this->error = array(
755 "error" => "Called SendAndMail() without being connected");
759 fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
761 $rply = $this->get_lines();
762 $code = substr($rply,0,3);
764 if($this->do_debug >= 2) {
765 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
770 array("error" => "SAML not accepted from server",
771 "smtp_code" => $code,
772 "smtp_msg" => substr($rply,4));
773 if($this->do_debug >= 1) {
774 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
782 * This is an optional command for SMTP that this class does not
783 * support. This method is here to make the RFC821 Definition
784 * complete for this class and __may__ be implimented in the future
786 * Implements from rfc 821: TURN <CRLF>
788 * SMTP CODE SUCCESS: 250
789 * SMTP CODE FAILURE: 502
790 * SMTP CODE ERROR : 500, 503
794 public function Turn() {
795 $this->error = array("error" => "This method, TURN, of the SMTP ".
796 "is not implemented");
797 if($this->do_debug >= 1) {
798 echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />';
804 * Get the current error
808 public function getError() {
812 /////////////////////////////////////////////////
813 // INTERNAL FUNCTIONS
814 /////////////////////////////////////////////////
817 * Read in as many lines as possible
818 * either before eof or socket timeout occurs on the operation.
819 * With SMTP we can tell if we have more lines to read if the
820 * 4th character is '-' symbol. If it is a space then we don't
821 * need to read anything else.
825 private function get_lines() {
827 // Avoid infinite loop if we don't have a resource to read.
828 if (!is_resource($this->smtp_conn)) {
829 $GLOBALS['log']->warn('SMTP Connection is not a valid resource');
834 while(!feof($this->smtp_conn)) {
835 $str = @fgets($this->smtp_conn,515);
836 if($this->do_debug >= 4) {
837 echo "SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />';
838 echo "SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />';
841 if($this->do_debug >= 4) {
842 echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />';
844 // if 4th character is a space, we are done reading, break the loop
845 if(substr($str,3,1) == " ") { break; }