Advertisement
Guest User

Untitled

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