SMTP

PHPMailer RFC821 SMTP email transport class.

Defined (1)

The class is defined in the following location(s).

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