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 // Check if the resource is valid
253 if(!is_resource($this->smtp_conn))
255 $GLOBALS['log']->fatal("SMTP -> ERROR:Not a valid SMTP resource supplied");
259 // Start authentication
260 fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
262 $rply = $this->get_lines();
263 $code = substr($rply,0,3);
266 $GLOBALS['log']->fatal("SMTP -> ERROR:AUTH not accepted from server. Code: $code Reply: $rply");
267 if($this->do_debug >= 1) {
268 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
273 // Send encoded username
274 fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
276 $rply = $this->get_lines();
277 $code = substr($rply,0,3);
280 $GLOBALS['log']->fatal("SMTP -> ERROR:Username not accepted from server. Code: $code Reply: $rply ");
281 if($this->do_debug >= 1) {
282 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
287 // Send encoded password
288 $password = from_html($password);
289 fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
291 $rply = $this->get_lines();
292 $code = substr($rply,0,3);
295 $GLOBALS['log']->fatal("SMTP -> ERROR:Password not accepted from server. Code: $code Reply: $rply ");
296 if($this->do_debug >= 1) {
297 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
306 * Returns true if connected to a server otherwise false
310 public function Connected() {
311 if(!empty($this->smtp_conn)) {
312 $sock_status = socket_get_status($this->smtp_conn);
313 if($sock_status["eof"]) {
314 // the socket is valid but we are not connected
315 if($this->do_debug >= 1) {
316 echo "SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected";
321 return true; // everything looks good
327 * Closes the socket and cleans up the state of the class.
328 * It is not considered good to use this function without
329 * first trying to use QUIT.
333 public function Close() {
334 $this->error = null; // so there is no confusion
335 $this->helo_rply = null;
336 if(!empty($this->smtp_conn)) {
337 // close the connection and cleanup
338 fclose($this->smtp_conn);
339 $this->smtp_conn = 0;
343 /////////////////////////////////////////////////
345 /////////////////////////////////////////////////
348 * Issues a data command and sends the msg_data to the server
349 * finializing the mail transaction. $msg_data is the message
350 * that is to be send with the headers. Each header needs to be
351 * on a single line followed by a <CRLF> with the message headers
352 * and the message body being seperated by and additional <CRLF>.
354 * Implements rfc 821: DATA <CRLF>
356 * SMTP CODE INTERMEDIATE: 354
359 * SMTP CODE SUCCESS: 250
360 * SMTP CODE FAILURE: 552,554,451,452
361 * SMTP CODE FAILURE: 451,554
362 * SMTP CODE ERROR : 500,501,503,421
366 public function Data($msg_data) {
367 $this->error = null; // so no confusion is caused
369 if(!$this->connected()) {
370 $this->error = array(
371 "error" => "Called Data() without being connected");
375 fputs($this->smtp_conn,"DATA" . $this->CRLF);
377 $rply = $this->get_lines();
378 $code = substr($rply,0,3);
380 if($this->do_debug >= 2) {
381 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
386 array("error" => "DATA command not accepted from server",
387 "smtp_code" => $code,
388 "smtp_msg" => substr($rply,4));
389 if($this->do_debug >= 1) {
390 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
395 /* the server is ready to accept data!
396 * according to rfc 821 we should not send more than 1000
398 * characters on a single line so we will break the data up
399 * into lines by \r and/or \n then if needed we will break
400 * each of those into smaller lines to fit within the limit.
401 * in addition we will be looking for lines that start with
402 * a period '.' and append and additional period '.' to that
403 * line. NOTE: this does not count towards limit.
406 // normalize the line breaks so we know the explode works
407 $msg_data = str_replace("\r\n","\n",$msg_data);
408 $msg_data = str_replace("\r","\n",$msg_data);
409 $lines = explode("\n",$msg_data);
411 /* we need to find a good way to determine is headers are
412 * in the msg_data or if it is a straight msg body
413 * currently I am assuming rfc 822 definitions of msg headers
414 * and if the first field of the first line (':' sperated)
415 * does not contain a space then it _should_ be a header
416 * and we can process all lines before a blank "" line as
420 $field = substr($lines[0],0,strpos($lines[0],":"));
422 if(!empty($field) && !strstr($field," ")) {
426 $max_line_length = 998; // used below; set here for ease in change
428 while(list(,$line) = @each($lines)) {
430 if($line == "" && $in_headers) {
433 // ok we need to break this line up into several smaller lines
434 while(strlen($line) > $max_line_length) {
435 $pos = strrpos(substr($line,0,$max_line_length)," ");
437 // Patch to fix DOS attack
439 $pos = $max_line_length - 1;
440 $lines_out[] = substr($line,0,$pos);
441 $line = substr($line,$pos);
443 $lines_out[] = substr($line,0,$pos);
444 $line = substr($line,$pos + 1);
447 /* if processing headers add a LWSP-char to the front of new line
448 * rfc 822 on long msg headers
451 $line = "\t" . $line;
454 $lines_out[] = $line;
456 // send the lines to the server
457 while(list(,$line_out) = @each($lines_out)) {
458 if(strlen($line_out) > 0)
460 if(substr($line_out, 0, 1) == ".") {
461 $line_out = "." . $line_out;
464 fputs($this->smtp_conn,$line_out . $this->CRLF);
468 // message data has been sent
469 fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
471 $rply = $this->get_lines();
472 $code = substr($rply,0,3);
474 if($this->do_debug >= 2) {
475 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
480 array("error" => "DATA not accepted from server",
481 "smtp_code" => $code,
482 "smtp_msg" => substr($rply,4));
483 if($this->do_debug >= 1) {
484 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
492 * Sends the HELO command to the smtp server.
493 * This makes sure that we and the server are in
494 * the same known state.
496 * Implements from rfc 821: HELO <SP> <domain> <CRLF>
498 * SMTP CODE SUCCESS: 250
499 * SMTP CODE ERROR : 500, 501, 504, 421
503 public function Hello($host = '') {
504 $this->error = null; // so no confusion is caused
506 if(!$this->connected()) {
507 $this->error = array(
508 "error" => "Called Hello() without being connected");
512 // if hostname for HELO was not specified send default
514 // determine appropriate default to send to server
518 // Send extended hello first (RFC 2821)
519 if(!$this->SendHello("EHLO", $host)) {
520 if(!$this->SendHello("HELO", $host)) {
529 * Sends a HELO/EHLO command.
533 private function SendHello($hello, $host) {
534 fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
536 $rply = $this->get_lines();
537 $code = substr($rply,0,3);
539 if($this->do_debug >= 2) {
540 echo "SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />';
545 array("error" => $hello . " not accepted from server",
546 "smtp_code" => $code,
547 "smtp_msg" => substr($rply,4));
548 if($this->do_debug >= 1) {
549 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
554 $this->helo_rply = $rply;
560 * Starts a mail transaction from the email address specified in
561 * $from. Returns true if successful or false otherwise. If True
562 * the mail transaction is started and then one or more Recipient
563 * commands may be called followed by a Data command.
565 * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
567 * SMTP CODE SUCCESS: 250
568 * SMTP CODE SUCCESS: 552,451,452
569 * SMTP CODE SUCCESS: 500,501,421
573 public function Mail($from) {
574 $this->error = null; // so no confusion is caused
576 if(!$this->connected()) {
577 $this->error = array(
578 "error" => "Called Mail() without being connected");
582 $useVerp = ($this->do_verp ? "XVERP" : "");
583 fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
585 $rply = $this->get_lines();
586 $code = substr($rply,0,3);
588 if($this->do_debug >= 2) {
589 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
594 array("error" => "MAIL not accepted from server",
595 "smtp_code" => $code,
596 "smtp_msg" => substr($rply,4));
597 if($this->do_debug >= 1) {
598 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
606 * Sends the quit command to the server and then closes the socket
607 * if there is no error or the $close_on_error argument is true.
609 * Implements from rfc 821: QUIT <CRLF>
611 * SMTP CODE SUCCESS: 221
612 * SMTP CODE ERROR : 500
616 public function Quit($close_on_error = true) {
617 $this->error = null; // so there is no confusion
619 if(!$this->connected()) {
620 $this->error = array(
621 "error" => "Called Quit() without being connected");
625 // send the quit command to the server
626 fputs($this->smtp_conn,"quit" . $this->CRLF);
628 // get any good-bye messages
629 $byemsg = $this->get_lines();
631 if($this->do_debug >= 2) {
632 echo "SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />';
638 $code = substr($byemsg,0,3);
640 // use e as a tmp var cause Close will overwrite $this->error
641 $e = array("error" => "SMTP server rejected quit command",
642 "smtp_code" => $code,
643 "smtp_rply" => substr($byemsg,4));
645 if($this->do_debug >= 1) {
646 echo "SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />';
650 if(empty($e) || $close_on_error) {
658 * Sends the command RCPT to the SMTP server with the TO: argument of $to.
659 * Returns true if the recipient was accepted false if it was rejected.
661 * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
663 * SMTP CODE SUCCESS: 250,251
664 * SMTP CODE FAILURE: 550,551,552,553,450,451,452
665 * SMTP CODE ERROR : 500,501,503,421
669 public function Recipient($to) {
670 $this->error = null; // so no confusion is caused
672 if(!$this->connected()) {
673 $this->error = array(
674 "error" => "Called Recipient() without being connected");
678 fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
680 $rply = $this->get_lines();
681 $code = substr($rply,0,3);
683 if($this->do_debug >= 2) {
684 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
687 if($code != 250 && $code != 251) {
688 $GLOBALS['log']->fatal("SMTP -> ERROR: RCPT not accepted from server. Code: $code Reply: $rply ");
689 if($this->do_debug >= 1) {
690 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
698 * Sends the RSET command to abort and transaction that is
699 * currently in progress. Returns true if successful false
702 * Implements rfc 821: RSET <CRLF>
704 * SMTP CODE SUCCESS: 250
705 * SMTP CODE ERROR : 500,501,504,421
709 public function Reset() {
710 $this->error = null; // so no confusion is caused
712 if(!$this->connected()) {
713 $this->error = array(
714 "error" => "Called Reset() without being connected");
718 fputs($this->smtp_conn,"RSET" . $this->CRLF);
720 $rply = $this->get_lines();
721 $code = substr($rply,0,3);
723 if($this->do_debug >= 2) {
724 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
729 array("error" => "RSET failed",
730 "smtp_code" => $code,
731 "smtp_msg" => substr($rply,4));
732 if($this->do_debug >= 1) {
733 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
742 * Starts a mail transaction from the email address specified in
743 * $from. Returns true if successful or false otherwise. If True
744 * the mail transaction is started and then one or more Recipient
745 * commands may be called followed by a Data command. This command
746 * will send the message to the users terminal if they are logged
747 * in and send them an email.
749 * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
751 * SMTP CODE SUCCESS: 250
752 * SMTP CODE SUCCESS: 552,451,452
753 * SMTP CODE SUCCESS: 500,501,502,421
757 public function SendAndMail($from) {
758 $this->error = null; // so no confusion is caused
760 if(!$this->connected()) {
761 $this->error = array(
762 "error" => "Called SendAndMail() without being connected");
766 fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
768 $rply = $this->get_lines();
769 $code = substr($rply,0,3);
771 if($this->do_debug >= 2) {
772 echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
777 array("error" => "SAML not accepted from server",
778 "smtp_code" => $code,
779 "smtp_msg" => substr($rply,4));
780 if($this->do_debug >= 1) {
781 echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
789 * This is an optional command for SMTP that this class does not
790 * support. This method is here to make the RFC821 Definition
791 * complete for this class and __may__ be implimented in the future
793 * Implements from rfc 821: TURN <CRLF>
795 * SMTP CODE SUCCESS: 250
796 * SMTP CODE FAILURE: 502
797 * SMTP CODE ERROR : 500, 503
801 public function Turn() {
802 $this->error = array("error" => "This method, TURN, of the SMTP ".
803 "is not implemented");
804 if($this->do_debug >= 1) {
805 echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />';
811 * Get the current error
815 public function getError() {
819 /////////////////////////////////////////////////
820 // INTERNAL FUNCTIONS
821 /////////////////////////////////////////////////
824 * Read in as many lines as possible
825 * either before eof or socket timeout occurs on the operation.
826 * With SMTP we can tell if we have more lines to read if the
827 * 4th character is '-' symbol. If it is a space then we don't
828 * need to read anything else.
832 private function get_lines() {
834 // Avoid infinite loop if we don't have a resource to read.
835 if (!is_resource($this->smtp_conn)) {
836 $GLOBALS['log']->warn('SMTP Connection is not a valid resource');
841 while(!feof($this->smtp_conn)) {
842 $str = @fgets($this->smtp_conn,515);
843 if($this->do_debug >= 4) {
844 echo "SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />';
845 echo "SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />';
848 if($this->do_debug >= 4) {
849 echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />';
851 // if 4th character is a space, we are done reading, break the loop
852 if(substr($str,3,1) == " ") { break; }