Guest User

Untitled

a guest
Jan 22nd, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. <?php
  2. class SMTP {
  3.  
  4. /**
  5. *
  6. * @var string $smtp_username - логин
  7. * @var string $smtp_password - пароль
  8. * @var string $smtp_host - хост
  9. * @var string $smtp_from - от кого
  10. * @var integer $smtp_port - порт
  11. * @var string $smtp_charset - кодировка
  12. *
  13. */
  14. public $smtp_username;
  15. public $smtp_password;
  16. public $smtp_host;
  17. public $smtp_from;
  18. public $smtp_port;
  19. public $smtp_charset;
  20.  
  21. public function __construct($smtp_username, $smtp_password, $smtp_host, $smtp_from, $smtp_port = 25, $smtp_charset = "utf-8") {
  22. $this->smtp_username = $smtp_username;
  23. $this->smtp_password = $smtp_password;
  24. $this->smtp_host = $smtp_host;
  25. $this->smtp_from = $smtp_from;
  26. $this->smtp_port = $smtp_port;
  27. $this->smtp_charset = $smtp_charset;
  28. }
  29.  
  30. /**
  31. * Отправка письма
  32. *
  33. * @param string $mailTo - получатель письма
  34. * @param string $subject - тема письма
  35. * @param string $message - тело письма
  36. * @param string $headers - заголовки письма
  37. *
  38. * @return bool|string В случаи отправки вернет true, иначе текст ошибки *
  39. */
  40. function send($mailTo, $subject, $message, $headers) {
  41. $contentMail = "Date: " . date("D, d M Y H:i:s") . " UT\r\n";
  42. $contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?' . base64_encode($subject) . "=?=\r\n";
  43. $contentMail .= $headers . "\r\n";
  44. $contentMail .= $message . "\r\n";
  45.  
  46. try {
  47. if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30)){
  48. throw new Exception($errorNumber.".".$errorDescription);
  49. }
  50. if (!$this->_parseServer($socket, "220")){
  51. throw new Exception('Connection error');
  52. }
  53.  
  54. $server_name = $_SERVER["SERVER_NAME"];
  55. fputs($socket, "HELO $server_name\r\n");
  56. if (!$this->_parseServer($socket, "250")) {
  57. fclose($socket);
  58. throw new Exception('Error of command sending: HELO');
  59. }
  60.  
  61. fputs($socket, "AUTH LOGIN\r\n");
  62. if (!$this->_parseServer($socket, "334")) {
  63. fclose($socket);
  64. throw new Exception('Autorization error');
  65. }
  66.  
  67.  
  68.  
  69. fputs($socket, base64_encode($this->smtp_username) . "\r\n");
  70. if (!$this->_parseServer($socket, "334")) {
  71. fclose($socket);
  72. throw new Exception('Autorization error');
  73. }
  74.  
  75. fputs($socket, base64_encode($this->smtp_password) . "\r\n");
  76. if (!$this->_parseServer($socket, "235")) {
  77. fclose($socket);
  78. throw new Exception('Autorization error');
  79. }
  80.  
  81. fputs($socket, "MAIL FROM: <".$this->smtp_username.">\r\n");
  82. if (!$this->_parseServer($socket, "250")) {
  83. fclose($socket);
  84. throw new Exception('Error of command sending: MAIL FROM');
  85. }
  86.  
  87. $mailTo = ltrim($mailTo, '<');
  88. $mailTo = rtrim($mailTo, '>');
  89. fputs($socket, "RCPT TO: <" . $mailTo . ">\r\n");
  90. if (!$this->_parseServer($socket, "250")) {
  91. fclose($socket);
  92. throw new Exception('Error of command sending: RCPT TO');
  93. }
  94.  
  95. fputs($socket, "DATA\r\n");
  96. if (!$this->_parseServer($socket, "354")) {
  97. fclose($socket);
  98. throw new Exception('Error of command sending: DATA');
  99. }
  100.  
  101. fputs($socket, $contentMail."\r\n.\r\n");
  102. if (!$this->_parseServer($socket, "250")) {
  103. fclose($socket);
  104. throw new Exception("E-mail didn't sent");
  105. }
  106.  
  107. fputs($socket, "QUIT\r\n");
  108. fclose($socket);
  109. } catch (Exception $e) {
  110. return $e->getMessage();
  111. }
  112. return true;
  113. }
  114.  
  115. private function _parseServer($socket, $response) {
  116. while (@substr($responseServer, 3, 1) != ' ') {
  117. if (!($responseServer = fgets($socket, 256))) {
  118. return false;
  119. }
  120. }
  121. if (!(substr($responseServer, 0, 3) == $response)) {
  122. return false;
  123. }
  124. return true;
  125.  
  126. }
  127. }
Add Comment
Please, Sign In to add comment