class.smtp.php 25 KB

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