PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/example/class.smtp.php

https://github.com/matschaffer/fakemail
PHP | 1039 lines | 555 code | 137 blank | 347 comment | 117 complexity | 04537022132c0dc9787117e60f87979c 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. $lines_out[] = substr($line,0,$pos);
  295. $line = substr($line,$pos + 1);
  296. # if we are processing headers we need to
  297. # add a LWSP-char to the front of the new line
  298. # rfc 822 on long msg headers
  299. if($in_headers) {
  300. $line = "\t" . $line;
  301. }
  302. }
  303. $lines_out[] = $line;
  304. # now send the lines to the server
  305. while(list(,$line_out) = @each($lines_out)) {
  306. if(strlen($line_out) > 0)
  307. {
  308. if(substr($line_out, 0, 1) == ".") {
  309. $line_out = "." . $line_out;
  310. }
  311. }
  312. fputs($this->smtp_conn,$line_out . $this->CRLF);
  313. }
  314. }
  315. # ok all the message data has been sent so lets get this
  316. # over with aleady
  317. fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
  318. $rply = $this->get_lines();
  319. $code = substr($rply,0,3);
  320. if($this->do_debug >= 2) {
  321. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  322. }
  323. if($code != 250) {
  324. $this->error =
  325. array("error" => "DATA not accepted from server",
  326. "smtp_code" => $code,
  327. "smtp_msg" => substr($rply,4));
  328. if($this->do_debug >= 1) {
  329. echo "SMTP -> ERROR: " . $this->error["error"] .
  330. ": " . $rply . $this->CRLF;
  331. }
  332. return false;
  333. }
  334. return true;
  335. }
  336. /**
  337. * Expand takes the name and asks the server to list all the
  338. * people who are members of the _list_. Expand will return
  339. * back and array of the result or false if an error occurs.
  340. * Each value in the array returned has the format of:
  341. * [ <full-name> <sp> ] <path>
  342. * The definition of <path> is defined in rfc 821
  343. *
  344. * Implements rfc 821: EXPN <SP> <string> <CRLF>
  345. *
  346. * SMTP CODE SUCCESS: 250
  347. * SMTP CODE FAILURE: 550
  348. * SMTP CODE ERROR : 500,501,502,504,421
  349. * @access public
  350. * @return string array
  351. */
  352. function Expand($name) {
  353. $this->error = null; # so no confusion is caused
  354. if(!$this->connected()) {
  355. $this->error = array(
  356. "error" => "Called Expand() without being connected");
  357. return false;
  358. }
  359. fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
  360. $rply = $this->get_lines();
  361. $code = substr($rply,0,3);
  362. if($this->do_debug >= 2) {
  363. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  364. }
  365. if($code != 250) {
  366. $this->error =
  367. array("error" => "EXPN not accepted from server",
  368. "smtp_code" => $code,
  369. "smtp_msg" => substr($rply,4));
  370. if($this->do_debug >= 1) {
  371. echo "SMTP -> ERROR: " . $this->error["error"] .
  372. ": " . $rply . $this->CRLF;
  373. }
  374. return false;
  375. }
  376. # parse the reply and place in our array to return to user
  377. $entries = explode($this->CRLF,$rply);
  378. while(list(,$l) = @each($entries)) {
  379. $list[] = substr($l,4);
  380. }
  381. return $list;
  382. }
  383. /**
  384. * Sends the HELO command to the smtp server.
  385. * This makes sure that we and the server are in
  386. * the same known state.
  387. *
  388. * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  389. *
  390. * SMTP CODE SUCCESS: 250
  391. * SMTP CODE ERROR : 500, 501, 504, 421
  392. * @access public
  393. * @return bool
  394. */
  395. function Hello($host="") {
  396. $this->error = null; # so no confusion is caused
  397. if(!$this->connected()) {
  398. $this->error = array(
  399. "error" => "Called Hello() without being connected");
  400. return false;
  401. }
  402. # if a hostname for the HELO wasn't specified determine
  403. # a suitable one to send
  404. if(empty($host)) {
  405. # we need to determine some sort of appopiate default
  406. # to send to the server
  407. $host = "localhost";
  408. }
  409. // Send extended hello first (RFC 2821)
  410. if(!$this->SendHello("EHLO", $host))
  411. {
  412. if(!$this->SendHello("HELO", $host))
  413. return false;
  414. }
  415. return true;
  416. }
  417. /**
  418. * Sends a HELO/EHLO command.
  419. * @access private
  420. * @return bool
  421. */
  422. function SendHello($hello, $host) {
  423. fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
  424. $rply = $this->get_lines();
  425. $code = substr($rply,0,3);
  426. if($this->do_debug >= 2) {
  427. echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
  428. }
  429. if($code != 250) {
  430. $this->error =
  431. array("error" => $hello . " not accepted from server",
  432. "smtp_code" => $code,
  433. "smtp_msg" => substr($rply,4));
  434. if($this->do_debug >= 1) {
  435. echo "SMTP -> ERROR: " . $this->error["error"] .
  436. ": " . $rply . $this->CRLF;
  437. }
  438. return false;
  439. }
  440. $this->helo_rply = $rply;
  441. return true;
  442. }
  443. /**
  444. * Gets help information on the keyword specified. If the keyword
  445. * is not specified then returns generic help, ussually contianing
  446. * A list of keywords that help is available on. This function
  447. * returns the results back to the user. It is up to the user to
  448. * handle the returned data. If an error occurs then false is
  449. * returned with $this->error set appropiately.
  450. *
  451. * Implements rfc 821: HELP [ <SP> <string> ] <CRLF>
  452. *
  453. * SMTP CODE SUCCESS: 211,214
  454. * SMTP CODE ERROR : 500,501,502,504,421
  455. * @access public
  456. * @return string
  457. */
  458. function Help($keyword="") {
  459. $this->error = null; # to avoid confusion
  460. if(!$this->connected()) {
  461. $this->error = array(
  462. "error" => "Called Help() without being connected");
  463. return false;
  464. }
  465. $extra = "";
  466. if(!empty($keyword)) {
  467. $extra = " " . $keyword;
  468. }
  469. fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
  470. $rply = $this->get_lines();
  471. $code = substr($rply,0,3);
  472. if($this->do_debug >= 2) {
  473. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  474. }
  475. if($code != 211 && $code != 214) {
  476. $this->error =
  477. array("error" => "HELP not accepted from server",
  478. "smtp_code" => $code,
  479. "smtp_msg" => substr($rply,4));
  480. if($this->do_debug >= 1) {
  481. echo "SMTP -> ERROR: " . $this->error["error"] .
  482. ": " . $rply . $this->CRLF;
  483. }
  484. return false;
  485. }
  486. return $rply;
  487. }
  488. /**
  489. * Starts a mail transaction from the email address specified in
  490. * $from. Returns true if successful or false otherwise. If True
  491. * the mail transaction is started and then one or more Recipient
  492. * commands may be called followed by a Data command.
  493. *
  494. * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  495. *
  496. * SMTP CODE SUCCESS: 250
  497. * SMTP CODE SUCCESS: 552,451,452
  498. * SMTP CODE SUCCESS: 500,501,421
  499. * @access public
  500. * @return bool
  501. */
  502. function Mail($from) {
  503. $this->error = null; # so no confusion is caused
  504. if(!$this->connected()) {
  505. $this->error = array(
  506. "error" => "Called Mail() without being connected");
  507. return false;
  508. }
  509. fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF);
  510. $rply = $this->get_lines();
  511. $code = substr($rply,0,3);
  512. if($this->do_debug >= 2) {
  513. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  514. }
  515. if($code != 250) {
  516. $this->error =
  517. array("error" => "MAIL not accepted from server",
  518. "smtp_code" => $code,
  519. "smtp_msg" => substr($rply,4));
  520. if($this->do_debug >= 1) {
  521. echo "SMTP -> ERROR: " . $this->error["error"] .
  522. ": " . $rply . $this->CRLF;
  523. }
  524. return false;
  525. }
  526. return true;
  527. }
  528. /**
  529. * Sends the command NOOP to the SMTP server.
  530. *
  531. * Implements from rfc 821: NOOP <CRLF>
  532. *
  533. * SMTP CODE SUCCESS: 250
  534. * SMTP CODE ERROR : 500, 421
  535. * @access public
  536. * @return bool
  537. */
  538. function Noop() {
  539. $this->error = null; # so no confusion is caused
  540. if(!$this->connected()) {
  541. $this->error = array(
  542. "error" => "Called Noop() without being connected");
  543. return false;
  544. }
  545. fputs($this->smtp_conn,"NOOP" . $this->CRLF);
  546. $rply = $this->get_lines();
  547. $code = substr($rply,0,3);
  548. if($this->do_debug >= 2) {
  549. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  550. }
  551. if($code != 250) {
  552. $this->error =
  553. array("error" => "NOOP not accepted from server",
  554. "smtp_code" => $code,
  555. "smtp_msg" => substr($rply,4));
  556. if($this->do_debug >= 1) {
  557. echo "SMTP -> ERROR: " . $this->error["error"] .
  558. ": " . $rply . $this->CRLF;
  559. }
  560. return false;
  561. }
  562. return true;
  563. }
  564. /**
  565. * Sends the quit command to the server and then closes the socket
  566. * if there is no error or the $close_on_error argument is true.
  567. *
  568. * Implements from rfc 821: QUIT <CRLF>
  569. *
  570. * SMTP CODE SUCCESS: 221
  571. * SMTP CODE ERROR : 500
  572. * @access public
  573. * @return bool
  574. */
  575. function Quit($close_on_error=true) {
  576. $this->error = null; # so there is no confusion
  577. if(!$this->connected()) {
  578. $this->error = array(
  579. "error" => "Called Quit() without being connected");
  580. return false;
  581. }
  582. # send the quit command to the server
  583. fputs($this->smtp_conn,"quit" . $this->CRLF);
  584. # get any good-bye messages
  585. $byemsg = $this->get_lines();
  586. if($this->do_debug >= 2) {
  587. echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
  588. }
  589. $rval = true;
  590. $e = null;
  591. $code = substr($byemsg,0,3);
  592. if($code != 221) {
  593. # use e as a tmp var cause Close will overwrite $this->error
  594. $e = array("error" => "SMTP server rejected quit command",
  595. "smtp_code" => $code,
  596. "smtp_rply" => substr($byemsg,4));
  597. $rval = false;
  598. if($this->do_debug >= 1) {
  599. echo "SMTP -> ERROR: " . $e["error"] . ": " .
  600. $byemsg . $this->CRLF;
  601. }
  602. }
  603. if(empty($e) || $close_on_error) {
  604. $this->Close();
  605. }
  606. return $rval;
  607. }
  608. /**
  609. * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  610. * Returns true if the recipient was accepted false if it was rejected.
  611. *
  612. * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  613. *
  614. * SMTP CODE SUCCESS: 250,251
  615. * SMTP CODE FAILURE: 550,551,552,553,450,451,452
  616. * SMTP CODE ERROR : 500,501,503,421
  617. * @access public
  618. * @return bool
  619. */
  620. function Recipient($to) {
  621. $this->error = null; # so no confusion is caused
  622. if(!$this->connected()) {
  623. $this->error = array(
  624. "error" => "Called Recipient() without being connected");
  625. return false;
  626. }
  627. fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
  628. $rply = $this->get_lines();
  629. $code = substr($rply,0,3);
  630. if($this->do_debug >= 2) {
  631. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  632. }
  633. if($code != 250 && $code != 251) {
  634. $this->error =
  635. array("error" => "RCPT not accepted from server",
  636. "smtp_code" => $code,
  637. "smtp_msg" => substr($rply,4));
  638. if($this->do_debug >= 1) {
  639. echo "SMTP -> ERROR: " . $this->error["error"] .
  640. ": " . $rply . $this->CRLF;
  641. }
  642. return false;
  643. }
  644. return true;
  645. }
  646. /**
  647. * Sends the RSET command to abort and transaction that is
  648. * currently in progress. Returns true if successful false
  649. * otherwise.
  650. *
  651. * Implements rfc 821: RSET <CRLF>
  652. *
  653. * SMTP CODE SUCCESS: 250
  654. * SMTP CODE ERROR : 500,501,504,421
  655. * @access public
  656. * @return bool
  657. */
  658. function Reset() {
  659. $this->error = null; # so no confusion is caused
  660. if(!$this->connected()) {
  661. $this->error = array(
  662. "error" => "Called Reset() without being connected");
  663. return false;
  664. }
  665. fputs($this->smtp_conn,"RSET" . $this->CRLF);
  666. $rply = $this->get_lines();
  667. $code = substr($rply,0,3);
  668. if($this->do_debug >= 2) {
  669. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  670. }
  671. if($code != 250) {
  672. $this->error =
  673. array("error" => "RSET failed",
  674. "smtp_code" => $code,
  675. "smtp_msg" => substr($rply,4));
  676. if($this->do_debug >= 1) {
  677. echo "SMTP -> ERROR: " . $this->error["error"] .
  678. ": " . $rply . $this->CRLF;
  679. }
  680. return false;
  681. }
  682. return true;
  683. }
  684. /**
  685. * Starts a mail transaction from the email address specified in
  686. * $from. Returns true if successful or false otherwise. If True
  687. * the mail transaction is started and then one or more Recipient
  688. * commands may be called followed by a Data command. This command
  689. * will send the message to the users terminal if they are logged
  690. * in.
  691. *
  692. * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF>
  693. *
  694. * SMTP CODE SUCCESS: 250
  695. * SMTP CODE SUCCESS: 552,451,452
  696. * SMTP CODE SUCCESS: 500,501,502,421
  697. * @access public
  698. * @return bool
  699. */
  700. function Send($from) {
  701. $this->error = null; # so no confusion is caused
  702. if(!$this->connected()) {
  703. $this->error = array(
  704. "error" => "Called Send() without being connected");
  705. return false;
  706. }
  707. fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
  708. $rply = $this->get_lines();
  709. $code = substr($rply,0,3);
  710. if($this->do_debug >= 2) {
  711. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  712. }
  713. if($code != 250) {
  714. $this->error =
  715. array("error" => "SEND not accepted from server",
  716. "smtp_code" => $code,
  717. "smtp_msg" => substr($rply,4));
  718. if($this->do_debug >= 1) {
  719. echo "SMTP -> ERROR: " . $this->error["error"] .
  720. ": " . $rply . $this->CRLF;
  721. }
  722. return false;
  723. }
  724. return true;
  725. }
  726. /**
  727. * Starts a mail transaction from the email address specified in
  728. * $from. Returns true if successful or false otherwise. If True
  729. * the mail transaction is started and then one or more Recipient
  730. * commands may be called followed by a Data command. This command
  731. * will send the message to the users terminal if they are logged
  732. * in and send them an email.
  733. *
  734. * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  735. *
  736. * SMTP CODE SUCCESS: 250
  737. * SMTP CODE SUCCESS: 552,451,452
  738. * SMTP CODE SUCCESS: 500,501,502,421
  739. * @access public
  740. * @return bool
  741. */
  742. function SendAndMail($from) {
  743. $this->error = null; # so no confusion is caused
  744. if(!$this->connected()) {
  745. $this->error = array(
  746. "error" => "Called SendAndMail() without being connected");
  747. return false;
  748. }
  749. fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
  750. $rply = $this->get_lines();
  751. $code = substr($rply,0,3);
  752. if($this->do_debug >= 2) {
  753. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  754. }
  755. if($code != 250) {
  756. $this->error =
  757. array("error" => "SAML not accepted from server",
  758. "smtp_code" => $code,
  759. "smtp_msg" => substr($rply,4));
  760. if($this->do_debug >= 1) {
  761. echo "SMTP -> ERROR: " . $this->error["error"] .
  762. ": " . $rply . $this->CRLF;
  763. }
  764. return false;
  765. }
  766. return true;
  767. }
  768. /**
  769. * Starts a mail transaction from the email address specified in
  770. * $from. Returns true if successful or false otherwise. If True
  771. * the mail transaction is started and then one or more Recipient
  772. * commands may be called followed by a Data command. This command
  773. * will send the message to the users terminal if they are logged
  774. * in or mail it to them if they are not.
  775. *
  776. * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF>
  777. *
  778. * SMTP CODE SUCCESS: 250
  779. * SMTP CODE SUCCESS: 552,451,452
  780. * SMTP CODE SUCCESS: 500,501,502,421
  781. * @access public
  782. * @return bool
  783. */
  784. function SendOrMail($from) {
  785. $this->error = null; # so no confusion is caused
  786. if(!$this->connected()) {
  787. $this->error = array(
  788. "error" => "Called SendOrMail() without being connected");
  789. return false;
  790. }
  791. fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
  792. $rply = $this->get_lines();
  793. $code = substr($rply,0,3);
  794. if($this->do_debug >= 2) {
  795. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  796. }
  797. if($code != 250) {
  798. $this->error =
  799. array("error" => "SOML not accepted from server",
  800. "smtp_code" => $code,
  801. "smtp_msg" => substr($rply,4));
  802. if($this->do_debug >= 1) {
  803. echo "SMTP -> ERROR: " . $this->error["error"] .
  804. ": " . $rply . $this->CRLF;
  805. }
  806. return false;
  807. }
  808. return true;
  809. }
  810. /**
  811. * This is an optional command for SMTP that this class does not
  812. * support. This method is here to make the RFC821 Definition
  813. * complete for this class and __may__ be implimented in the future
  814. *
  815. * Implements from rfc 821: TURN <CRLF>
  816. *
  817. * SMTP CODE SUCCESS: 250
  818. * SMTP CODE FAILURE: 502
  819. * SMTP CODE ERROR : 500, 503
  820. * @access public
  821. * @return bool
  822. */
  823. function Turn() {
  824. $this->error = array("error" => "This method, TURN, of the SMTP ".
  825. "is not implemented");
  826. if($this->do_debug >= 1) {
  827. echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
  828. }
  829. return false;
  830. }
  831. /**
  832. * Verifies that the name is recognized by the server.
  833. * Returns false if the name could not be verified otherwise
  834. * the response from the server is returned.
  835. *
  836. * Implements rfc 821: VRFY <SP> <string> <CRLF>
  837. *
  838. * SMTP CODE SUCCESS: 250,251
  839. * SMTP CODE FAILURE: 550,551,553
  840. * SMTP CODE ERROR : 500,501,502,421
  841. * @access public
  842. * @return int
  843. */
  844. function Verify($name) {
  845. $this->error = null; # so no confusion is caused
  846. if(!$this->connected()) {
  847. $this->error = array(
  848. "error" => "Called Verify() without being connected");
  849. return false;
  850. }
  851. fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
  852. $rply = $this->get_lines();
  853. $code = substr($rply,0,3);
  854. if($this->do_debug >= 2) {
  855. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  856. }
  857. if($code != 250 && $code != 251) {
  858. $this->error =
  859. array("error" => "VRFY failed on name '$name'",
  860. "smtp_code" => $code,
  861. "smtp_msg" => substr($rply,4));
  862. if($this->do_debug >= 1) {
  863. echo "SMTP -> ERROR: " . $this->error["error"] .
  864. ": " . $rply . $this->CRLF;
  865. }
  866. return false;
  867. }
  868. return $rply;
  869. }
  870. /*******************************************************************
  871. * INTERNAL FUNCTIONS *
  872. ******************************************************************/
  873. /**
  874. * Read in as many lines as possible
  875. * either before eof or socket timeout occurs on the operation.
  876. * With SMTP we can tell if we have more lines to read if the
  877. * 4th character is '-' symbol. If it is a space then we don't
  878. * need to read anything else.
  879. * @access private
  880. * @return string
  881. */
  882. function get_lines() {
  883. $data = "";
  884. while($str = fgets($this->smtp_conn,515)) {
  885. if($this->do_debug >= 4) {
  886. echo "SMTP -> get_lines(): \$data was \"$data\"" .
  887. $this->CRLF;
  888. echo "SMTP -> get_lines(): \$str is \"$str\"" .
  889. $this->CRLF;
  890. }
  891. $data .= $str;
  892. if($this->do_debug >= 4) {
  893. echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
  894. }
  895. # if the 4th character is a space then we are done reading
  896. # so just break the loop
  897. if(substr($str,3,1) == " ") { break; }
  898. }
  899. return $data;
  900. }
  901. }
  902. ?>