Advertisement
seinoxygen

SMTP CLass

Feb 18th, 2012
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.46 KB | None | 0 0
  1. class Mail {
  2.    
  3.     private $connection;
  4.    
  5.     public $server;
  6.     public $port = 25;
  7.     public $timeout = 60;
  8.    
  9.     private $newline = "\r\n";
  10.     private $charset = "utf-8";
  11.    
  12.     private $format = "plain";
  13.    
  14.     public $username;
  15.     public $password;
  16.    
  17.     private $headers;
  18.    
  19.     private $from;
  20.     private $to = array();
  21.     private $cc = array();
  22.     private $bcc = array();
  23.    
  24.     public $subject;
  25.     public $message;
  26.    
  27.     public $debug = array();
  28.    
  29.     public function __construct($server = null, $port = 25, $username = null, $password = null){
  30.         if(!is_null($server)){
  31.             $this->server = $server;
  32.             $this->port = $port;
  33.             $this->username = $username;
  34.             $this->password = $password;
  35.         }
  36.         set_time_limit(240);
  37.     }
  38.    
  39.     public function initialize($config = array()){
  40.        
  41.     }
  42.    
  43.     public function from($email, $name = null){
  44.         $this->from = $email;
  45.         if(is_null($name)){
  46.             $this->headers['From'] = "<$email>";
  47.         }
  48.         else{
  49.             $this->headers['From'] = "$name <$email>";
  50.         }
  51.     }
  52.    
  53.     public function reply_to($email){
  54.         if(is_null($name)){
  55.             $this->headers['Reply-To'] = "<$email>";
  56.         }
  57.         else{
  58.             $this->headers['Reply-To'] = "$name <$email>";
  59.         }
  60.     }
  61.    
  62.     public function to($email){
  63.         if(is_array($email)){
  64.             $this->to = array_merge($this->to, $email);
  65.         }
  66.         else{
  67.             array_push($this->to, $email);
  68.         }
  69.     }
  70.    
  71.     public function cc($email){
  72.         if(is_array($email)){
  73.             $this->cc = array_merge($this->cc, $email);
  74.         }
  75.         else{
  76.             array_push($this->cc, $email);
  77.         }
  78.     }
  79.    
  80.     public function bcc($email){
  81.         if(is_array($email)){
  82.             $this->bcc = array_merge($this->bcc, $email);
  83.         }
  84.         else{
  85.             array_push($this->bcc, $email);
  86.         }
  87.     }
  88.    
  89.     public function subject($subject){
  90.         $this->subject = $subject;
  91.     }
  92.        
  93.     public function message($message){
  94.         $this->message = $message;
  95.     }
  96.        
  97.     private function compile_message(){
  98.         //plain
  99.         $headers = "";        
  100.         $headers .= "MIME-Version: 1.0" . $this->newline;
  101.        
  102.         foreach ($this->headers as $key => $val) {
  103.             $headers .= $key . ": " . $val . $this->newline;
  104.         }
  105.        
  106.         $headers .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  107.         $headers .= "Content-Transfer-Encoding: 8bit" . $this->newline;
  108.                
  109.         return $headers . $this->newline . $this->newline .$this->message;
  110.     }
  111.    
  112.     public function command($command, $nls = 1){
  113.        
  114.         $newlines = "";
  115.         for ($i = 0; $i < $nls; $i++) {
  116.             $newlines .= $this->newline;
  117.         }
  118.        
  119.         fwrite($this->connection, $command . $newlines);
  120.            
  121.         array_push($this->debug, "Command: $command");
  122.        
  123.         array_push($this->debug, "Response: ".$this->response());
  124.        
  125.         $command = htmlentities($command);
  126.         echo "<pre>Command: $command</pre>";
  127.     }
  128.    
  129.     public function response(){
  130.         $data = "";
  131.         while ($str = fgets($this->connection, 512)) {
  132.             $data .= $str;
  133.  
  134.             if (substr($str, 3, 1) == " ") {
  135.                 break;
  136.             }
  137.         }
  138.         echo "<pre>$data</pre>";
  139.         return $data;
  140.     }
  141.        
  142.     public function send(){
  143.         $success = false;
  144.  
  145.         $this->connection = fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
  146.        
  147.         $this->command('EHLO '.$this->server);
  148.  
  149.         $this->command('AUTH LOGIN');
  150.        
  151.         $this->command(base64_encode($this->username));
  152.         $this->command(base64_encode($this->password));      
  153.                
  154.         if(!empty($this->from)){
  155.             $this->command('MAIL FROM: <' . $this->from .'>');
  156.         }
  157.        
  158.         if(!empty($this->to)){
  159.             foreach($this->to as $to){
  160.                 $this->command('RCPT TO: <' . $to . '>');
  161.             }
  162.         }
  163.        
  164.         if(!empty($this->cc)){
  165.             foreach($this->cc as $cc){
  166.                 $this->command('RCPT TO: <' . $cc . '>');
  167.             }
  168.         }
  169.        
  170.         if(!empty($this->bcc)){
  171.             foreach($this->bcc as $bcc){
  172.                 $this->command('RCPT TO: <' . $bcc . '>');
  173.             }
  174.         }      
  175.        
  176.         $this->command('DATA');
  177.        
  178.         $this->command('From: ' . $this->from);
  179.        
  180.         $this->command('To: ' . implode(',', $this->to));
  181.        
  182.         $this->command('Reply-To: ' . $this->from);
  183.              
  184.         $this->command('Subject: '.$this->subject);
  185.                
  186.         fflush($this->connection);
  187.                
  188.         if($this->format != "html"){            
  189.             $message = $this->compile_message();
  190.            
  191.             $this->command($message, 1);
  192.            
  193.             $response = $this->command('.');
  194.         }
  195.                
  196.         if(substr($response, 0, 3) == 250){
  197.             $success = true;
  198.         }
  199.        
  200.         $this->command('QUIT');
  201.         fclose($this->connection);
  202.                
  203.         return $success;
  204.     }
  205.    
  206.     public function debug(){
  207.         return $this->debug;
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement