stuppid_bot

PHP Mailer Class

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