stuppid_bot

Класс для отправки почты с вложениями

May 4th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2.  
  3. class Mailer {
  4.     protected $charset = 'UTF-8',
  5.               $to,
  6.               $from,
  7.               $subject = '',
  8.               $attachments = array();
  9.    
  10.     public function setCharset($value) {
  11.         $this->charset = $value;
  12.     }
  13.    
  14.     protected function encode($value) {
  15.         return '=?' . $this->charset . '?B?' . base64_encode($value) . '?=';
  16.     }
  17.    
  18.     public function setFrom($email, $name = null) {
  19.         $this->from = $name ? $this->encode($name) . " <$email>" : $email;
  20.     }
  21.    
  22.     public function setSubject($value) {
  23.         $this->subject = $this->encode($value);
  24.     }
  25.    
  26.     public function addAttachment($name, $data, $type = 'application/octet-stream') {
  27.         $name = $this->encode($name);
  28.         $data = chunk_split( base64_encode($data) );
  29.         $this->attachments[] = compact('name', 'data', 'type');
  30.     }
  31.    
  32.     public function send($to, $body, $is_html = 0) {
  33.         // $headers = array('Mime-Version: 1.0');
  34.         $headers = array();
  35.        
  36.         if ($this->from) {
  37.             $headers[] = 'From: ' . $this->from;
  38.         }
  39.        
  40.         $type = 'Content-Type: text/' . ($is_html ? 'html' : 'plain') . '; charset=' . $this->charset;
  41.         $encoding = 'Content-Transfer-Encoding: 8 bit';
  42.  
  43.         if ( $count = count($this->attachments) ) {
  44.             $boundary = md5( time() );
  45.             $headers[] = 'Content-Type: multipart/mixed; boundary="' . $boundary . '"';
  46.             $body  = array("--$boundary", $type, "$encoding\r\n", $body);
  47.  
  48.             for ($i = 0; $i < $count; ++$i) {
  49.                 $body[] = "--$boundary";
  50.                 $body[] = "Content-Type: " . $this->attachments[$i]['type'] . "; name=\"" . $this->attachments[$i]['name'] . "\"";
  51.                 $body[] = 'Content-Disposition: attachment';
  52.                 $body[] = "Content-Transfer-Encoding: base64\r\n";      
  53.                 $body[] =  $this->attachments[$i]['data'];      
  54.             }
  55.  
  56.             $body = implode("\r\n", $body) . "\r\n--$boundary--";
  57.         }
  58.         else {
  59.             $headers[] = $type;
  60.             $headers[] = $encoding;
  61.         }
  62.  
  63.         return @mail( $to, $this->subject, $body, implode("\r\n", $headers) );
  64.     }
  65.    
  66.     public function html($to, $body) {
  67.         return $this->send($to, $body, 1);
  68.     }
  69. }
  70.  
  71. // $mail = new Mailer;
  72. // $mail->setSubject('Привет');
  73. // $mail->addAttachment('file.txt', 'Some contents');
  74. // $mail->send('[email protected]', 'Как дела?');
  75.  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment