PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/experimental/php/example/class.smtp.php

https://github.com/matschaffer/fakemail
PHP | 1045 lines | 558 code | 139 blank | 348 comment | 118 complexity | 4b458a4f4ac2e812b2a42ee039fd5876 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. ////////////////////////////////////////////////////
  3. // SMTP - PHP SMTP class
  4. //
  5. // Version 1.02
  6. //
  7. // Define an SMTP class that can be used to connect
  8. // and communicate with any SMTP server. It implements
  9. // all the SMTP functions defined in RFC821 except TURN.
  10. //
  11. // Author: Chris Ryan
  12. //
  13. // License: LGPL, see LICENSE
  14. ////////////////////////////////////////////////////
  15. /**
  16. * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
  17. * commands except TURN which will always return a not implemented
  18. * error. SMTP also provides some utility methods for sending mail
  19. * to an SMTP server.
  20. * @package PHPMailer
  21. * @author Chris Ryan
  22. */
  23. class SMTP
  24. {
  25. /**
  26. * SMTP server port
  27. * @var int
  28. */
  29. var $SMTP_PORT = 25;
  30. /**
  31. * SMTP reply line ending
  32. * @var string
  33. */
  34. var $CRLF = "\r\n";
  35. /**
  36. * Sets whether debugging is turned on
  37. * @var bool
  38. */
  39. var $do_debug; # the level of debug to perform
  40. /**#@+
  41. * @access private
  42. */
  43. var $smtp_conn; # the socket to the server
  44. var $error; # error if any on the last call
  45. var $helo_rply; # the reply the server sent to us for HELO
  46. /**#@-*/
  47. /**
  48. * Initialize the class so that the data is in a known state.
  49. * @access public
  50. * @return void
  51. */
  52. function SMTP() {
  53. $this->smtp_conn = 0;
  54. $this->error = null;
  55. $this->helo_rply = null;
  56. $this->do_debug = 0;
  57. }
  58. /*************************************************************
  59. * CONNECTION FUNCTIONS *
  60. ***********************************************************/
  61. /**
  62. * Connect to the server specified on the port specified.
  63. * If the port is not specified use the default SMTP_PORT.
  64. * If tval is specified then a connection will try and be
  65. * established with the server for that number of seconds.
  66. * If tval is not specified the default is 30 seconds to
  67. * try on the connection.
  68. *
  69. * SMTP CODE SUCCESS: 220
  70. * SMTP CODE FAILURE: 421
  71. * @access public
  72. * @return bool
  73. */
  74. function Connect($host,$port=0,$tval=30) {
  75. # set the error val to null so there is no confusion
  76. $this->error = null;
  77. # make sure we are __not__ connected
  78. if($this->connected()) {
  79. # ok we are connected! what should we do?
  80. # for now we will just give an error saying we
  81. # are already connected
  82. $this->error =
  83. array("error" => "Already connected to a server");
  84. return false;
  85. }
  86. if(empty($port)) {
  87. $port = $this->SMTP_PORT;
  88. }
  89. #connect to the smtp server
  90. $this->smtp_conn = fsockopen($host, # the host of the server
  91. $port, # the port to use
  92. $errno, # error number if any
  93. $errstr, # error message if any
  94. $tval); # give up after ? secs
  95. # verify we connected properly
  96. if(empty($this->smtp_conn)) {
  97. $this->error = array("error" => "Failed to connect to server",
  98. "errno" => $errno,
  99. "errstr" => $errstr);
  100. if($this->do_debug >= 1) {
  101. echo "SMTP -> ERROR: " . $this->error["error"] .
  102. ": $errstr ($errno)" . $this->CRLF;
  103. }
  104. return false;
  105. }
  106. # sometimes the SMTP server takes a little longer to respond
  107. # so we will give it a longer timeout for the first read
  108. // Windows still does not have support for this timeout function
  109. if(substr(PHP_OS, 0, 3) != "WIN")
  110. socket_set_timeout($this->smtp_conn, $tval, 0);
  111. # get any announcement stuff
  112. $announce = $this->get_lines();
  113. # set the timeout of any socket functions at 1/10 of a second
  114. //if(function_exists("socket_set_timeout"))
  115. // socket_set_timeout($this->smtp_conn, 0, 100000);
  116. if($this->do_debug >= 2) {
  117. echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Performs SMTP authentication. Must be run after running the
  123. * Hello() method. Returns true if successfully authenticated.
  124. * @access public
  125. * @return bool
  126. */
  127. function Authenticate($username, $password) {
  128. // Start authentication
  129. fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
  130. $rply = $this->get_lines();
  131. $code = substr($rply,0,3);
  132. if($code != 334) {
  133. $this->error =
  134. array("error" => "AUTH not accepted from server",
  135. "smtp_code" => $code,
  136. "smtp_msg" => substr($rply,4));
  137. if($this->do_debug >= 1) {
  138. echo "SMTP -> ERROR: " . $this->error["error"] .
  139. ": " . $rply . $this->CRLF;
  140. }
  141. return false;
  142. }
  143. // Send encoded username
  144. fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
  145. $rply = $this->get_lines();
  146. $code = substr($rply,0,3);
  147. if($code != 334) {
  148. $this->error =
  149. array("error" => "Username not accepted from server",
  150. "smtp_code" => $code,
  151. "smtp_msg" => substr($rply,4));
  152. if($this->do_debug >= 1) {
  153. echo "SMTP -> ERROR: " . $this->error["error"] .
  154. ": " . $rply . $this->CRLF;
  155. }
  156. return false;
  157. }
  158. // Send encoded password
  159. fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
  160. $rply = $this->get_lines();
  161. $code = substr($rply,0,3);
  162. if($code != 235) {
  163. $this->error =
  164. array("error" => "Password not accepted from server",
  165. "smtp_code" => $code,
  166. "smtp_msg" => substr($rply,4));
  167. if($this->do_debug >= 1) {
  168. echo "SMTP -> ERROR: " . $this->error["error"] .
  169. ": " . $rply . $this->CRLF;
  170. }
  171. return false;
  172. }
  173. return true;
  174. }
  175. /**
  176. * Returns true if connected to a server otherwise false
  177. * @access private
  178. * @return bool
  179. */
  180. function Connected() {
  181. if(!empty($this->smtp_conn)) {
  182. $sock_status = socket_get_status($this->smtp_conn);
  183. if($sock_status["eof"]) {
  184. # hmm this is an odd situation... the socket is
  185. # valid but we aren't connected anymore
  186. if($this->do_debug >= 1) {
  187. echo "SMTP -> NOTICE:" . $this->CRLF .
  188. "EOF caught while checking if connected";
  189. }
  190. $this->Close();
  191. return false;
  192. }
  193. return true; # everything looks good
  194. }
  195. return false;
  196. }
  197. /**
  198. * Closes the socket and cleans up the state of the class.
  199. * It is not considered good to use this function without
  200. * first trying to use QUIT.
  201. * @access public
  202. * @return void
  203. */
  204. function Close() {
  205. $this->error = null; # so there is no confusion
  206. $this->helo_rply = null;
  207. if(!empty($this->smtp_conn)) {
  208. # close the connection and cleanup
  209. fclose($this->smtp_conn);
  210. $this->smtp_conn = 0;
  211. }
  212. }
  213. /***************************************************************
  214. * SMTP COMMANDS *
  215. *************************************************************/
  216. /**
  217. * Issues a data command and sends the msg_data to the server
  218. * finializing the mail transaction. $msg_data is the message
  219. * that is to be send with the headers. Each header needs to be
  220. * on a single line followed by a <CRLF> with the message headers
  221. * and the message body being seperated by and additional <CRLF>.
  222. *
  223. * Implements rfc 821: DATA <CRLF>
  224. *
  225. * SMTP CODE INTERMEDIATE: 354
  226. * [data]
  227. * <CRLF>.<CRLF>
  228. * SMTP CODE SUCCESS: 250
  229. * SMTP CODE FAILURE: 552,554,451,452
  230. * SMTP CODE FAILURE: 451,554
  231. * SMTP CODE ERROR : 500,501,503,421
  232. * @access public
  233. * @return bool
  234. */
  235. function Data($msg_data) {
  236. $this->error = null; # so no confusion is caused
  237. if(!$this->connected()) {
  238. $this->error = array(
  239. "error" => "Called Data() without being connected");
  240. return false;
  241. }
  242. fputs($this->smtp_conn,"DATA" . $this->CRLF);
  243. $rply = $this->get_lines();
  244. $code = substr($rply,0,3);
  245. if($this->do_debug >= 2) {
  246. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  247. }
  248. if($code != 354) {
  249. $this->error =
  250. array("error" => "DATA command not accepted from server",
  251. "smtp_code" => $code,
  252. "smtp_msg" => substr($rply,4));
  253. if($this->do_debug >= 1) {
  254. echo "SMTP -> ERROR: " . $this->error["error"] .
  255. ": " . $rply . $this->CRLF;
  256. }
  257. return false;
  258. }
  259. # the server is ready to accept data!
  260. # according to rfc 821 we should not send more than 1000
  261. # including the CRLF
  262. # characters on a single line so we will break the data up
  263. # into lines by \r and/or \n then if needed we will break
  264. # each of those into smaller lines to fit within the limit.
  265. # in addition we will be looking for lines that start with
  266. # a period '.' and append and additional period '.' to that
  267. # line. NOTE: this does not count towards are limit.
  268. # normalize the line breaks so we know the explode works
  269. $msg_data = str_replace("\r\n","\n",$msg_data);
  270. $msg_data = str_replace("\r","\n",$msg_data);
  271. $lines = explode("\n",$msg_data);
  272. # we need to find a good way to determine is headers are
  273. # in the msg_data or if it is a straight msg body
  274. # currently I'm assuming rfc 822 definitions of msg headers
  275. # and if the first field of the first line (':' sperated)
  276. # does not contain a space then it _should_ be a header
  277. # and we can process all lines before a blank "" line as
  278. # headers.
  279. $field = substr($lines[0],0,strpos($lines[0],":"));
  280. $in_headers = false;
  281. if(!empty($field) && !strstr($field," ")) {
  282. $in_headers = true;
  283. }
  284. $max_line_length = 998; # used below; set here for ease in change
  285. while(list(,$line) = @each($lines)) {
  286. $lines_out = null;
  287. if($line == "" && $in_headers) {
  288. $in_headers = false;
  289. }
  290. # ok we need to break this line up into several
  291. # smaller lines
  292. while(strlen($line) > $max_line_length) {
  293. $pos = strrpos(substr($line,0,$max_line_length)," ");
  294. # Patch to fix DOS attack
  295. if(!$pos) {
  296. $pos = $max_line_length - 1;
  297. }
  298. $lines_out[] = substr($line,0,$pos);
  299. $line = substr($line,$pos + 1);
  300. # if we are processing headers we need to
  301. # add a LWSP-char to the front of the new line
  302. # rfc 822 on long msg headers
  303. if($in_headers) {
  304. $line = "\t" . $line;
  305. }
  306. }
  307. $lines_out[] = $line;
  308. # now send the lines to the server
  309. while(list(,$line_out) = @each($lines_out)) {
  310. if(strlen($line_out) > 0)
  311. {
  312. if(substr($line_out, 0, 1) == ".") {
  313. $line_out = "." . $line_out;
  314. }
  315. }
  316. fputs($this->smtp_conn,$line_out . $this->CRLF);
  317. }
  318. }
  319. # ok all the message data has been sent so lets get this
  320. # over with aleady
  321. fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
  322. $rply = $this->get_lines();
  323. $code = substr($rply,0,3);
  324. if($this->do_debug >= 2) {
  325. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  326. }
  327. if($code != 250) {
  328. $this->error =
  329. array("error" => "DATA not accepted from server",
  330. "smtp_code" => $code,
  331. "smtp_msg" => substr($rply,4));
  332. if($this->do_debug >= 1) {
  333. echo "SMTP -> ERROR: " . $this->error["error"] .
  334. ": " . $rply . $this->CRLF;
  335. }
  336. return false;
  337. }
  338. return true;
  339. }
  340. /**
  341. * Expand takes the name and asks the server to list all the
  342. * people who are members of the _list_. Expand will return
  343. * back and array of the result or false if an error occurs.
  344. * Each value in the array returned has the format of:
  345. * [ <full-name> <sp> ] <path>
  346. * The definition of <path> is defined in rfc 821
  347. *
  348. * Implements rfc 821: EXPN <SP> <string> <CRLF>
  349. *
  350. * SMTP CODE SUCCESS: 250
  351. * SMTP CODE FAILURE: 550
  352. * SMTP CODE ERROR : 500,501,502,504,421
  353. * @access public
  354. * @return string array
  355. */
  356. function Expand($name) {
  357. $this->error = null; # so no confusion is caused
  358. if(!$this->connected()) {
  359. $this->error = array(
  360. "error" => "Called Expand() without being connected");
  361. return false;
  362. }
  363. fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
  364. $rply = $this->get_lines();
  365. $code = substr($rply,0,3);
  366. if($this->do_debug >= 2) {
  367. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  368. }
  369. if($code != 250) {
  370. $this->error =
  371. array("error" => "EXPN not accepted from server",
  372. "smtp_code" => $code,
  373. "smtp_msg" => substr($rply,4));
  374. if($this->do_debug >= 1) {
  375. echo "SMTP -> ERROR: " . $this->error["error"] .
  376. ": " . $rply . $this->CRLF;
  377. }
  378. return false;
  379. }
  380. # parse the reply and place in our array to return to user
  381. $entries = explode($this->CRLF,$rply);
  382. while(list(,$l) = @each($entries)) {
  383. $list[] = substr($l,4);
  384. }
  385. return $list;
  386. }
  387. /**
  388. * Sends the HELO command to the smtp server.
  389. * This makes sure that we and the server are in
  390. * the same known state.
  391. *
  392. * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  393. *
  394. * SMTP CODE SUCCESS: 250
  395. * SMTP CODE ERROR : 500, 501, 504, 421
  396. * @access public
  397. * @return bool
  398. */
  399. function Hello($host="") {
  400. $this->error = null; # so no confusion is caused
  401. if(!$this->connected()) {
  402. $this->error = array(
  403. "error" => "Called Hello() without being connected");
  404. return false;
  405. }
  406. # if a hostname for the HELO wasn't specified determine
  407. # a suitable one to send
  408. if(empty($host)) {
  409. # we need to determine some sort of appopiate default
  410. # to send to the server
  411. $host = "localhost";
  412. }
  413. // Send extended hello first (RFC 2821)
  414. if(!$this->SendHello("EHLO", $host))
  415. {
  416. if(!$this->SendHello("HELO", $host))
  417. return false;
  418. }
  419. return true;
  420. }
  421. /**
  422. * Sends a HELO/EHLO command.
  423. * @access private
  424. * @return bool
  425. */
  426. function SendHello($hello, $host) {
  427. fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
  428. $rply = $this->get_lines();
  429. $code = substr($rply,0,3);
  430. if($this->do_debug >= 2) {
  431. echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
  432. }
  433. if($code != 250) {
  434. $this->error =
  435. array("error" => $hello . " not accepted from server",
  436. "smtp_code" => $code,
  437. "smtp_msg" => substr($rply,4));
  438. if($this->do_debug >= 1) {
  439. echo "SMTP -> ERROR: " . $this->error["error"] .
  440. ": " . $rply . $this->CRLF;
  441. }
  442. return false;
  443. }
  444. $this->helo_rply = $rply;
  445. return true;
  446. }
  447. /**
  448. * Gets help information on the keyword specified. If the keyword
  449. * is not specified then returns generic help, ussually contianing
  450. * A list of keywords that help is available on. This function
  451. * returns the results back to the user. It is up to the user to
  452. * handle the returned data. If an error occurs then false is
  453. * returned with $this->error set appropiately.
  454. *
  455. * Implements rfc 821: HELP [ <SP> <string> ] <CRLF>
  456. *
  457. * SMTP CODE SUCCESS: 211,214
  458. * SMTP CODE ERROR : 500,501,502,504,421
  459. * @access public
  460. * @return string
  461. */
  462. function Help($keyword="") {
  463. $this->error = null; # to avoid confusion
  464. if(!$this->connected()) {
  465. $this->error = array(
  466. "error" => "Called Help() without being connected");
  467. return false;
  468. }
  469. $extra = "";
  470. if(!empty($keyword)) {
  471. $extra = " " . $keyword;
  472. }
  473. fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
  474. $rply = $this->get_lines();
  475. $code = substr($rply,0,3);
  476. if($this->do_debug >= 2) {
  477. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  478. }
  479. if($code != 211 && $code != 214) {
  480. $this->error =
  481. array("error" => "HELP 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"] .
  486. ": " . $rply . $this->CRLF;
  487. }
  488. return false;
  489. }
  490. return $rply;
  491. }
  492. /**
  493. * Starts a mail transaction from the email address specified in
  494. * $from. Returns true if successful or false otherwise. If True
  495. * the mail transaction is started and then one or more Recipient
  496. * commands may be called followed by a Data command.
  497. *
  498. * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  499. *
  500. * SMTP CODE SUCCESS: 250
  501. * SMTP CODE SUCCESS: 552,451,452
  502. * SMTP CODE SUCCESS: 500,501,421
  503. * @access public
  504. * @return bool
  505. */
  506. function Mail($from) {
  507. $this->error = null; # so no confusion is caused
  508. if(!$this->connected()) {
  509. $this->error = array(
  510. "error" => "Called Mail() without being connected");
  511. return false;
  512. }
  513. fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF);
  514. $rply = $this->get_lines();
  515. $code = substr($rply,0,3);
  516. if($this->do_debug >= 2) {
  517. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  518. }
  519. if($code != 250) {
  520. $this->error =
  521. array("error" => "MAIL not accepted from server",
  522. "smtp_code" => $code,
  523. "smtp_msg" => substr($rply,4));
  524. if($this->do_debug >= 1) {
  525. echo "SMTP -> ERROR: " . $this->error["error"] .
  526. ": " . $rply . $this->CRLF;
  527. }
  528. return false;
  529. }
  530. return true;
  531. }
  532. /**
  533. * Sends the command NOOP to the SMTP server.
  534. *
  535. * Implements from rfc 821: NOOP <CRLF>
  536. *
  537. * SMTP CODE SUCCESS: 250
  538. * SMTP CODE ERROR : 500, 421
  539. * @access public
  540. * @return bool
  541. */
  542. function Noop() {
  543. $this->error = null; # so no confusion is caused
  544. if(!$this->connected()) {
  545. $this->error = array(
  546. "error" => "Called Noop() without being connected");
  547. return false;
  548. }
  549. fputs($this->smtp_conn,"NOOP" . $this->CRLF);
  550. $rply = $this->get_lines();
  551. $code = substr($rply,0,3);
  552. if($this->do_debug >= 2) {
  553. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  554. }
  555. if($code != 250) {
  556. $this->error =
  557. array("error" => "NOOP not accepted from server",
  558. "smtp_code" => $code,
  559. "smtp_msg" => substr($rply,4));
  560. if($this->do_debug >= 1) {
  561. echo "SMTP -> ERROR: " . $this->error["error"] .
  562. ": " . $rply . $this->CRLF;
  563. }
  564. return false;
  565. }
  566. return true;
  567. }
  568. /**
  569. * Sends the quit command to the server and then closes the socket
  570. * if there is no error or the $close_on_error argument is true.
  571. *
  572. * Implements from rfc 821: QUIT <CRLF>
  573. *
  574. * SMTP CODE SUCCESS: 221
  575. * SMTP CODE ERROR : 500
  576. * @access public
  577. * @return bool
  578. */
  579. function Quit($close_on_error=true) {
  580. $this->error = null; # so there is no confusion
  581. if(!$this->connected()) {
  582. $this->error = array(
  583. "error" => "Called Quit() without being connected");
  584. return false;
  585. }
  586. # send the quit command to the server
  587. fputs($this->smtp_conn,"quit" . $this->CRLF);
  588. # get any good-bye messages
  589. $byemsg = $this->get_lines();
  590. if($this->do_debug >= 2) {
  591. echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
  592. }
  593. $rval = true;
  594. $e = null;
  595. $code = substr($byemsg,0,3);
  596. if($code != 221) {
  597. # use e as a tmp var cause Close will overwrite $this->error
  598. $e = array("error" => "SMTP server rejected quit command",
  599. "smtp_code" => $code,
  600. "smtp_rply" => substr($byemsg,4));
  601. $rval = false;
  602. if($this->do_debug >= 1) {
  603. echo "SMTP -> ERROR: " . $e["error"] . ": " .
  604. $byemsg . $this->CRLF;
  605. }
  606. }
  607. if(empty($e) || $close_on_error) {
  608. $this->Close();
  609. }
  610. return $rval;
  611. }
  612. /**
  613. * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  614. * Returns true if the recipient was accepted false if it was rejected.
  615. *
  616. * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  617. *
  618. * SMTP CODE SUCCESS: 250,251
  619. * SMTP CODE FAILURE: 550,551,552,553,450,451,452
  620. * SMTP CODE ERROR : 500,501,503,421
  621. * @access public
  622. * @return bool
  623. */
  624. function Recipient($to) {
  625. $this->error = null; # so no confusion is caused
  626. if(!$this->connected()) {
  627. $this->error = array(
  628. "error" => "Called Recipient() without being connected");
  629. return false;
  630. }
  631. fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
  632. $rply = $this->get_lines();
  633. $code = substr($rply,0,3);
  634. if($this->do_debug >= 2) {
  635. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  636. }
  637. if($code != 250 && $code != 251) {
  638. $this->error =
  639. array("error" => "RCPT 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"] .
  644. ": " . $rply . $this->CRLF;
  645. }
  646. return false;
  647. }
  648. return true;
  649. }
  650. /**
  651. * Sends the RSET command to abort and transaction that is
  652. * currently in progress. Returns true if successful false
  653. * otherwise.
  654. *
  655. * Implements rfc 821: RSET <CRLF>
  656. *
  657. * SMTP CODE SUCCESS: 250
  658. * SMTP CODE ERROR : 500,501,504,421
  659. * @access public
  660. * @return bool
  661. */
  662. function Reset() {
  663. $this->error = null; # so no confusion is caused
  664. if(!$this->connected()) {
  665. $this->error = array(
  666. "error" => "Called Reset() without being connected");
  667. return false;
  668. }
  669. fputs($this->smtp_conn,"RSET" . $this->CRLF);
  670. $rply = $this->get_lines();
  671. $code = substr($rply,0,3);
  672. if($this->do_debug >= 2) {
  673. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  674. }
  675. if($code != 250) {
  676. $this->error =
  677. array("error" => "RSET failed",
  678. "smtp_code" => $code,
  679. "smtp_msg" => substr($rply,4));
  680. if($this->do_debug >= 1) {
  681. echo "SMTP -> ERROR: " . $this->error["error"] .
  682. ": " . $rply . $this->CRLF;
  683. }
  684. return false;
  685. }
  686. return true;
  687. }
  688. /**
  689. * Starts a mail transaction from the email address specified in
  690. * $from. Returns true if successful or false otherwise. If True
  691. * the mail transaction is started and then one or more Recipient
  692. * commands may be called followed by a Data command. This command
  693. * will send the message to the users terminal if they are logged
  694. * in.
  695. *
  696. * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF>
  697. *
  698. * SMTP CODE SUCCESS: 250
  699. * SMTP CODE SUCCESS: 552,451,452
  700. * SMTP CODE SUCCESS: 500,501,502,421
  701. * @access public
  702. * @return bool
  703. */
  704. function Send($from) {
  705. $this->error = null; # so no confusion is caused
  706. if(!$this->connected()) {
  707. $this->error = array(
  708. "error" => "Called Send() without being connected");
  709. return false;
  710. }
  711. fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
  712. $rply = $this->get_lines();
  713. $code = substr($rply,0,3);
  714. if($this->do_debug >= 2) {
  715. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  716. }
  717. if($code != 250) {
  718. $this->error =
  719. array("error" => "SEND not accepted from server",
  720. "smtp_code" => $code,
  721. "smtp_msg" => substr($rply,4));
  722. if($this->do_debug >= 1) {
  723. echo "SMTP -> ERROR: " . $this->error["error"] .
  724. ": " . $rply . $this->CRLF;
  725. }
  726. return false;
  727. }
  728. return true;
  729. }
  730. /**
  731. * Starts a mail transaction from the email address specified in
  732. * $from. Returns true if successful or false otherwise. If True
  733. * the mail transaction is started and then one or more Recipient
  734. * commands may be called followed by a Data command. This command
  735. * will send the message to the users terminal if they are logged
  736. * in and send them an email.
  737. *
  738. * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  739. *
  740. * SMTP CODE SUCCESS: 250
  741. * SMTP CODE SUCCESS: 552,451,452
  742. * SMTP CODE SUCCESS: 500,501,502,421
  743. * @access public
  744. * @return bool
  745. */
  746. function SendAndMail($from) {
  747. $this->error = null; # so no confusion is caused
  748. if(!$this->connected()) {
  749. $this->error = array(
  750. "error" => "Called SendAndMail() without being connected");
  751. return false;
  752. }
  753. fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
  754. $rply = $this->get_lines();
  755. $code = substr($rply,0,3);
  756. if($this->do_debug >= 2) {
  757. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  758. }
  759. if($code != 250) {
  760. $this->error =
  761. array("error" => "SAML not accepted from server",
  762. "smtp_code" => $code,
  763. "smtp_msg" => substr($rply,4));
  764. if($this->do_debug >= 1) {
  765. echo "SMTP -> ERROR: " . $this->error["error"] .
  766. ": " . $rply . $this->CRLF;
  767. }
  768. return false;
  769. }
  770. return true;
  771. }
  772. /**
  773. * Starts a mail transaction from the email address specified in
  774. * $from. Returns true if successful or false otherwise. If True
  775. * the mail transaction is started and then one or more Recipient
  776. * commands may be called followed by a Data command. This command
  777. * will send the message to the users terminal if they are logged
  778. * in or mail it to them if they are not.
  779. *
  780. * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF>
  781. *
  782. * SMTP CODE SUCCESS: 250
  783. * SMTP CODE SUCCESS: 552,451,452
  784. * SMTP CODE SUCCESS: 500,501,502,421
  785. * @access public
  786. * @return bool
  787. */
  788. function SendOrMail($from) {
  789. $this->error = null; # so no confusion is caused
  790. if(!$this->connected()) {
  791. $this->error = array(
  792. "error" => "Called SendOrMail() without being connected");
  793. return false;
  794. }
  795. fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
  796. $rply = $this->get_lines();
  797. $code = substr($rply,0,3);
  798. if($this->do_debug >= 2) {
  799. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  800. }
  801. if($code != 250) {
  802. $this->error =
  803. array("error" => "SOML not accepted from server",
  804. "smtp_code" => $code,
  805. "smtp_msg" => substr($rply,4));
  806. if($this->do_debug >= 1) {
  807. echo "SMTP -> ERROR: " . $this->error["error"] .
  808. ": " . $rply . $this->CRLF;
  809. }
  810. return false;
  811. }
  812. return true;
  813. }
  814. /**
  815. * This is an optional command for SMTP that this class does not
  816. * support. This method is here to make the RFC821 Definition
  817. * complete for this class and __may__ be implimented in the future
  818. *
  819. * Implements from rfc 821: TURN <CRLF>
  820. *
  821. * SMTP CODE SUCCESS: 250
  822. * SMTP CODE FAILURE: 502
  823. * SMTP CODE ERROR : 500, 503
  824. * @access public
  825. * @return bool
  826. */
  827. function Turn() {
  828. $this->error = array("error" => "This method, TURN, of the SMTP ".
  829. "is not implemented");
  830. if($this->do_debug >= 1) {
  831. echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
  832. }
  833. return false;
  834. }
  835. /**
  836. * Verifies that the name is recognized by the server.
  837. * Returns false if the name could not be verified otherwise
  838. * the response from the server is returned.
  839. *
  840. * Implements rfc 821: VRFY <SP> <string> <CRLF>
  841. *
  842. * SMTP CODE SUCCESS: 250,251
  843. * SMTP CODE FAILURE: 550,551,553
  844. * SMTP CODE ERROR : 500,501,502,421
  845. * @access public
  846. * @return int
  847. */
  848. function Verify($name) {
  849. $this->error = null; # so no confusion is caused
  850. if(!$this->connected()) {
  851. $this->error = array(
  852. "error" => "Called Verify() without being connected");
  853. return false;
  854. }
  855. fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
  856. $rply = $this->get_lines();
  857. $code = substr($rply,0,3);
  858. if($this->do_debug >= 2) {
  859. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  860. }
  861. if($code != 250 && $code != 251) {
  862. $this->error =
  863. array("error" => "VRFY failed on name '$name'",
  864. "smtp_code" => $code,
  865. "smtp_msg" => substr($rply,4));
  866. if($this->do_debug >= 1) {
  867. echo "SMTP -> ERROR: " . $this->error["error"] .
  868. ": " . $rply . $this->CRLF;
  869. }
  870. return false;
  871. }
  872. return $rply;
  873. }
  874. /*******************************************************************
  875. * INTERNAL FUNCTIONS *
  876. ******************************************************************/
  877. /**
  878. * Read in as many lines as possible
  879. * either before eof or socket timeout occurs on the operation.
  880. * With SMTP we can tell if we have more lines to read if the
  881. * 4th character is '-' symbol. If it is a space then we don't
  882. * need to read anything else.
  883. * @access private
  884. * @return string
  885. */
  886. function get_lines() {
  887. $data = "";
  888. while($str = fgets($this->smtp_conn,515)) {
  889. if($this->do_debug >= 4) {
  890. echo "SMTP -> get_lines(): \$data was \"$data\"" .
  891. $this->CRLF;
  892. echo "SMTP -> get_lines(): \$str is \"$str\"" .
  893. $this->CRLF;
  894. }
  895. $data .= $str;
  896. if($this->do_debug >= 4) {
  897. echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
  898. }
  899. # if the 4th character is a space then we are done reading
  900. # so just break the loop
  901. if(substr($str,3,1) == " ") { break; }
  902. }
  903. return $data;
  904. }
  905. }
  906. ?>