PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/phpmailer/smtp.php

https://github.com/MilkZoft/zan
PHP | 684 lines | 553 code | 131 blank | 0 comment | 118 complexity | f2dc22d13d48bfdc91c15a92087db1b4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class SMTP
  3. {
  4. public $SMTP_PORT = 25;
  5. public $CRLF = "\r\n";
  6. public $do_debug;
  7. public $smtp_conn;
  8. public $error;
  9. public $helo_rply;
  10. public function __construct() {
  11. $this->smtp_conn = 0;
  12. $this->error = null;
  13. $this->helo_rply = null;
  14. $this->do_debug = 0;
  15. }
  16. public function Connect($host,$port=0,$tval=30) {
  17. $this->error = null;
  18. if($this->connected()) {
  19. $this->error =
  20. array("error" => "Already connected to a server");
  21. return false;
  22. }
  23. if(empty($port)) {
  24. $port = $this->SMTP_PORT;
  25. }
  26. $this->smtp_conn = @fsockopen($host, $port, $errno, $errstr, $tval);
  27. if(empty($this->smtp_conn)) {
  28. $this->error = array("error" => "Failed to connect to server",
  29. "errno" => $errno,
  30. "errstr" => $errstr);
  31. if($this->do_debug >= 1) {
  32. echo "SMTP -> ERROR: " . $this->error["error"] .
  33. ": $errstr ($errno)" . $this->CRLF;
  34. }
  35. return false;
  36. }
  37. if(substr(PHP_OS, 0, 3) != "WIN")
  38. socket_set_timeout($this->smtp_conn, $tval, 0);
  39. $announce = $this->get_lines();
  40. if($this->do_debug >= 2) {
  41. echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
  42. }
  43. return true;
  44. }
  45. public function Authenticate($username, $password) {
  46. fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
  47. $rply = $this->get_lines();
  48. $code = substr($rply,0,3);
  49. if($code != 334) {
  50. $this->error =
  51. array("error" => "AUTH not accepted from server",
  52. "smtp_code" => $code,
  53. "smtp_msg" => substr($rply,4));
  54. if($this->do_debug >= 1) {
  55. echo "SMTP -> ERROR: " . $this->error["error"] .
  56. ": " . $rply . $this->CRLF;
  57. }
  58. return false;
  59. }
  60. fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
  61. $rply = $this->get_lines();
  62. $code = substr($rply,0,3);
  63. if($code != 334) {
  64. $this->error =
  65. array("error" => "Username not accepted from server",
  66. "smtp_code" => $code,
  67. "smtp_msg" => substr($rply,4));
  68. if($this->do_debug >= 1) {
  69. echo "SMTP -> ERROR: " . $this->error["error"] .
  70. ": " . $rply . $this->CRLF;
  71. }
  72. return false;
  73. }
  74. fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
  75. $rply = $this->get_lines();
  76. $code = substr($rply,0,3);
  77. if($code != 235) {
  78. $this->error =
  79. array("error" => "Password not accepted from server",
  80. "smtp_code" => $code,
  81. "smtp_msg" => substr($rply,4));
  82. if($this->do_debug >= 1) {
  83. echo "SMTP -> ERROR: " . $this->error["error"] .
  84. ": " . $rply . $this->CRLF;
  85. }
  86. return false;
  87. }
  88. return true;
  89. }
  90. public function Connected() {
  91. if(!empty($this->smtp_conn)) {
  92. $sock_status = socket_get_status($this->smtp_conn);
  93. if($sock_status["eof"]) {
  94. if($this->do_debug >= 1) {
  95. echo "SMTP -> NOTICE:" . $this->CRLF .
  96. "EOF caught while checking if connected";
  97. }
  98. $this->Close();
  99. return false;
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. public function Close() {
  106. $this->error = null;
  107. $this->helo_rply = null;
  108. if(!empty($this->smtp_conn)) {
  109. fclose($this->smtp_conn);
  110. $this->smtp_conn = 0;
  111. }
  112. }
  113. public function Data($msg_data) {
  114. $this->error = null;
  115. if(!$this->connected()) {
  116. $this->error = array(
  117. "error" => "Called Data() without being connected");
  118. return false;
  119. }
  120. fputs($this->smtp_conn,"DATA" . $this->CRLF);
  121. $rply = $this->get_lines();
  122. $code = substr($rply,0,3);
  123. if($this->do_debug >= 2) {
  124. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  125. }
  126. if($code != 354) {
  127. $this->error =
  128. array("error" => "DATA command not accepted from server",
  129. "smtp_code" => $code,
  130. "smtp_msg" => substr($rply,4));
  131. if($this->do_debug >= 1) {
  132. echo "SMTP -> ERROR: " . $this->error["error"] .
  133. ": " . $rply . $this->CRLF;
  134. }
  135. return false;
  136. }
  137. $msg_data = str_replace("\r\n","\n",$msg_data);
  138. $msg_data = str_replace("\r","\n",$msg_data);
  139. $lines = explode("\n",$msg_data);
  140. $field = substr($lines[0],0,strpos($lines[0],":"));
  141. $in_headers = false;
  142. if(!empty($field) && !strstr($field," ")) {
  143. $in_headers = true;
  144. }
  145. $max_line_length = 998;
  146. while(list(,$line) = @each($lines)) {
  147. $lines_out = null;
  148. if($line == "" && $in_headers) {
  149. $in_headers = false;
  150. }
  151. while(strlen($line) > $max_line_length) {
  152. $pos = strrpos(substr($line,0,$max_line_length)," ");
  153. if(!$pos) {
  154. $pos = $max_line_length - 1;
  155. }
  156. $lines_out[] = substr($line,0,$pos);
  157. $line = substr($line,$pos + 1);
  158. if($in_headers) {
  159. $line = "\t" . $line;
  160. }
  161. }
  162. $lines_out[] = $line;
  163. while(list(,$line_out) = @each($lines_out)) {
  164. if(strlen($line_out) > 0)
  165. {
  166. if(substr($line_out, 0, 1) == ".") {
  167. $line_out = "." . $line_out;
  168. }
  169. }
  170. fputs($this->smtp_conn,$line_out . $this->CRLF);
  171. }
  172. }
  173. fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
  174. $rply = $this->get_lines();
  175. $code = substr($rply,0,3);
  176. if($this->do_debug >= 2) {
  177. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  178. }
  179. if($code != 250) {
  180. $this->error =
  181. array("error" => "DATA not accepted from server",
  182. "smtp_code" => $code,
  183. "smtp_msg" => substr($rply,4));
  184. if($this->do_debug >= 1) {
  185. echo "SMTP -> ERROR: " . $this->error["error"] .
  186. ": " . $rply . $this->CRLF;
  187. }
  188. return false;
  189. }
  190. return true;
  191. }
  192. public function Expand($name) {
  193. $this->error = null;
  194. if(!$this->connected()) {
  195. $this->error = array(
  196. "error" => "Called Expand() without being connected");
  197. return false;
  198. }
  199. fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
  200. $rply = $this->get_lines();
  201. $code = substr($rply,0,3);
  202. if($this->do_debug >= 2) {
  203. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  204. }
  205. if($code != 250) {
  206. $this->error =
  207. array("error" => "EXPN not accepted from server",
  208. "smtp_code" => $code,
  209. "smtp_msg" => substr($rply,4));
  210. if($this->do_debug >= 1) {
  211. echo "SMTP -> ERROR: " . $this->error["error"] .
  212. ": " . $rply . $this->CRLF;
  213. }
  214. return false;
  215. }
  216. $entries = explode($this->CRLF,$rply);
  217. while(list(,$l) = @each($entries)) {
  218. $list[] = substr($l,4);
  219. }
  220. return $list;
  221. }
  222. public function Hello($host="") {
  223. $this->error = null;
  224. if(!$this->connected()) {
  225. $this->error = array(
  226. "error" => "Called Hello() without being connected");
  227. return false;
  228. }
  229. if(empty($host)) {
  230. $host = "localhost";
  231. }
  232. if(!$this->SendHello("EHLO", $host))
  233. {
  234. if(!$this->SendHello("HELO", $host))
  235. return false;
  236. }
  237. return true;
  238. }
  239. public function SendHello($hello, $host) {
  240. fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
  241. $rply = $this->get_lines();
  242. $code = substr($rply,0,3);
  243. if($this->do_debug >= 2) {
  244. echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
  245. }
  246. if($code != 250) {
  247. $this->error =
  248. array("error" => $hello . " not accepted from server",
  249. "smtp_code" => $code,
  250. "smtp_msg" => substr($rply,4));
  251. if($this->do_debug >= 1) {
  252. echo "SMTP -> ERROR: " . $this->error["error"] .
  253. ": " . $rply . $this->CRLF;
  254. }
  255. return false;
  256. }
  257. $this->helo_rply = $rply;
  258. return true;
  259. }
  260. public function Help($keyword="") {
  261. $this->error = null;
  262. if(!$this->connected()) {
  263. $this->error = array(
  264. "error" => "Called Help() without being connected");
  265. return false;
  266. }
  267. $extra = "";
  268. if(!empty($keyword)) {
  269. $extra = " " . $keyword;
  270. }
  271. fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
  272. $rply = $this->get_lines();
  273. $code = substr($rply,0,3);
  274. if($this->do_debug >= 2) {
  275. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  276. }
  277. if($code != 211 && $code != 214) {
  278. $this->error =
  279. array("error" => "HELP not accepted from server",
  280. "smtp_code" => $code,
  281. "smtp_msg" => substr($rply,4));
  282. if($this->do_debug >= 1) {
  283. echo "SMTP -> ERROR: " . $this->error["error"] .
  284. ": " . $rply . $this->CRLF;
  285. }
  286. return false;
  287. }
  288. return $rply;
  289. }
  290. public function Mail($from) {
  291. $this->error = null;
  292. if(!$this->connected()) {
  293. $this->error = array(
  294. "error" => "Called Mail() without being connected");
  295. return false;
  296. }
  297. fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF);
  298. $rply = $this->get_lines();
  299. $code = substr($rply,0,3);
  300. if($this->do_debug >= 2) {
  301. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  302. }
  303. if($code != 250) {
  304. $this->error =
  305. array("error" => "MAIL not accepted from server",
  306. "smtp_code" => $code,
  307. "smtp_msg" => substr($rply,4));
  308. if($this->do_debug >= 1) {
  309. echo "SMTP -> ERROR: " . $this->error["error"] .
  310. ": " . $rply . $this->CRLF;
  311. }
  312. return false;
  313. }
  314. return true;
  315. }
  316. public function Noop() {
  317. $this->error = null;
  318. if(!$this->connected()) {
  319. $this->error = array(
  320. "error" => "Called Noop() without being connected");
  321. return false;
  322. }
  323. fputs($this->smtp_conn,"NOOP" . $this->CRLF);
  324. $rply = $this->get_lines();
  325. $code = substr($rply,0,3);
  326. if($this->do_debug >= 2) {
  327. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  328. }
  329. if($code != 250) {
  330. $this->error =
  331. array("error" => "NOOP not accepted from server",
  332. "smtp_code" => $code,
  333. "smtp_msg" => substr($rply,4));
  334. if($this->do_debug >= 1) {
  335. echo "SMTP -> ERROR: " . $this->error["error"] .
  336. ": " . $rply . $this->CRLF;
  337. }
  338. return false;
  339. }
  340. return true;
  341. }
  342. public function Quit($close_on_error=true) {
  343. $this->error = null;
  344. if(!$this->connected()) {
  345. $this->error = array(
  346. "error" => "Called Quit() without being connected");
  347. return false;
  348. }
  349. fputs($this->smtp_conn,"quit" . $this->CRLF);
  350. $byemsg = $this->get_lines();
  351. if($this->do_debug >= 2) {
  352. echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
  353. }
  354. $rval = true;
  355. $e = null;
  356. $code = substr($byemsg,0,3);
  357. if($code != 221) {
  358. $e = array("error" => "SMTP server rejected quit command",
  359. "smtp_code" => $code,
  360. "smtp_rply" => substr($byemsg,4));
  361. $rval = false;
  362. if($this->do_debug >= 1) {
  363. echo "SMTP -> ERROR: " . $e["error"] . ": " .
  364. $byemsg . $this->CRLF;
  365. }
  366. }
  367. if(empty($e) || $close_on_error) {
  368. $this->Close();
  369. }
  370. return $rval;
  371. }
  372. public function Recipient($to) {
  373. $this->error = null;
  374. if(!$this->connected()) {
  375. $this->error = array(
  376. "error" => "Called Recipient() without being connected");
  377. return false;
  378. }
  379. fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
  380. $rply = $this->get_lines();
  381. $code = substr($rply,0,3);
  382. if($this->do_debug >= 2) {
  383. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  384. }
  385. if($code != 250 && $code != 251) {
  386. $this->error =
  387. array("error" => "RCPT not accepted from server",
  388. "smtp_code" => $code,
  389. "smtp_msg" => substr($rply,4));
  390. if($this->do_debug >= 1) {
  391. echo "SMTP -> ERROR: " . $this->error["error"] .
  392. ": " . $rply . $this->CRLF;
  393. }
  394. return false;
  395. }
  396. return true;
  397. }
  398. public function Reset() {
  399. $this->error = null;
  400. if(!$this->connected()) {
  401. $this->error = array(
  402. "error" => "Called Reset() without being connected");
  403. return false;
  404. }
  405. fputs($this->smtp_conn,"RSET" . $this->CRLF);
  406. $rply = $this->get_lines();
  407. $code = substr($rply,0,3);
  408. if($this->do_debug >= 2) {
  409. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  410. }
  411. if($code != 250) {
  412. $this->error =
  413. array("error" => "RSET failed",
  414. "smtp_code" => $code,
  415. "smtp_msg" => substr($rply,4));
  416. if($this->do_debug >= 1) {
  417. echo "SMTP -> ERROR: " . $this->error["error"] .
  418. ": " . $rply . $this->CRLF;
  419. }
  420. return false;
  421. }
  422. return true;
  423. }
  424. public function Send($from) {
  425. $this->error = null;
  426. if(!$this->connected()) {
  427. $this->error = array(
  428. "error" => "Called Send() without being connected");
  429. return false;
  430. }
  431. fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
  432. $rply = $this->get_lines();
  433. $code = substr($rply,0,3);
  434. if($this->do_debug >= 2) {
  435. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  436. }
  437. if($code != 250) {
  438. $this->error =
  439. array("error" => "SEND not accepted from server",
  440. "smtp_code" => $code,
  441. "smtp_msg" => substr($rply,4));
  442. if($this->do_debug >= 1) {
  443. echo "SMTP -> ERROR: " . $this->error["error"] .
  444. ": " . $rply . $this->CRLF;
  445. }
  446. return false;
  447. }
  448. return true;
  449. }
  450. public function SendAndMail($from) {
  451. $this->error = null;
  452. if(!$this->connected()) {
  453. $this->error = array(
  454. "error" => "Called SendAndMail() without being connected");
  455. return false;
  456. }
  457. fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
  458. $rply = $this->get_lines();
  459. $code = substr($rply,0,3);
  460. if($this->do_debug >= 2) {
  461. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  462. }
  463. if($code != 250) {
  464. $this->error =
  465. array("error" => "SAML not accepted from server",
  466. "smtp_code" => $code,
  467. "smtp_msg" => substr($rply,4));
  468. if($this->do_debug >= 1) {
  469. echo "SMTP -> ERROR: " . $this->error["error"] .
  470. ": " . $rply . $this->CRLF;
  471. }
  472. return false;
  473. }
  474. return true;
  475. }
  476. public function SendOrMail($from) {
  477. $this->error = null;
  478. if(!$this->connected()) {
  479. $this->error = array(
  480. "error" => "Called SendOrMail() without being connected");
  481. return false;
  482. }
  483. fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
  484. $rply = $this->get_lines();
  485. $code = substr($rply,0,3);
  486. if($this->do_debug >= 2) {
  487. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  488. }
  489. if($code != 250) {
  490. $this->error =
  491. array("error" => "SOML not accepted from server",
  492. "smtp_code" => $code,
  493. "smtp_msg" => substr($rply,4));
  494. if($this->do_debug >= 1) {
  495. echo "SMTP -> ERROR: " . $this->error["error"] .
  496. ": " . $rply . $this->CRLF;
  497. }
  498. return false;
  499. }
  500. return true;
  501. }
  502. public function Turn() {
  503. $this->error = array("error" => "This method, TURN, of the SMTP ".
  504. "is not implemented");
  505. if($this->do_debug >= 1) {
  506. echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
  507. }
  508. return false;
  509. }
  510. public function Verify($name) {
  511. $this->error = null;
  512. if(!$this->connected()) {
  513. $this->error = array(
  514. "error" => "Called Verify() without being connected");
  515. return false;
  516. }
  517. fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
  518. $rply = $this->get_lines();
  519. $code = substr($rply,0,3);
  520. if($this->do_debug >= 2) {
  521. echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
  522. }
  523. if($code != 250 && $code != 251) {
  524. $this->error =
  525. array("error" => "VRFY failed on name '$name'",
  526. "smtp_code" => $code,
  527. "smtp_msg" => substr($rply,4));
  528. if($this->do_debug >= 1) {
  529. echo "SMTP -> ERROR: " . $this->error["error"] .
  530. ": " . $rply . $this->CRLF;
  531. }
  532. return false;
  533. }
  534. return $rply;
  535. }
  536. public function get_lines() {
  537. $data = "";
  538. while($str = fgets($this->smtp_conn,515)) {
  539. if($this->do_debug >= 4) {
  540. echo "SMTP -> get_lines(): \$data was \"$data\"" .
  541. $this->CRLF;
  542. echo "SMTP -> get_lines(): \$str is \"$str\"" .
  543. $this->CRLF;
  544. }
  545. $data .= $str;
  546. if($this->do_debug >= 4) {
  547. echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
  548. }
  549. if(substr($str,3,1) == " ") { break; }
  550. }
  551. return $data;
  552. }
  553. }