Smtp.class.php 32 KB

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