smtp.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. if(!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));
  4. class smtp
  5. {
  6. public $smtp_username;
  7. public $smtp_password;
  8. public $smtp_host;
  9. public $smtp_from;
  10. public $smtp_port;
  11. public $smtp_charset;
  12. public function __construct($smtp_username, $smtp_password, $smtp_host, $smtp_from, $smtp_port = 25, $smtp_charset = 'utf-8')
  13. {
  14. $this->smtp_username = $smtp_username;
  15. $this->smtp_password = $smtp_password;
  16. $this->smtp_host = $smtp_host;
  17. $this->smtp_from = $smtp_from;
  18. $this->smtp_port = $smtp_port;
  19. $this->smtp_charset = $smtp_charset;
  20. }
  21. function send($mailTo, $subject, $message, $headers)
  22. {
  23. global $cfg;
  24. $contentMail = 'Date: '.date('D, d M Y H:i:s')." UT\r\n";
  25. $contentMail .= 'Subject: =?'.$this->smtp_charset.'?B?'.base64_encode($subject)."=?=\r\n";
  26. $contentMail .= $headers."\r\n";
  27. $contentMail .= $message."\r\n";
  28. try
  29. {
  30. if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30))
  31. throw new Exception($errorNumber.'.'.$errorDescription);
  32. if(!$this->_parseServer($socket, '220'))
  33. throw new Exception('Connection error');
  34. $server_name = $cfg['url'];
  35. fputs($socket, 'HELO '.$server_name."\r\n");
  36. if(!$this->_parseServer($socket, '250'))
  37. {
  38. fclose($socket);
  39. throw new Exception('Error of command sending: HELO');
  40. }
  41. fputs($socket, 'AUTH LOGIN'."\r\n");
  42. if(!$this->_parseServer($socket, '334'))
  43. {
  44. fclose($socket);
  45. throw new Exception('Autorization error');
  46. }
  47. fputs($socket, base64_encode($this->smtp_username)."\r\n");
  48. if(!$this->_parseServer($socket, '334'))
  49. {
  50. fclose($socket);
  51. throw new Exception('Autorization error');
  52. }
  53. fputs($socket, base64_encode($this->smtp_password)."\r\n");
  54. if(!$this->_parseServer($socket, '235'))
  55. {
  56. fclose($socket);
  57. throw new Exception('Autorization error');
  58. }
  59. fputs($socket, 'MAIL FROM: <'.$this->smtp_username.">\r\n");
  60. if(!$this->_parseServer($socket, '250'))
  61. {
  62. fclose($socket);
  63. throw new Exception('Error of command sending: MAIL FROM');
  64. }
  65. $mailTo = ltrim($mailTo, '<');
  66. $mailTo = rtrim($mailTo, '>');
  67. fputs($socket, 'RCPT TO: <'.$mailTo.">\r\n");
  68. if(!$this->_parseServer($socket, '250'))
  69. {
  70. fclose($socket);
  71. throw new Exception('Error of command sending: RCPT TO');
  72. }
  73. fputs($socket, 'DATA'."\r\n");
  74. if(!$this->_parseServer($socket, "354"))
  75. {
  76. fclose($socket);
  77. throw new Exception('Error of command sending: DATA');
  78. }
  79. fputs($socket, $contentMail."\r\n.\r\n");
  80. if(!$this->_parseServer($socket, '250'))
  81. {
  82. fclose($socket);
  83. throw new Exception('E-mail didn\'t sent');
  84. }
  85. fputs($socket, 'QUIT'."\r\n");
  86. fclose($socket);
  87. }
  88. catch(Exception $e)
  89. {
  90. return $e->getMessage();
  91. }
  92. return true;
  93. }
  94. private function _parseServer($socket, $response)
  95. {
  96. while(@substr($responseServer, 3, 1) != ' ')
  97. if(!($responseServer = fgets($socket, 256)))
  98. return false;
  99. if(!(substr($responseServer, 0, 3) == $response))
  100. return false;
  101. return true;
  102. }
  103. }
  104. ?>