stuppid_bot

PHP Mailer

Nov 6th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2.  
  3. class Mailer {
  4.     protected $_boundary = false,
  5.         $_charset = 'UTF-8',
  6.         $_header = '',
  7.         $_additional = '';
  8.     public $is_html = false;
  9.  
  10.     public function setCharset($charset) {
  11.         $this->_charset = $charset;
  12.     }
  13.  
  14.     public function addHeader($name, $value) {
  15.         $this->_headers[] = "$name: $value";
  16.     }
  17.  
  18.     public function encode($data) {
  19.         return '=?' . $this->_charset . '?B?' . base64_encode($data) . '?=';
  20.     }
  21.  
  22.     public function setFrom($email, $name = null) {
  23.         $this->addHeader('From', $name ? $this->encode($name) . " <$email>" : $email);
  24.     }
  25.  
  26.     public function addAttachment($name, $data, $type = null) {
  27.         if (!$this->_boundary) {
  28.             $this->_boundary = uniqid();
  29.         }
  30.         if (!$type) {
  31.             $type = 'application/octet-stream';
  32.         }
  33.         $this->_additional .= "--{$this->_boundary}\r\n";
  34.         $this->_additional .= "Content-Type: $type; name=\"" . $this->encode($name) . "\"\r\n";
  35.         $this->_additional .= "Content-Disposition: attachment\r\n";
  36.         $this->_additional .= "Content-Transfer-Encoding: base64\r\n\r\n";
  37.         $this->_additional .= chunk_split(base64_encode($data)) . "\r\n";
  38.     }
  39.  
  40.     public function attachFile($filename, $content_type = null) {
  41.         $this->addAttachment(basename($filename), file_get_contents($filename), $content_type);
  42.     }
  43.  
  44.     public function send($addr, $subject = '', $message = '') {
  45.         $message = chunk_split(base64_encode($message));
  46.         $content_type = 'Content-Type: text/' . ($this->is_html ? 'html' : 'plain') . '; charset=' . $this->_charset;
  47.         $content_transfer_encoding = 'Content-Transfer-Encoding: base64';
  48.         $body = '';
  49.         if ($this->_boundary) {
  50.             $this->_headers[] = "Content-Type: multipart/mixed; boundary=\"{$this->_boundary}\"";
  51.             $body .= "--{$this->_boundary}\r\n";
  52.             $body .= "$content_type\r\n";
  53.             $body .= "$content_transfer_encoding\r\n\r\n";
  54.             $body .= "$message\r\n";
  55.             $body .= $this->_additional;  
  56.             $body .= "--{$this->_boundary}--";
  57.         }
  58.         else {
  59.             $this->_headers[] = $content_type;
  60.             $this->_headers[] = $content_transfer_encoding;
  61.             $body = $message;
  62.         }
  63.         return mail($addr, $this->encode($subject), $body, implode("\r\n", $this->_headers));
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment