SMTP.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. <?php
  2. namespace KIF\Mail;
  3. /*~ class.smtp.php
  4. .---------------------------------------------------------------------------.
  5. | Software: PHPMailer - PHP email class |
  6. | Version: 5.2 |
  7. | Site: https://code.google.com/a/apache-extras.org/p/phpmailer/ |
  8. | ------------------------------------------------------------------------- |
  9. | Admin: Jim Jagielski (project admininistrator) |
  10. | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  11. | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net |
  12. | : Jim Jagielski (jimjag) jimjag@gmail.com |
  13. | Founder: Brent R. Matzelle (original founder) |
  14. | Copyright (c) 2010-2011, Jim Jagielski. All Rights Reserved. |
  15. | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
  16. | Copyright (c) 2001-2003, Brent R. Matzelle |
  17. | ------------------------------------------------------------------------- |
  18. | License: Distributed under the Lesser General Public License (LGPL) |
  19. | http://www.gnu.org/copyleft/lesser.html |
  20. | This program is distributed in the hope that it will be useful - WITHOUT |
  21. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  22. | FITNESS FOR A PARTICULAR PURPOSE. |
  23. '---------------------------------------------------------------------------'
  24. */
  25. /**
  26. * PHPMailer - PHP SMTP email transport class
  27. * NOTE: Designed for use with PHP version 5 and up
  28. * @package PHPMailer
  29. * @author Andy Prevost
  30. * @author Marcus Bointon
  31. * @copyright 2004 - 2008 Andy Prevost
  32. * @author Jim Jagielski
  33. * @copyright 2010 - 2011 Jim Jagielski
  34. * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
  35. * @version $Id: class.smtp.php 450 2010-06-23 16:46:33Z coolbru $
  36. */
  37. /**
  38. * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
  39. * commands except TURN which will always return a not implemented
  40. * error. SMTP also provides some utility methods for sending mail
  41. * to an SMTP server.
  42. * original author: Chris Ryan
  43. */
  44. class SMTP {
  45. /**
  46. * SMTP server port
  47. * @var int
  48. */
  49. public $SMTP_PORT = 25;
  50. /**
  51. * SMTP reply line ending
  52. * @var string
  53. */
  54. public $CRLF = "\r\n";
  55. /**
  56. * Sets whether debugging is turned on
  57. * @var bool
  58. */
  59. public $do_debug; // the level of debug to perform
  60. /**
  61. * Sets VERP use on/off (default is off)
  62. * @var bool
  63. */
  64. public $do_verp = false;
  65. /**
  66. * Sets the SMTP PHPMailer Version number
  67. * @var string
  68. */
  69. public $Version = '5.2';
  70. /////////////////////////////////////////////////
  71. // PROPERTIES, PRIVATE AND PROTECTED
  72. /////////////////////////////////////////////////
  73. private $smtp_conn; // the socket to the server
  74. private $error; // error if any on the last call
  75. private $helo_rply; // the reply the server sent to us for HELO
  76. /**
  77. * Initialize the class so that the data is in a known state.
  78. * @access public
  79. * @return void
  80. */
  81. public function __construct() {
  82. $this->smtp_conn = 0;
  83. $this->error = null;
  84. $this->helo_rply = null;
  85. $this->do_debug = 0;
  86. }
  87. /////////////////////////////////////////////////
  88. // CONNECTION FUNCTIONS
  89. /////////////////////////////////////////////////
  90. /**
  91. * Connect to the server specified on the port specified.
  92. * If the port is not specified use the default SMTP_PORT.
  93. * If tval is specified then a connection will try and be
  94. * established with the server for that number of seconds.
  95. * If tval is not specified the default is 30 seconds to
  96. * try on the connection.
  97. *
  98. * SMTP CODE SUCCESS: 220
  99. * SMTP CODE FAILURE: 421
  100. * @access public
  101. * @return bool
  102. */
  103. public function Connect($host, $port = 0, $tval = 30) {
  104. // set the error val to null so there is no confusion
  105. $this->error = null;
  106. // make sure we are __not__ connected
  107. if($this->connected()) {
  108. // already connected, generate error
  109. $this->error = array("error" => "Already connected to a server");
  110. return false;
  111. }
  112. if(empty($port)) {
  113. $port = $this->SMTP_PORT;
  114. }
  115. // connect to the smtp server
  116. $this->smtp_conn = @fsockopen($host, // the host of the server
  117. $port, // the port to use
  118. $errno, // error number if any
  119. $errstr, // error message if any
  120. $tval); // give up after ? secs
  121. // verify we connected properly
  122. if(empty($this->smtp_conn)) {
  123. $this->error = array("error" => "Failed to connect to server",
  124. "errno" => $errno,
  125. "errstr" => $errstr);
  126. if($this->do_debug >= 1) {
  127. echo "SMTP -> ERROR: " . $this->error["error"] . ": $errstr ($errno)" . $this->CRLF . '<br />';
  128. }
  129. return false;
  130. }
  131. // SMTP server can take longer to respond, give longer timeout for first read
  132. // Windows does not have support for this timeout function
  133. if(substr(PHP_OS, 0, 3) != "WIN")
  134. socket_set_timeout($this->smtp_conn, $tval, 0);
  135. // get any announcement
  136. $announce = $this->get_lines();
  137. if($this->do_debug >= 2) {
  138. echo "SMTP -> FROM SERVER:" . $announce . $this->CRLF . '<br />';
  139. }
  140. return true;
  141. }
  142. /**
  143. * Initiate a TLS communication with the server.
  144. *
  145. * SMTP CODE 220 Ready to start TLS
  146. * SMTP CODE 501 Syntax error (no parameters allowed)
  147. * SMTP CODE 454 TLS not available due to temporary reason
  148. * @access public
  149. * @return bool success
  150. */
  151. public function StartTLS() {
  152. $this->error = null; # to avoid confusion
  153. if(!$this->connected()) {
  154. $this->error = array("error" => "Called StartTLS() without being connected");
  155. return false;
  156. }
  157. fputs($this->smtp_conn,"STARTTLS" . $this->CRLF);
  158. $rply = $this->get_lines();
  159. $code = substr($rply,0,3);
  160. if($this->do_debug >= 2) {
  161. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  162. }
  163. if($code != 220) {
  164. $this->error =
  165. array("error" => "STARTTLS not accepted from server",
  166. "smtp_code" => $code,
  167. "smtp_msg" => substr($rply,4));
  168. if($this->do_debug >= 1) {
  169. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  170. }
  171. return false;
  172. }
  173. // Begin encrypted connection
  174. if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. /**
  180. * Performs SMTP authentication. Must be run after running the
  181. * Hello() method. Returns true if successfully authenticated.
  182. * @access public
  183. * @return bool
  184. */
  185. public function Authenticate($username, $password) {
  186. // Start authentication
  187. fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
  188. $rply = $this->get_lines();
  189. $code = substr($rply,0,3);
  190. if($code != 334) {
  191. $this->error =
  192. array("error" => "AUTH not accepted from server",
  193. "smtp_code" => $code,
  194. "smtp_msg" => substr($rply,4));
  195. if($this->do_debug >= 1) {
  196. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  197. }
  198. return false;
  199. }
  200. // Send encoded username
  201. fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
  202. $rply = $this->get_lines();
  203. $code = substr($rply,0,3);
  204. if($code != 334) {
  205. $this->error =
  206. array("error" => "Username not accepted from server",
  207. "smtp_code" => $code,
  208. "smtp_msg" => substr($rply,4));
  209. if($this->do_debug >= 1) {
  210. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  211. }
  212. return false;
  213. }
  214. // Send encoded password
  215. fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
  216. $rply = $this->get_lines();
  217. $code = substr($rply,0,3);
  218. if($code != 235) {
  219. $this->error =
  220. array("error" => "Password not accepted from server",
  221. "smtp_code" => $code,
  222. "smtp_msg" => substr($rply,4));
  223. if($this->do_debug >= 1) {
  224. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  225. }
  226. return false;
  227. }
  228. return true;
  229. }
  230. /**
  231. * Returns true if connected to a server otherwise false
  232. * @access public
  233. * @return bool
  234. */
  235. public function Connected() {
  236. if(!empty($this->smtp_conn)) {
  237. $sock_status = socket_get_status($this->smtp_conn);
  238. if($sock_status["eof"]) {
  239. // the socket is valid but we are not connected
  240. if($this->do_debug >= 1) {
  241. echo "SMTP -> NOTICE:" . $this->CRLF . "EOF caught while checking if connected";
  242. }
  243. $this->Close();
  244. return false;
  245. }
  246. return true; // everything looks good
  247. }
  248. return false;
  249. }
  250. /**
  251. * Closes the socket and cleans up the state of the class.
  252. * It is not considered good to use this function without
  253. * first trying to use QUIT.
  254. * @access public
  255. * @return void
  256. */
  257. public function Close() {
  258. $this->error = null; // so there is no confusion
  259. $this->helo_rply = null;
  260. if(!empty($this->smtp_conn)) {
  261. // close the connection and cleanup
  262. fclose($this->smtp_conn);
  263. $this->smtp_conn = 0;
  264. }
  265. }
  266. /////////////////////////////////////////////////
  267. // SMTP COMMANDS
  268. /////////////////////////////////////////////////
  269. /**
  270. * Issues a data command and sends the msg_data to the server
  271. * finializing the mail transaction. $msg_data is the message
  272. * that is to be send with the headers. Each header needs to be
  273. * on a single line followed by a <CRLF> with the message headers
  274. * and the message body being seperated by and additional <CRLF>.
  275. *
  276. * Implements rfc 821: DATA <CRLF>
  277. *
  278. * SMTP CODE INTERMEDIATE: 354
  279. * [data]
  280. * <CRLF>.<CRLF>
  281. * SMTP CODE SUCCESS: 250
  282. * SMTP CODE FAILURE: 552,554,451,452
  283. * SMTP CODE FAILURE: 451,554
  284. * SMTP CODE ERROR : 500,501,503,421
  285. * @access public
  286. * @return bool
  287. */
  288. public function Data($msg_data) {
  289. $this->error = null; // so no confusion is caused
  290. if(!$this->connected()) {
  291. $this->error = array(
  292. "error" => "Called Data() without being connected");
  293. return false;
  294. }
  295. fputs($this->smtp_conn,"DATA" . $this->CRLF);
  296. $rply = $this->get_lines();
  297. $code = substr($rply,0,3);
  298. if($this->do_debug >= 2) {
  299. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  300. }
  301. if($code != 354) {
  302. $this->error =
  303. array("error" => "DATA command not accepted from server",
  304. "smtp_code" => $code,
  305. "smtp_msg" => substr($rply,4));
  306. if($this->do_debug >= 1) {
  307. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  308. }
  309. return false;
  310. }
  311. /* the server is ready to accept data!
  312. * according to rfc 821 we should not send more than 1000
  313. * including the CRLF
  314. * characters on a single line so we will break the data up
  315. * into lines by \r and/or \n then if needed we will break
  316. * each of those into smaller lines to fit within the limit.
  317. * in addition we will be looking for lines that start with
  318. * a period '.' and append and additional period '.' to that
  319. * line. NOTE: this does not count towards limit.
  320. */
  321. // normalize the line breaks so we know the explode works
  322. $msg_data = str_replace("\r\n","\n",$msg_data);
  323. $msg_data = str_replace("\r","\n",$msg_data);
  324. $lines = explode("\n",$msg_data);
  325. /* we need to find a good way to determine is headers are
  326. * in the msg_data or if it is a straight msg body
  327. * currently I am assuming rfc 822 definitions of msg headers
  328. * and if the first field of the first line (':' sperated)
  329. * does not contain a space then it _should_ be a header
  330. * and we can process all lines before a blank "" line as
  331. * headers.
  332. */
  333. $field = substr($lines[0],0,strpos($lines[0],":"));
  334. $in_headers = false;
  335. if(!empty($field) && !strstr($field," ")) {
  336. $in_headers = true;
  337. }
  338. $max_line_length = 998; // used below; set here for ease in change
  339. while(list(,$line) = @each($lines)) {
  340. $lines_out = null;
  341. if($line == "" && $in_headers) {
  342. $in_headers = false;
  343. }
  344. // ok we need to break this line up into several smaller lines
  345. while(strlen($line) > $max_line_length) {
  346. $pos = strrpos(substr($line,0,$max_line_length)," ");
  347. // Patch to fix DOS attack
  348. if(!$pos) {
  349. $pos = $max_line_length - 1;
  350. $lines_out[] = substr($line,0,$pos);
  351. $line = substr($line,$pos);
  352. } else {
  353. $lines_out[] = substr($line,0,$pos);
  354. $line = substr($line,$pos + 1);
  355. }
  356. /* if processing headers add a LWSP-char to the front of new line
  357. * rfc 822 on long msg headers
  358. */
  359. if($in_headers) {
  360. $line = "\t" . $line;
  361. }
  362. }
  363. $lines_out[] = $line;
  364. // send the lines to the server
  365. while(list(,$line_out) = @each($lines_out)) {
  366. if(strlen($line_out) > 0)
  367. {
  368. if(substr($line_out, 0, 1) == ".") {
  369. $line_out = "." . $line_out;
  370. }
  371. }
  372. fputs($this->smtp_conn,$line_out . $this->CRLF);
  373. }
  374. }
  375. // message data has been sent
  376. fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
  377. $rply = $this->get_lines();
  378. $code = substr($rply,0,3);
  379. if($this->do_debug >= 2) {
  380. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  381. }
  382. if($code != 250) {
  383. $this->error =
  384. array("error" => "DATA not accepted from server",
  385. "smtp_code" => $code,
  386. "smtp_msg" => substr($rply,4));
  387. if($this->do_debug >= 1) {
  388. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  389. }
  390. return false;
  391. }
  392. return true;
  393. }
  394. /**
  395. * Sends the HELO command to the smtp server.
  396. * This makes sure that we and the server are in
  397. * the same known state.
  398. *
  399. * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  400. *
  401. * SMTP CODE SUCCESS: 250
  402. * SMTP CODE ERROR : 500, 501, 504, 421
  403. * @access public
  404. * @return bool
  405. */
  406. public function Hello($host = '') {
  407. $this->error = null; // so no confusion is caused
  408. if(!$this->connected()) {
  409. $this->error = array(
  410. "error" => "Called Hello() without being connected");
  411. return false;
  412. }
  413. // if hostname for HELO was not specified send default
  414. if(empty($host)) {
  415. // determine appropriate default to send to server
  416. $host = "localhost";
  417. }
  418. // Send extended hello first (RFC 2821)
  419. if(!$this->SendHello("EHLO", $host)) {
  420. if(!$this->SendHello("HELO", $host)) {
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. /**
  427. * Sends a HELO/EHLO command.
  428. * @access private
  429. * @return bool
  430. */
  431. private function SendHello($hello, $host) {
  432. fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
  433. $rply = $this->get_lines();
  434. $code = substr($rply,0,3);
  435. if($this->do_debug >= 2) {
  436. echo "SMTP -> FROM SERVER: " . $rply . $this->CRLF . '<br />';
  437. }
  438. if($code != 250) {
  439. $this->error =
  440. array("error" => $hello . " not accepted from server",
  441. "smtp_code" => $code,
  442. "smtp_msg" => substr($rply,4));
  443. if($this->do_debug >= 1) {
  444. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  445. }
  446. return false;
  447. }
  448. $this->helo_rply = $rply;
  449. return true;
  450. }
  451. /**
  452. * Starts a mail transaction from the email address specified in
  453. * $from. Returns true if successful or false otherwise. If True
  454. * the mail transaction is started and then one or more Recipient
  455. * commands may be called followed by a Data command.
  456. *
  457. * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  458. *
  459. * SMTP CODE SUCCESS: 250
  460. * SMTP CODE SUCCESS: 552,451,452
  461. * SMTP CODE SUCCESS: 500,501,421
  462. * @access public
  463. * @return bool
  464. */
  465. public function Mail($from) {
  466. $this->error = null; // so no confusion is caused
  467. if(!$this->connected()) {
  468. $this->error = array(
  469. "error" => "Called Mail() without being connected");
  470. return false;
  471. }
  472. $useVerp = ($this->do_verp ? "XVERP" : "");
  473. fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $useVerp . $this->CRLF);
  474. $rply = $this->get_lines();
  475. $code = substr($rply,0,3);
  476. if($this->do_debug >= 2) {
  477. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  478. }
  479. if($code != 250) {
  480. $this->error =
  481. array("error" => "MAIL not accepted from server",
  482. "smtp_code" => $code,
  483. "smtp_msg" => substr($rply,4));
  484. if($this->do_debug >= 1) {
  485. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  486. }
  487. return false;
  488. }
  489. return true;
  490. }
  491. /**
  492. * Sends the quit command to the server and then closes the socket
  493. * if there is no error or the $close_on_error argument is true.
  494. *
  495. * Implements from rfc 821: QUIT <CRLF>
  496. *
  497. * SMTP CODE SUCCESS: 221
  498. * SMTP CODE ERROR : 500
  499. * @access public
  500. * @return bool
  501. */
  502. public function Quit($close_on_error = true) {
  503. $this->error = null; // so there is no confusion
  504. if(!$this->connected()) {
  505. $this->error = array(
  506. "error" => "Called Quit() without being connected");
  507. return false;
  508. }
  509. // send the quit command to the server
  510. fputs($this->smtp_conn,"quit" . $this->CRLF);
  511. // get any good-bye messages
  512. $byemsg = $this->get_lines();
  513. if($this->do_debug >= 2) {
  514. echo "SMTP -> FROM SERVER:" . $byemsg . $this->CRLF . '<br />';
  515. }
  516. $rval = true;
  517. $e = null;
  518. $code = substr($byemsg,0,3);
  519. if($code != 221) {
  520. // use e as a tmp var cause Close will overwrite $this->error
  521. $e = array("error" => "SMTP server rejected quit command",
  522. "smtp_code" => $code,
  523. "smtp_rply" => substr($byemsg,4));
  524. $rval = false;
  525. if($this->do_debug >= 1) {
  526. echo "SMTP -> ERROR: " . $e["error"] . ": " . $byemsg . $this->CRLF . '<br />';
  527. }
  528. }
  529. if(empty($e) || $close_on_error) {
  530. $this->Close();
  531. }
  532. return $rval;
  533. }
  534. /**
  535. * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  536. * Returns true if the recipient was accepted false if it was rejected.
  537. *
  538. * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  539. *
  540. * SMTP CODE SUCCESS: 250,251
  541. * SMTP CODE FAILURE: 550,551,552,553,450,451,452
  542. * SMTP CODE ERROR : 500,501,503,421
  543. * @access public
  544. * @return bool
  545. */
  546. public function Recipient($to) {
  547. $this->error = null; // so no confusion is caused
  548. if(!$this->connected()) {
  549. $this->error = array(
  550. "error" => "Called Recipient() without being connected");
  551. return false;
  552. }
  553. fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
  554. $rply = $this->get_lines();
  555. $code = substr($rply,0,3);
  556. if($this->do_debug >= 2) {
  557. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  558. }
  559. if($code != 250 && $code != 251) {
  560. $this->error =
  561. array("error" => "RCPT not accepted from server",
  562. "smtp_code" => $code,
  563. "smtp_msg" => substr($rply,4));
  564. if($this->do_debug >= 1) {
  565. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  566. }
  567. return false;
  568. }
  569. return true;
  570. }
  571. /**
  572. * Sends the RSET command to abort and transaction that is
  573. * currently in progress. Returns true if successful false
  574. * otherwise.
  575. *
  576. * Implements rfc 821: RSET <CRLF>
  577. *
  578. * SMTP CODE SUCCESS: 250
  579. * SMTP CODE ERROR : 500,501,504,421
  580. * @access public
  581. * @return bool
  582. */
  583. public function Reset() {
  584. $this->error = null; // so no confusion is caused
  585. if(!$this->connected()) {
  586. $this->error = array(
  587. "error" => "Called Reset() without being connected");
  588. return false;
  589. }
  590. fputs($this->smtp_conn,"RSET" . $this->CRLF);
  591. $rply = $this->get_lines();
  592. $code = substr($rply,0,3);
  593. if($this->do_debug >= 2) {
  594. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  595. }
  596. if($code != 250) {
  597. $this->error =
  598. array("error" => "RSET failed",
  599. "smtp_code" => $code,
  600. "smtp_msg" => substr($rply,4));
  601. if($this->do_debug >= 1) {
  602. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  603. }
  604. return false;
  605. }
  606. return true;
  607. }
  608. /**
  609. * Starts a mail transaction from the email address specified in
  610. * $from. Returns true if successful or false otherwise. If True
  611. * the mail transaction is started and then one or more Recipient
  612. * commands may be called followed by a Data command. This command
  613. * will send the message to the users terminal if they are logged
  614. * in and send them an email.
  615. *
  616. * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  617. *
  618. * SMTP CODE SUCCESS: 250
  619. * SMTP CODE SUCCESS: 552,451,452
  620. * SMTP CODE SUCCESS: 500,501,502,421
  621. * @access public
  622. * @return bool
  623. */
  624. public function SendAndMail($from) {
  625. $this->error = null; // so no confusion is caused
  626. if(!$this->connected()) {
  627. $this->error = array(
  628. "error" => "Called SendAndMail() without being connected");
  629. return false;
  630. }
  631. fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
  632. $rply = $this->get_lines();
  633. $code = substr($rply,0,3);
  634. if($this->do_debug >= 2) {
  635. echo "SMTP -> FROM SERVER:" . $rply . $this->CRLF . '<br />';
  636. }
  637. if($code != 250) {
  638. $this->error =
  639. array("error" => "SAML not accepted from server",
  640. "smtp_code" => $code,
  641. "smtp_msg" => substr($rply,4));
  642. if($this->do_debug >= 1) {
  643. echo "SMTP -> ERROR: " . $this->error["error"] . ": " . $rply . $this->CRLF . '<br />';
  644. }
  645. return false;
  646. }
  647. return true;
  648. }
  649. /**
  650. * This is an optional command for SMTP that this class does not
  651. * support. This method is here to make the RFC821 Definition
  652. * complete for this class and __may__ be implimented in the future
  653. *
  654. * Implements from rfc 821: TURN <CRLF>
  655. *
  656. * SMTP CODE SUCCESS: 250
  657. * SMTP CODE FAILURE: 502
  658. * SMTP CODE ERROR : 500, 503
  659. * @access public
  660. * @return bool
  661. */
  662. public function Turn() {
  663. $this->error = array("error" => "This method, TURN, of the SMTP ".
  664. "is not implemented");
  665. if($this->do_debug >= 1) {
  666. echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF . '<br />';
  667. }
  668. return false;
  669. }
  670. /**
  671. * Get the current error
  672. * @access public
  673. * @return array
  674. */
  675. public function getError() {
  676. return $this->error;
  677. }
  678. /////////////////////////////////////////////////
  679. // INTERNAL FUNCTIONS
  680. /////////////////////////////////////////////////
  681. /**
  682. * Read in as many lines as possible
  683. * either before eof or socket timeout occurs on the operation.
  684. * With SMTP we can tell if we have more lines to read if the
  685. * 4th character is '-' symbol. If it is a space then we don't
  686. * need to read anything else.
  687. * @access private
  688. * @return string
  689. */
  690. private function get_lines() {
  691. $data = "";
  692. while($str = @fgets($this->smtp_conn,515)) {
  693. if($this->do_debug >= 4) {
  694. echo "SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />';
  695. echo "SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />';
  696. }
  697. $data .= $str;
  698. if($this->do_debug >= 4) {
  699. echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />';
  700. }
  701. // if 4th character is a space, we are done reading, break the loop
  702. if(substr($str,3,1) == " ") { break; }
  703. }
  704. return $data;
  705. }
  706. }