Advertisement
Guest User

mail_class

a guest
Sep 19th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. <?php
  2. class Mail {
  3. private $smtpServer = 'smtp.google.it';
  4. private $port = '25';
  5. private $timeout = '45';
  6. private $username = 'email@gmail.com';
  7. private $password = 'password';
  8. private $newline = "\n";
  9. ////////////////////////////////////////////// NU MODIFICATI ///////////////////
  10. private $charset = 'windows-1251';
  11. private $contentTransferEncoding = true;
  12.  
  13. // Do not change anything below
  14. private $smtpConnect = false;
  15. private $to = false;
  16. private $subject = false;
  17. private $message = false;
  18. private $headers = false;
  19. private $logArray = array(); // Array response message for debug
  20. private $Error = '';
  21.  
  22. public function __construct($to, $subject, $message) {
  23. $this->to = &$to;
  24. $this->subject = &$subject;
  25. $this->message = &$message;
  26. // Connect to server
  27. if(!$this->Connect2Server()) {
  28. // Display error message
  29. echo $this->Error.$this->newline.'<!-- '.$this->newline;
  30. print_r($this->logArray);
  31. echo $this->newline.'-->'.$this->newline;
  32. return false;
  33. }
  34. return false;
  35. }
  36.  
  37. private function Connect2Server() {
  38. // Connect to server
  39. $this->smtpConnect = fsockopen($this->smtpServer,$this->port,$errno,$error,$this->timeout);
  40. $this->logArray['CONNECT_RESPONSE'] = $this->readResponse();
  41.  
  42. if (!is_resource($this->smtpConnect)) {
  43. return false;
  44. }
  45. $this->logArray['connection'] = "Connection accepted: $smtpResponse";
  46. // Hi, server!
  47. $this->sendCommand("EHLO $this->localdomain");
  48. $this->logArray['EHLO'] = $this->readResponse();
  49. // Let's know each other
  50. $this->sendCommand('AUTH LOGIN');
  51. $this->logArray['AUTH_REQUEST'] = $this->readResponse();
  52. // My name...
  53. $this->sendCommand(base64_encode($this->username));
  54. $this->logArray['REQUEST_USER'] = $this->readResponse();
  55. // My password..
  56. $this->sendCommand(base64_encode($this->password));
  57. $this->logArray['REQUEST_PASSWD'] = $this->readResponse();
  58. // If error in response auth...
  59. if (substr($this->logArray['REQUEST_PASSWD'],0,3)!='235') {
  60. $this->Error .= 'Authorization error! '.$this->logArray['REQUEST_PASSWD'].$this->newline;
  61. return false;
  62. }
  63. // "From" mail...
  64. $this->sendCommand("MAIL FROM: $this->username");
  65. $this->logArray['MAIL_FROM_RESPONSE'] = $this->readResponse();
  66. if (substr($this->logArray['MAIL_FROM_RESPONSE'],0,3)!='250') {
  67. $this->Error .= 'Mistake in sender\'s address! '.$this->logArray['MAIL_FROM_RESPONSE'].$this->newline;
  68. return false;
  69. }
  70. // "To" address
  71. $this->sendCommand("RCPT TO: $this->to");
  72. $this->logArray['RCPT_TO_RESPONCE'] = $this->readResponse();
  73. if (substr($this->logArray['RCPT_TO_RESPONCE'],0,3)!='250') {
  74. $this->Error .= 'Mistake in reciepent address! '.$this->logArray['RCPT_TO_RESPONCE'].$this->newline;
  75. }
  76. // Send data to server
  77. $this->sendCommand('DATA');
  78. $this->logArray['DATA_RESPONSE'] = $this->readResponse();
  79. // Send mail message
  80. if (!$this->sendMail()) return false;
  81. // Good bye server! =)
  82. $this->sendCommand('QUIT');
  83. $this->logArray['QUIT_RESPONSE'] = $this->readResponse();
  84. // Close smtp connect
  85. fclose($this->smtpConnect);
  86. return true;
  87. }
  88. // Function send mail
  89. private function sendMail() {
  90. $this->sendHeaders();
  91. $this->sendCommand($this->message);
  92. $this->sendCommand('.');
  93. $this->logArray['SEND_DATA_RESPONSE'] = $this->readResponse();
  94. if(substr($this->logArray['SEND_DATA_RESPONSE'],0,3)!='250') {
  95. $this->Error .= 'Mistake in sending data! '.$this->logArray['SEND_DATA_RESPONSE'].$this->newline;
  96. return false;
  97. }
  98. return true;
  99. }
  100. // Function read response
  101. private function readResponse() {
  102. $data="";
  103. while($str = fgets($this->smtpConnect,4096))
  104. {
  105. $data .= $str;
  106. if(substr($str,3,1) == " ") { break; }
  107. }
  108. return $data;
  109. }
  110. // function send command to server
  111. private function sendCommand($string) {
  112. fputs($this->smtpConnect,$string.$this->newline);
  113. return ;
  114. }
  115. // function send headers
  116. private function sendHeaders() {
  117. $this->sendCommand("Date: ".date("D, j M Y G:i:s")." +0700");
  118. $this->sendCommand("From: <$this->username>");
  119. $this->sendCommand("Reply-To: <$this->username>");
  120. $this->sendCommand("To: <$this->to>");
  121. $this->sendCommand("Subject: $this->subject");
  122. $this->sendCommand("MIME-Version: 1.0");
  123. $this->sendCommand("Content-Type: text/plain; charset=$this->charset");
  124. if ($this->contentTransferEncoding) $this->sendCommand("Content-Transfer-Encoding: $this->contentTransferEncoding");
  125. $this->sendCommand($this->newline);
  126. return ;
  127. }
  128.  
  129. public function __destruct() {
  130. if (is_resource($this->smtpConnect)) fclose($this->smtpConnect);
  131. }
  132. }
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement