Advertisement
Guest User

Untitled

a guest
May 4th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. <?php
  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. $this->smtp_username = $smtp_username;
  22. $this->smtp_password = $smtp_password;
  23. $this->smtp_host = $smtp_host;
  24. $this->smtp_from = $smtp_from;
  25. $this->smtp_port = $smtp_port;
  26. $this->smtp_charset = $smtp_charset;
  27. }
  28.  
  29. /**
  30. * Отправка письма
  31. *
  32. * @param string $mailTo - получатель письма
  33. * @param string $subject - тема письма
  34. * @param string $message - тело письма
  35. * @param string $headers - заголовки письма
  36. *
  37. * @return bool|string В случаи отправки вернет true, иначе текст ошибки *
  38. */
  39. function send($mailTo, $subject, $message, $headers) {
  40. $contentMail = "Date: " . date("D, d M Y H:i:s") . " UTrn";
  41. $contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?' . base64_encode($subject) . "=?=rn";
  42. $contentMail .= $headers . "rn";
  43. $contentMail .= $message . "rn";
  44.  
  45. try {
  46. if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30)){
  47. throw new Exception($errorNumber.".".$errorDescription);
  48. }
  49. if (!$this->_parseServer($socket, "220")){
  50. throw new Exception('Connection error');
  51. }
  52.  
  53. $server_name = $_SERVER["SERVER_NAME"];
  54. fputs($socket, "HELO $server_namern");
  55. if (!$this->_parseServer($socket, "250")) {
  56. fclose($socket);
  57. throw new Exception('Error of command sending: HELO');
  58. }
  59.  
  60. fputs($socket, "AUTH LOGINrn");
  61. if (!$this->_parseServer($socket, "334")) {
  62. fclose($socket);
  63. throw new Exception('Autorization error');
  64. }
  65.  
  66.  
  67.  
  68. fputs($socket, base64_encode($this->smtp_username) . "rn");
  69. if (!$this->_parseServer($socket, "334")) {
  70. fclose($socket);
  71. throw new Exception('Autorization error');
  72. }
  73.  
  74. fputs($socket, base64_encode($this->smtp_password) . "rn");
  75. if (!$this->_parseServer($socket, "235")) {
  76. fclose($socket);
  77. throw new Exception('Autorization error');
  78. }
  79.  
  80. fputs($socket, "MAIL FROM: <".$this->smtp_username.">rn");
  81. if (!$this->_parseServer($socket, "250")) {
  82. fclose($socket);
  83. throw new Exception('Error of command sending: MAIL FROM');
  84. }
  85.  
  86. $mailTo = ltrim($mailTo, '<');
  87. $mailTo = rtrim($mailTo, '>');
  88. fputs($socket, "RCPT TO: <" . $mailTo . ">rn");
  89. if (!$this->_parseServer($socket, "250")) {
  90. fclose($socket);
  91. throw new Exception('Error of command sending: RCPT TO');
  92. }
  93.  
  94. fputs($socket, "DATArn");
  95. if (!$this->_parseServer($socket, "354")) {
  96. fclose($socket);
  97. throw new Exception('Error of command sending: DATA');
  98. }
  99.  
  100. fputs($socket, $contentMail."rn.rn");
  101. if (!$this->_parseServer($socket, "250")) {
  102. fclose($socket);
  103. throw new Exception("E-mail didn't sent");
  104. }
  105.  
  106. fputs($socket, "QUITrn");
  107. fclose($socket);
  108. } catch (Exception $e) {
  109. return $e->getMessage();
  110. }
  111. return true;
  112. }
  113.  
  114. private function _parseServer($socket, $response) {
  115. while (@substr($responseServer, 3, 1) != ' ') {
  116. if (!($responseServer = fgets($socket, 256))) {
  117. return false;
  118. }
  119. }
  120. if (!(substr($responseServer, 0, 3) == $response)) {
  121. return false;
  122. }
  123. return true;
  124.  
  125. }
  126.  
  127. require_once "SendMailSmtpClass.php"; // подключаем класс
  128. $mailSMTP = new SendMailSmtpClass('ksovreli2017@yandex.ru', 'nikusha0', 'ssl://smtp.yandex.ru', 'ksovrela', 465);
  129. // $mailSMTP = new SendMailSmtpClass('логин', 'пароль', 'хост', 'имя отправителя');
  130. $headers= "MIME-Version: 1.0rn";
  131. $headers .= "Content-type: text/html; charset=utf-8rn"; // кодировка письма
  132. $headers .= "From: $name <$email>rn"; // от кого письмо
  133. $result = $mailSMTP->send($to, $tema, $text, $headers);
  134. // $result = $mailSMTP->send('Кому письмо', 'Тема письма', 'Текст письма', 'Заголовки письма');
  135. if($result === true){
  136. echo "Письмо успешно отправлено!!!";
  137. $date = date('d.m.Y - H:i');
  138. $res = mysql_query("INSERT INTO message(`to`,`name`,`email`,`title`,`text`,`login`,`date`) VALUES('$to','$name','$email','$tema','$text','$login','$date')");
  139. echo mysql_error();
  140. }
  141. else{
  142. echo "проблема";
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement