class.smtp.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. <?php
  2. /*~ class.smtp.php
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 5.2.4 |
  6. | Site: https://code.google.com/a/apache-extras.org/p/phpmailer/ |
  7. | ------------------------------------------------------------------------- |
  8. | Admin: Jim Jagielski (project admininistrator) |
  9. | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  10. | : Marcus Bointon (coolbru) phpmailer@synchromedia.co.uk |
  11. | : Jim Jagielski (jimjag) jimjag@gmail.com |
  12. | Founder: Brent R. Matzelle (original founder) |
  13. | Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved. |
  14. | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
  15. | Copyright (c) 2001-2003, Brent R. Matzelle |
  16. | ------------------------------------------------------------------------- |
  17. | License: Distributed under the Lesser General Public License (LGPL) |
  18. | http://www.gnu.org/copyleft/lesser.html |
  19. | This program is distributed in the hope that it will be useful - WITHOUT |
  20. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  21. | FITNESS FOR A PARTICULAR PURPOSE. |
  22. '---------------------------------------------------------------------------'
  23. */
  24. /**
  25. * PHPMailer - PHP SMTP email transport class
  26. * NOTE: Designed for use with PHP version 5 and up
  27. * @package PHPMailer
  28. * @author Andy Prevost
  29. * @author Marcus Bointon
  30. * @copyright 2004 - 2008 Andy Prevost
  31. * @author Jim Jagielski
  32. * @copyright 2010 - 2012 Jim Jagielski
  33. * @license http://www.gnu.org/copyleft/lesser.html Distributed under the Lesser General Public License (LGPL)
  34. */
  35. /**
  36. * PHP RFC821 SMTP client
  37. *
  38. * Implements all the RFC 821 SMTP commands except TURN which will always return a not implemented error.
  39. * SMTP also provides some utility methods for sending mail to an SMTP server.
  40. * @author Chris Ryan
  41. * @package PHPMailer
  42. */
  43. class SMTP {
  44. /**
  45. * SMTP server port
  46. * @var int
  47. */
  48. public $SMTP_PORT = 25;
  49. /**
  50. * SMTP reply line ending (don't change)
  51. * @var string
  52. */
  53. public $CRLF = "\r\n";
  54. /**
  55. * Debug output level; 0 for no output
  56. * @var int
  57. */
  58. public $do_debug = 0;
  59. /**
  60. * Sets the function/method to use for debugging output.
  61. * Right now we only honor 'echo' or 'error_log'
  62. * @var string
  63. */
  64. public $Debugoutput = 'echo';
  65. /**
  66. * Sets VERP use on/off (default is off)
  67. * @var bool
  68. */
  69. public $do_verp = false;
  70. /**
  71. * Sets the SMTP timeout value for reads, in seconds
  72. * @var int
  73. */
  74. public $Timeout = 15;
  75. /**
  76. * Sets the SMTP timelimit value for reads, in seconds
  77. * @var int
  78. */
  79. public $Timelimit = 30;
  80. /**
  81. * Sets the SMTP PHPMailer Version number
  82. * @var string
  83. */
  84. public $Version = '5.2.4';
  85. /////////////////////////////////////////////////
  86. // PROPERTIES, PRIVATE AND PROTECTED
  87. /////////////////////////////////////////////////
  88. /**
  89. * @var resource The socket to the server
  90. */
  91. private $smtp_conn;
  92. /**
  93. * @var string Error message, if any, for the last call
  94. */
  95. private $error;
  96. /**
  97. * @var string The reply the server sent to us for HELO
  98. */
  99. private $helo_rply;
  100. /**
  101. * Outputs debugging info via user-defined method
  102. * @param string $str
  103. */
  104. private function edebug($str) {
  105. if ($this->Debugoutput == 'error_log') {
  106. error_log($str);
  107. } else {
  108. echo $str;
  109. }
  110. }
  111. /**
  112. * Initialize the class so that the data is in a known state.
  113. * @access public
  114. * @return SMTP
  115. */
  116. public function __construct() {
  117. $this->smtp_conn = 0;
  118. $this->error = null;
  119. $this->helo_rply = null;
  120. $this->do_debug = 0;
  121. }
  122. /////////////////////////////////////////////////
  123. // CONNECTION FUNCTIONS
  124. /////////////////////////////////////////////////
  125. /**
  126. * Connect to the server specified on the port specified.
  127. * If the port is not specified use the default SMTP_PORT.
  128. * If tval is specified then a connection will try and be
  129. * established with the server for that number of seconds.
  130. * If tval is not specified the default is 30 seconds to
  131. * try on the connection.
  132. *
  133. * SMTP CODE SUCCESS: 220
  134. * SMTP CODE FAILURE: 421
  135. * @access public
  136. * @param string $host
  137. * @param int $port
  138. * @param int $tval
  139. * @return bool
  140. */
  141. public function Connect($host, $port = 0, $tval = 30) {
  142. // set the error val to null so there is no confusion
  143. $this->error = null;
  144. // make sure we are __not__ connected
  145. if($this->connected()) {
  146. // already connected, generate error
  147. $this->error = array('error' => 'Already connected to a server');
  148. return false;
  149. }
  150. if(empty($port)) {
  151. $port = $this->SMTP_PORT;
  152. }
  153. // connect to the smtp server
  154. $this->smtp_conn = @fsockopen($host, // the host of the server
  155. $port, // the port to use
  156. $errno, // error number if any
  157. $errstr, // error message if any
  158. $tval); // give up after ? secs
  159. // verify we connected properly
  160. if(empty($this->smtp_conn)) {
  161. $this->error = array('error' => 'Failed to connect to server',
  162. 'errno' => $errno,
  163. 'errstr' => $errstr);
  164. if($this->do_debug >= 1) {
  165. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ": $errstr ($errno)" . $this->CRLF . '<br />');
  166. }
  167. return false;
  168. }
  169. // SMTP server can take longer to respond, give longer timeout for first read
  170. // Windows does not have support for this timeout function
  171. if(substr(PHP_OS, 0, 3) != 'WIN') {
  172. $max = ini_get('max_execution_time');
  173. if ($max != 0 && $tval > $max) { // don't bother if unlimited
  174. @set_time_limit($tval);
  175. }
  176. stream_set_timeout($this->smtp_conn, $tval, 0);
  177. }
  178. // get any announcement
  179. $announce = $this->get_lines();
  180. if($this->do_debug >= 2) {
  181. $this->edebug('SMTP -> FROM SERVER:' . $announce . $this->CRLF . '<br />');
  182. }
  183. return true;
  184. }
  185. /**
  186. * Initiate a TLS communication with the server.
  187. *
  188. * SMTP CODE 220 Ready to start TLS
  189. * SMTP CODE 501 Syntax error (no parameters allowed)
  190. * SMTP CODE 454 TLS not available due to temporary reason
  191. * @access public
  192. * @return bool success
  193. */
  194. public function StartTLS() {
  195. $this->error = null; # to avoid confusion
  196. if(!$this->connected()) {
  197. $this->error = array('error' => 'Called StartTLS() without being connected');
  198. return false;
  199. }
  200. $this->client_send('STARTTLS' . $this->CRLF);
  201. $rply = $this->get_lines();
  202. $code = substr($rply, 0, 3);
  203. if($this->do_debug >= 2) {
  204. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  205. }
  206. if($code != 220) {
  207. $this->error =
  208. array('error' => 'STARTTLS not accepted from server',
  209. 'smtp_code' => $code,
  210. 'smtp_msg' => substr($rply, 4));
  211. if($this->do_debug >= 1) {
  212. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  213. }
  214. return false;
  215. }
  216. // Begin encrypted connection
  217. if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
  218. return false;
  219. }
  220. return true;
  221. }
  222. /**
  223. * Performs SMTP authentication. Must be run after running the
  224. * Hello() method. Returns true if successfully authenticated.
  225. * @access public
  226. * @param string $username
  227. * @param string $password
  228. * @param string $authtype
  229. * @param string $realm
  230. * @param string $workstation
  231. * @return bool
  232. */
  233. public function Authenticate($username, $password, $authtype='LOGIN', $realm='', $workstation='') {
  234. if (empty($authtype)) {
  235. $authtype = 'LOGIN';
  236. }
  237. switch ($authtype) {
  238. case 'PLAIN':
  239. // Start authentication
  240. $this->client_send('AUTH PLAIN' . $this->CRLF);
  241. $rply = $this->get_lines();
  242. $code = substr($rply, 0, 3);
  243. if($code != 334) {
  244. $this->error =
  245. array('error' => 'AUTH not accepted from server',
  246. 'smtp_code' => $code,
  247. 'smtp_msg' => substr($rply, 4));
  248. if($this->do_debug >= 1) {
  249. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  250. }
  251. return false;
  252. }
  253. // Send encoded username and password
  254. $this->client_send(base64_encode("\0".$username."\0".$password) . $this->CRLF);
  255. $rply = $this->get_lines();
  256. $code = substr($rply, 0, 3);
  257. if($code != 235) {
  258. $this->error =
  259. array('error' => 'Authentication not accepted from server',
  260. 'smtp_code' => $code,
  261. 'smtp_msg' => substr($rply, 4));
  262. if($this->do_debug >= 1) {
  263. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  264. }
  265. return false;
  266. }
  267. break;
  268. case 'LOGIN':
  269. // Start authentication
  270. $this->client_send('AUTH LOGIN' . $this->CRLF);
  271. $rply = $this->get_lines();
  272. $code = substr($rply, 0, 3);
  273. if($code != 334) {
  274. $this->error =
  275. array('error' => 'AUTH not accepted from server',
  276. 'smtp_code' => $code,
  277. 'smtp_msg' => substr($rply, 4));
  278. if($this->do_debug >= 1) {
  279. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  280. }
  281. return false;
  282. }
  283. // Send encoded username
  284. $this->client_send(base64_encode($username) . $this->CRLF);
  285. $rply = $this->get_lines();
  286. $code = substr($rply, 0, 3);
  287. if($code != 334) {
  288. $this->error =
  289. array('error' => 'Username not accepted from server',
  290. 'smtp_code' => $code,
  291. 'smtp_msg' => substr($rply, 4));
  292. if($this->do_debug >= 1) {
  293. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  294. }
  295. return false;
  296. }
  297. // Send encoded password
  298. $this->client_send(base64_encode($password) . $this->CRLF);
  299. $rply = $this->get_lines();
  300. $code = substr($rply, 0, 3);
  301. if($code != 235) {
  302. $this->error =
  303. array('error' => 'Password not accepted from server',
  304. 'smtp_code' => $code,
  305. 'smtp_msg' => substr($rply, 4));
  306. if($this->do_debug >= 1) {
  307. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  308. }
  309. return false;
  310. }
  311. break;
  312. case 'NTLM':
  313. /*
  314. * ntlm_sasl_client.php
  315. ** Bundled with Permission
  316. **
  317. ** How to telnet in windows: http://technet.microsoft.com/en-us/library/aa995718%28EXCHG.65%29.aspx
  318. ** PROTOCOL Documentation http://curl.haxx.se/rfc/ntlm.html#ntlmSmtpAuthentication
  319. */
  320. require_once 'extras/ntlm_sasl_client.php';
  321. $temp = new stdClass();
  322. $ntlm_client = new ntlm_sasl_client_class;
  323. if(! $ntlm_client->Initialize($temp)){//let's test if every function its available
  324. $this->error = array('error' => $temp->error);
  325. if($this->do_debug >= 1) {
  326. $this->edebug('You need to enable some modules in your php.ini file: ' . $this->error['error'] . $this->CRLF);
  327. }
  328. return false;
  329. }
  330. $msg1 = $ntlm_client->TypeMsg1($realm, $workstation);//msg1
  331. $this->client_send('AUTH NTLM ' . base64_encode($msg1) . $this->CRLF);
  332. $rply = $this->get_lines();
  333. $code = substr($rply, 0, 3);
  334. if($code != 334) {
  335. $this->error =
  336. array('error' => 'AUTH not accepted from server',
  337. 'smtp_code' => $code,
  338. 'smtp_msg' => substr($rply, 4));
  339. if($this->do_debug >= 1) {
  340. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF);
  341. }
  342. return false;
  343. }
  344. $challenge = substr($rply, 3);//though 0 based, there is a white space after the 3 digit number....//msg2
  345. $challenge = base64_decode($challenge);
  346. $ntlm_res = $ntlm_client->NTLMResponse(substr($challenge, 24, 8), $password);
  347. $msg3 = $ntlm_client->TypeMsg3($ntlm_res, $username, $realm, $workstation);//msg3
  348. // Send encoded username
  349. $this->client_send(base64_encode($msg3) . $this->CRLF);
  350. $rply = $this->get_lines();
  351. $code = substr($rply, 0, 3);
  352. if($code != 235) {
  353. $this->error =
  354. array('error' => 'Could not authenticate',
  355. 'smtp_code' => $code,
  356. 'smtp_msg' => substr($rply, 4));
  357. if($this->do_debug >= 1) {
  358. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF);
  359. }
  360. return false;
  361. }
  362. break;
  363. case 'CRAM-MD5':
  364. // Start authentication
  365. $this->client_send('AUTH CRAM-MD5' . $this->CRLF);
  366. $rply = $this->get_lines();
  367. $code = substr($rply, 0, 3);
  368. if($code != 334) {
  369. $this->error =
  370. array('error' => 'AUTH not accepted from server',
  371. 'smtp_code' => $code,
  372. 'smtp_msg' => substr($rply, 4));
  373. if($this->do_debug >= 1) {
  374. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  375. }
  376. return false;
  377. }
  378. // Get the challenge
  379. $challenge = base64_decode(substr($rply, 4));
  380. // Build the response
  381. $response = $username . ' ' . $this->hmac($challenge, $password);
  382. // Send encoded credentials
  383. $this->client_send(base64_encode($response) . $this->CRLF);
  384. $rply = $this->get_lines();
  385. $code = substr($rply, 0, 3);
  386. if($code != 334) {
  387. $this->error =
  388. array('error' => 'Credentials not accepted from server',
  389. 'smtp_code' => $code,
  390. 'smtp_msg' => substr($rply, 4));
  391. if($this->do_debug >= 1) {
  392. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  393. }
  394. return false;
  395. }
  396. break;
  397. }
  398. return true;
  399. }
  400. /**
  401. * Works like hash_hmac('md5', $data, $key) in case that function is not available
  402. * @access private
  403. * @param string $data
  404. * @param string $key
  405. * @return string
  406. */
  407. private function hmac($data, $key) {
  408. if (function_exists('hash_hmac')) {
  409. return hash_hmac('md5', $data, $key);
  410. }
  411. // The following borrowed from http://php.net/manual/en/function.mhash.php#27225
  412. // RFC 2104 HMAC implementation for php.
  413. // Creates an md5 HMAC.
  414. // Eliminates the need to install mhash to compute a HMAC
  415. // Hacked by Lance Rushing
  416. $b = 64; // byte length for md5
  417. if (strlen($key) > $b) {
  418. $key = pack('H*', md5($key));
  419. }
  420. $key = str_pad($key, $b, chr(0x00));
  421. $ipad = str_pad('', $b, chr(0x36));
  422. $opad = str_pad('', $b, chr(0x5c));
  423. $k_ipad = $key ^ $ipad ;
  424. $k_opad = $key ^ $opad;
  425. return md5($k_opad . pack('H*', md5($k_ipad . $data)));
  426. }
  427. /**
  428. * Returns true if connected to a server otherwise false
  429. * @access public
  430. * @return bool
  431. */
  432. public function Connected() {
  433. if(!empty($this->smtp_conn)) {
  434. $sock_status = stream_get_meta_data($this->smtp_conn);
  435. if($sock_status['eof']) {
  436. // the socket is valid but we are not connected
  437. if($this->do_debug >= 1) {
  438. $this->edebug('SMTP -> NOTICE:' . $this->CRLF . 'EOF caught while checking if connected');
  439. }
  440. $this->Close();
  441. return false;
  442. }
  443. return true; // everything looks good
  444. }
  445. return false;
  446. }
  447. /**
  448. * Closes the socket and cleans up the state of the class.
  449. * It is not considered good to use this function without
  450. * first trying to use QUIT.
  451. * @access public
  452. * @return void
  453. */
  454. public function Close() {
  455. $this->error = null; // so there is no confusion
  456. $this->helo_rply = null;
  457. if(!empty($this->smtp_conn)) {
  458. // close the connection and cleanup
  459. fclose($this->smtp_conn);
  460. $this->smtp_conn = 0;
  461. }
  462. }
  463. /////////////////////////////////////////////////
  464. // SMTP COMMANDS
  465. /////////////////////////////////////////////////
  466. /**
  467. * Issues a data command and sends the msg_data to the server
  468. * finializing the mail transaction. $msg_data is the message
  469. * that is to be send with the headers. Each header needs to be
  470. * on a single line followed by a <CRLF> with the message headers
  471. * and the message body being seperated by and additional <CRLF>.
  472. *
  473. * Implements rfc 821: DATA <CRLF>
  474. *
  475. * SMTP CODE INTERMEDIATE: 354
  476. * [data]
  477. * <CRLF>.<CRLF>
  478. * SMTP CODE SUCCESS: 250
  479. * SMTP CODE FAILURE: 552, 554, 451, 452
  480. * SMTP CODE FAILURE: 451, 554
  481. * SMTP CODE ERROR : 500, 501, 503, 421
  482. * @access public
  483. * @param string $msg_data
  484. * @return bool
  485. */
  486. public function Data($msg_data) {
  487. $this->error = null; // so no confusion is caused
  488. if(!$this->connected()) {
  489. $this->error = array(
  490. 'error' => 'Called Data() without being connected');
  491. return false;
  492. }
  493. $this->client_send('DATA' . $this->CRLF);
  494. $rply = $this->get_lines();
  495. $code = substr($rply, 0, 3);
  496. if($this->do_debug >= 2) {
  497. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  498. }
  499. if($code != 354) {
  500. $this->error =
  501. array('error' => 'DATA command not accepted from server',
  502. 'smtp_code' => $code,
  503. 'smtp_msg' => substr($rply, 4));
  504. if($this->do_debug >= 1) {
  505. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  506. }
  507. return false;
  508. }
  509. /* the server is ready to accept data!
  510. * according to rfc 821 we should not send more than 1000
  511. * including the CRLF
  512. * characters on a single line so we will break the data up
  513. * into lines by \r and/or \n then if needed we will break
  514. * each of those into smaller lines to fit within the limit.
  515. * in addition we will be looking for lines that start with
  516. * a period '.' and append and additional period '.' to that
  517. * line. NOTE: this does not count towards limit.
  518. */
  519. // normalize the line breaks so we know the explode works
  520. $msg_data = str_replace("\r\n", "\n", $msg_data);
  521. $msg_data = str_replace("\r", "\n", $msg_data);
  522. $lines = explode("\n", $msg_data);
  523. /* we need to find a good way to determine is headers are
  524. * in the msg_data or if it is a straight msg body
  525. * currently I am assuming rfc 822 definitions of msg headers
  526. * and if the first field of the first line (':' sperated)
  527. * does not contain a space then it _should_ be a header
  528. * and we can process all lines before a blank "" line as
  529. * headers.
  530. */
  531. $field = substr($lines[0], 0, strpos($lines[0], ':'));
  532. $in_headers = false;
  533. if(!empty($field) && !strstr($field, ' ')) {
  534. $in_headers = true;
  535. }
  536. $max_line_length = 998; // used below; set here for ease in change
  537. while(list(, $line) = @each($lines)) {
  538. $lines_out = null;
  539. if($line == '' && $in_headers) {
  540. $in_headers = false;
  541. }
  542. // ok we need to break this line up into several smaller lines
  543. while(strlen($line) > $max_line_length) {
  544. $pos = strrpos(substr($line, 0, $max_line_length), ' ');
  545. // Patch to fix DOS attack
  546. if(!$pos) {
  547. $pos = $max_line_length - 1;
  548. $lines_out[] = substr($line, 0, $pos);
  549. $line = substr($line, $pos);
  550. } else {
  551. $lines_out[] = substr($line, 0, $pos);
  552. $line = substr($line, $pos + 1);
  553. }
  554. /* if processing headers add a LWSP-char to the front of new line
  555. * rfc 822 on long msg headers
  556. */
  557. if($in_headers) {
  558. $line = "\t" . $line;
  559. }
  560. }
  561. $lines_out[] = $line;
  562. // send the lines to the server
  563. while(list(, $line_out) = @each($lines_out)) {
  564. if(strlen($line_out) > 0)
  565. {
  566. if(substr($line_out, 0, 1) == '.') {
  567. $line_out = '.' . $line_out;
  568. }
  569. }
  570. $this->client_send($line_out . $this->CRLF);
  571. }
  572. }
  573. // message data has been sent
  574. $this->client_send($this->CRLF . '.' . $this->CRLF);
  575. $rply = $this->get_lines();
  576. $code = substr($rply, 0, 3);
  577. if($this->do_debug >= 2) {
  578. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  579. }
  580. if($code != 250) {
  581. $this->error =
  582. array('error' => 'DATA not accepted from server',
  583. 'smtp_code' => $code,
  584. 'smtp_msg' => substr($rply, 4));
  585. if($this->do_debug >= 1) {
  586. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  587. }
  588. return false;
  589. }
  590. return true;
  591. }
  592. /**
  593. * Sends the HELO command to the smtp server.
  594. * This makes sure that we and the server are in
  595. * the same known state.
  596. *
  597. * Implements from rfc 821: HELO <SP> <domain> <CRLF>
  598. *
  599. * SMTP CODE SUCCESS: 250
  600. * SMTP CODE ERROR : 500, 501, 504, 421
  601. * @access public
  602. * @param string $host
  603. * @return bool
  604. */
  605. public function Hello($host = '') {
  606. $this->error = null; // so no confusion is caused
  607. if(!$this->connected()) {
  608. $this->error = array(
  609. 'error' => 'Called Hello() without being connected');
  610. return false;
  611. }
  612. // if hostname for HELO was not specified send default
  613. if(empty($host)) {
  614. // determine appropriate default to send to server
  615. $host = 'localhost';
  616. }
  617. // Send extended hello first (RFC 2821)
  618. if(!$this->SendHello('EHLO', $host)) {
  619. if(!$this->SendHello('HELO', $host)) {
  620. return false;
  621. }
  622. }
  623. return true;
  624. }
  625. /**
  626. * Sends a HELO/EHLO command.
  627. * @access private
  628. * @param string $hello
  629. * @param string $host
  630. * @return bool
  631. */
  632. private function SendHello($hello, $host) {
  633. $this->client_send($hello . ' ' . $host . $this->CRLF);
  634. $rply = $this->get_lines();
  635. $code = substr($rply, 0, 3);
  636. if($this->do_debug >= 2) {
  637. $this->edebug('SMTP -> FROM SERVER: ' . $rply . $this->CRLF . '<br />');
  638. }
  639. if($code != 250) {
  640. $this->error =
  641. array('error' => $hello . ' not accepted from server',
  642. 'smtp_code' => $code,
  643. 'smtp_msg' => substr($rply, 4));
  644. if($this->do_debug >= 1) {
  645. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  646. }
  647. return false;
  648. }
  649. $this->helo_rply = $rply;
  650. return true;
  651. }
  652. /**
  653. * Starts a mail transaction from the email address specified in
  654. * $from. Returns true if successful or false otherwise. If True
  655. * the mail transaction is started and then one or more Recipient
  656. * commands may be called followed by a Data command.
  657. *
  658. * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
  659. *
  660. * SMTP CODE SUCCESS: 250
  661. * SMTP CODE SUCCESS: 552, 451, 452
  662. * SMTP CODE SUCCESS: 500, 501, 421
  663. * @access public
  664. * @param string $from
  665. * @return bool
  666. */
  667. public function Mail($from) {
  668. $this->error = null; // so no confusion is caused
  669. if(!$this->connected()) {
  670. $this->error = array(
  671. 'error' => 'Called Mail() without being connected');
  672. return false;
  673. }
  674. $useVerp = ($this->do_verp ? ' XVERP' : '');
  675. $this->client_send('MAIL FROM:<' . $from . '>' . $useVerp . $this->CRLF);
  676. $rply = $this->get_lines();
  677. $code = substr($rply, 0, 3);
  678. if($this->do_debug >= 2) {
  679. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  680. }
  681. if($code != 250) {
  682. $this->error =
  683. array('error' => 'MAIL not accepted from server',
  684. 'smtp_code' => $code,
  685. 'smtp_msg' => substr($rply, 4));
  686. if($this->do_debug >= 1) {
  687. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  688. }
  689. return false;
  690. }
  691. return true;
  692. }
  693. /**
  694. * Sends the quit command to the server and then closes the socket
  695. * if there is no error or the $close_on_error argument is true.
  696. *
  697. * Implements from rfc 821: QUIT <CRLF>
  698. *
  699. * SMTP CODE SUCCESS: 221
  700. * SMTP CODE ERROR : 500
  701. * @access public
  702. * @param bool $close_on_error
  703. * @return bool
  704. */
  705. public function Quit($close_on_error = true) {
  706. $this->error = null; // so there is no confusion
  707. if(!$this->connected()) {
  708. $this->error = array(
  709. 'error' => 'Called Quit() without being connected');
  710. return false;
  711. }
  712. // send the quit command to the server
  713. $this->client_send('quit' . $this->CRLF);
  714. // get any good-bye messages
  715. $byemsg = $this->get_lines();
  716. if($this->do_debug >= 2) {
  717. $this->edebug('SMTP -> FROM SERVER:' . $byemsg . $this->CRLF . '<br />');
  718. }
  719. $rval = true;
  720. $e = null;
  721. $code = substr($byemsg, 0, 3);
  722. if($code != 221) {
  723. // use e as a tmp var cause Close will overwrite $this->error
  724. $e = array('error' => 'SMTP server rejected quit command',
  725. 'smtp_code' => $code,
  726. 'smtp_rply' => substr($byemsg, 4));
  727. $rval = false;
  728. if($this->do_debug >= 1) {
  729. $this->edebug('SMTP -> ERROR: ' . $e['error'] . ': ' . $byemsg . $this->CRLF . '<br />');
  730. }
  731. }
  732. if(empty($e) || $close_on_error) {
  733. $this->Close();
  734. }
  735. return $rval;
  736. }
  737. /**
  738. * Sends the command RCPT to the SMTP server with the TO: argument of $to.
  739. * Returns true if the recipient was accepted false if it was rejected.
  740. *
  741. * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
  742. *
  743. * SMTP CODE SUCCESS: 250, 251
  744. * SMTP CODE FAILURE: 550, 551, 552, 553, 450, 451, 452
  745. * SMTP CODE ERROR : 500, 501, 503, 421
  746. * @access public
  747. * @param string $to
  748. * @return bool
  749. */
  750. public function Recipient($to) {
  751. $this->error = null; // so no confusion is caused
  752. if(!$this->connected()) {
  753. $this->error = array(
  754. 'error' => 'Called Recipient() without being connected');
  755. return false;
  756. }
  757. $this->client_send('RCPT TO:<' . $to . '>' . $this->CRLF);
  758. $rply = $this->get_lines();
  759. $code = substr($rply, 0, 3);
  760. if($this->do_debug >= 2) {
  761. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  762. }
  763. if($code != 250 && $code != 251) {
  764. $this->error =
  765. array('error' => 'RCPT not accepted from server',
  766. 'smtp_code' => $code,
  767. 'smtp_msg' => substr($rply, 4));
  768. if($this->do_debug >= 1) {
  769. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  770. }
  771. return false;
  772. }
  773. return true;
  774. }
  775. /**
  776. * Sends the RSET command to abort and transaction that is
  777. * currently in progress. Returns true if successful false
  778. * otherwise.
  779. *
  780. * Implements rfc 821: RSET <CRLF>
  781. *
  782. * SMTP CODE SUCCESS: 250
  783. * SMTP CODE ERROR : 500, 501, 504, 421
  784. * @access public
  785. * @return bool
  786. */
  787. public function Reset() {
  788. $this->error = null; // so no confusion is caused
  789. if(!$this->connected()) {
  790. $this->error = array('error' => 'Called Reset() without being connected');
  791. return false;
  792. }
  793. $this->client_send('RSET' . $this->CRLF);
  794. $rply = $this->get_lines();
  795. $code = substr($rply, 0, 3);
  796. if($this->do_debug >= 2) {
  797. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  798. }
  799. if($code != 250) {
  800. $this->error =
  801. array('error' => 'RSET failed',
  802. 'smtp_code' => $code,
  803. 'smtp_msg' => substr($rply, 4));
  804. if($this->do_debug >= 1) {
  805. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  806. }
  807. return false;
  808. }
  809. return true;
  810. }
  811. /**
  812. * Starts a mail transaction from the email address specified in
  813. * $from. Returns true if successful or false otherwise. If True
  814. * the mail transaction is started and then one or more Recipient
  815. * commands may be called followed by a Data command. This command
  816. * will send the message to the users terminal if they are logged
  817. * in and send them an email.
  818. *
  819. * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
  820. *
  821. * SMTP CODE SUCCESS: 250
  822. * SMTP CODE SUCCESS: 552, 451, 452
  823. * SMTP CODE SUCCESS: 500, 501, 502, 421
  824. * @access public
  825. * @param string $from
  826. * @return bool
  827. */
  828. public function SendAndMail($from) {
  829. $this->error = null; // so no confusion is caused
  830. if(!$this->connected()) {
  831. $this->error = array(
  832. 'error' => 'Called SendAndMail() without being connected');
  833. return false;
  834. }
  835. $this->client_send('SAML FROM:' . $from . $this->CRLF);
  836. $rply = $this->get_lines();
  837. $code = substr($rply, 0, 3);
  838. if($this->do_debug >= 2) {
  839. $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
  840. }
  841. if($code != 250) {
  842. $this->error =
  843. array('error' => 'SAML not accepted from server',
  844. 'smtp_code' => $code,
  845. 'smtp_msg' => substr($rply, 4));
  846. if($this->do_debug >= 1) {
  847. $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
  848. }
  849. return false;
  850. }
  851. return true;
  852. }
  853. /**
  854. * This is an optional command for SMTP that this class does not
  855. * support. This method is here to make the RFC821 Definition
  856. * complete for this class and __may__ be implimented in the future
  857. *
  858. * Implements from rfc 821: TURN <CRLF>
  859. *
  860. * SMTP CODE SUCCESS: 250
  861. * SMTP CODE FAILURE: 502
  862. * SMTP CODE ERROR : 500, 503
  863. * @access public
  864. * @return bool
  865. */
  866. public function Turn() {
  867. $this->error = array('error' => 'This method, TURN, of the SMTP '.
  868. 'is not implemented');
  869. if($this->do_debug >= 1) {
  870. $this->edebug('SMTP -> NOTICE: ' . $this->error['error'] . $this->CRLF . '<br />');
  871. }
  872. return false;
  873. }
  874. /**
  875. * Sends data to the server
  876. * @param string $data
  877. * @access public
  878. * @return Integer number of bytes sent to the server or FALSE on error
  879. */
  880. public function client_send($data) {
  881. if ($this->do_debug >= 1) {
  882. $this->edebug("CLIENT -> SMTP: $data" . $this->CRLF . '<br />');
  883. }
  884. return fwrite($this->smtp_conn, $data);
  885. }
  886. /**
  887. * Get the current error
  888. * @access public
  889. * @return array
  890. */
  891. public function getError() {
  892. return $this->error;
  893. }
  894. /////////////////////////////////////////////////
  895. // INTERNAL FUNCTIONS
  896. /////////////////////////////////////////////////
  897. /**
  898. * Read in as many lines as possible
  899. * either before eof or socket timeout occurs on the operation.
  900. * With SMTP we can tell if we have more lines to read if the
  901. * 4th character is '-' symbol. If it is a space then we don't
  902. * need to read anything else.
  903. * @access private
  904. * @return string
  905. */
  906. private function get_lines() {
  907. $data = '';
  908. $endtime = 0;
  909. /* If for some reason the fp is bad, don't inf loop */
  910. if (!is_resource($this->smtp_conn)) {
  911. return $data;
  912. }
  913. stream_set_timeout($this->smtp_conn, $this->Timeout);
  914. if ($this->Timelimit > 0) {
  915. $endtime = time() + $this->Timelimit;
  916. }
  917. while(is_resource($this->smtp_conn) && !feof($this->smtp_conn)) {
  918. $str = @fgets($this->smtp_conn, 515);
  919. if($this->do_debug >= 4) {
  920. $this->edebug("SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />');
  921. $this->edebug("SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />');
  922. }
  923. $data .= $str;
  924. if($this->do_debug >= 4) {
  925. $this->edebug("SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />');
  926. }
  927. // if 4th character is a space, we are done reading, break the loop
  928. if(substr($str, 3, 1) == ' ') { break; }
  929. // Timed-out? Log and break
  930. $info = stream_get_meta_data($this->smtp_conn);
  931. if ($info['timed_out']) {
  932. if($this->do_debug >= 4) {
  933. $this->edebug('SMTP -> get_lines(): timed-out (' . $this->Timeout . ' seconds) <br />');
  934. }
  935. break;
  936. }
  937. // Now check if reads took too long
  938. if ($endtime) {
  939. if (time() > $endtime) {
  940. if($this->do_debug >= 4) {
  941. $this->edebug('SMTP -> get_lines(): timelimit reached (' . $this->Timelimit . ' seconds) <br />');
  942. }
  943. break;
  944. }
  945. }
  946. }
  947. return $data;
  948. }
  949. }