Advertisement
Guest User

Untitled

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