fabi0

Untitled

May 27th, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.10 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Description of Database
  5.  *
  6.  * @author Ivaylo Ivanov
  7.  */
  8.  
  9. namespace Models;
  10.  
  11. class Mailer {
  12.  
  13.     /**
  14.      * This is destination email address
  15.      * @var string
  16.      */
  17.     private $to;
  18.  
  19.     /**
  20.      * This is source email address
  21.      * @var string
  22.      */
  23.     private $from;
  24.  
  25.     /**
  26.      * This is return path email address
  27.      * @var string
  28.      */
  29.     private $return;
  30.  
  31.     /**
  32.      * This is subject for email
  33.      * @var string
  34.      */
  35.     private $subject;
  36.  
  37.     /**
  38.      * This is header for email
  39.      * @var string
  40.      */
  41.     private $header;
  42.  
  43.     /**
  44.      * Pass Variable
  45.      * @param   string  $from    Content from email address
  46.      * @param   string  $to      Content to email address
  47.      * @param   string  $subject Content subject email
  48.      * @param   string  $return  Content return path email address
  49.      * No return value. Pass variable into this class
  50.      */
  51.     function __construct($from, $to, $subject, $return) {
  52.         $this->from = $from;
  53.         $this->to = $to;
  54.         $this->subject = $subject;
  55.         $this->return = $return;
  56.     }
  57.  
  58.     /**
  59.      * Изпраща обикновен Имейл
  60.      * @param   string  $message    Content for this email
  61.      * No return value. Send email
  62.      * @access  public
  63.      * @access  static
  64.      */
  65.     public function simpleMail($message) {
  66.         $this->header = "From: " . $this->from . "\n";
  67.         $this->header .= "Reply-To: " . $this->from . "\n";
  68.         $this->header .= "Return-Path: " . $this->return . "\n";
  69.         $this->header .= "X-Mailer: PHP/" . phpversion() . "\n";
  70.         $this->send($message);
  71.     }
  72.  
  73.     /**
  74.      * Изпраща HTML емаил
  75.      * @param type $html
  76.      * @param type $plainText
  77.      */
  78.     public function htmlMail($html, $plainText = '') {
  79.         $boundary = md5(uniqid(time()));
  80.         $this->header = "From: " . $this->from . "\n";
  81.         $this->header .= "To: " . $this->to . "\n";
  82.         $this->header .= "Return-Path: " . $this->return . "\n";
  83.         $this->header .= "MIME-Version: 1.0\n";
  84.         $this->header .= "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"\n";
  85.  
  86.         // Text Version
  87.         $msgPlainText = "--" . $boundary . "\n";
  88.         $msgPlainText .= "Content-Type: text/plain; charset=iso-8589-1\n";
  89.         $msgPlainText .= "Content-Transfer-Encoding: 8bit\n";
  90.         $msgPlainText .= "If you are seeing this is because you may need to change your\n";
  91.         $msgPlainText .= "preferred message format from HTML to plain text.\n\n";
  92.         if ($plainText == '') {
  93.            
  94.             $plainText = strip_tags($html);
  95.         }
  96.         $msgPlainText .= $plainText . "\n";
  97.  
  98.        
  99.         $msgHtml = "--" . $boundary . "\n";
  100.         $msgHtml .= "Content-Type: text/html; charset=iso-8589-1\n";
  101.         $msgHtml .= "Content-Transfer-Encoding: 8bit\n";
  102.         $msgHtml .= $html;
  103.  
  104.         $message = $msgPlainText . $msgHtml . "\n\n";
  105.        
  106.         $this->send($message);
  107.     }
  108.  
  109.     /**
  110.      * Send simple email with attachment
  111.      * @param   string  $file       File for attachment (file name or path of file)
  112.      * @param   string  $plainText  Message in plain text format
  113.      * No return value. Send simple email with attachment
  114.      * @access  public
  115.      * @access  static
  116.      */
  117.     public function simpleAttachment($file, $plainText = '') {
  118.         $handle = fopen($file, 'rb');
  119.         $data = fread($handle, filesize($file));
  120.         $data = chunk_split(base64_encode($data));
  121.         $filetype = mime_content_type($file);
  122.  
  123.         $boundary = md5(uniqid(time()));
  124.         $this->header = "From: " . $this->from . "\n";
  125.         $this->header .= "To: " . $this->to . "\n";
  126.         $this->header .= "Return-Path: " . $this->return . "\n";
  127.         $this->header .= "MIME-Version: 1.0\n";
  128.         $this->header .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"";
  129.  
  130.         // Text Version
  131.         $msgPlainText = "--" . $boundary . "\n";
  132.         $msgPlainText .= "Content-Type: text/plain; charset=utf-8\n";
  133.         $msgPlainText .= "Content-Transfer-Encoding: 8bit\n";
  134.         if ($plainText != '') {
  135.             $msgPlainText .= $plainText . "\n";
  136.         }
  137.  
  138.         // Attachment Version
  139.         $attach = "--" . $boundary . "\n";
  140.         $attach .= "Content-Type: " . $filetype . "; name=\"" . $file . "\"\n";
  141.         $attach .= "Content-Transfer-Encoding: base64 \n";
  142.         // Need two end of lines
  143.         $attach .= "Content-Disposition: attachment; filename=\"" . $file . "\"\n\n";
  144.         $attach .= $data . "\n\n";
  145.  
  146.         $message = $msgPlainText . $attach;
  147.         // Send Email
  148.         $this->send($message);
  149.     }
  150.  
  151.     /**
  152.      * Изпраща HTML емаил с прикачен файл
  153.      * @param type $file
  154.      * @param type $html
  155.      * @param type $plainText
  156.      */
  157.     public function htmlAttachment($file, $html, $plainText = '') {
  158.         $handle = fopen($file, 'rb');
  159.         $data = fread($handle, filesize($file));
  160.         $data = chunk_split(base64_encode($data));
  161.         $filetype = mime_content_type($file);
  162.  
  163.         $boundary = md5(uniqid(time()));
  164.         $this->header = "From: " . $this->from . "\n";
  165.         $this->header .= "To: " . $this->to . "\n";
  166.         $this->header .= "Return-Path: " . $this->return . "\n";
  167.         $this->header .= "MIME-Version: 1.0\n";
  168.         $this->header .= "Content-Type: multipart/related; boundary=\"" . $boundary . "\"\n";
  169.  
  170.         // Text Version
  171.         $msgPlainText = "--" . $boundary . "\n";
  172.         $msgPlainText .= "Content-Type: text/plain; charset=iso-8589-1\n";
  173.         $msgPlainText .= "Content-Transfer-Encoding: 8bit\n";
  174.         $msgPlainText .= "If you are seeing this is because you may need to change your\n";
  175.         $msgPlainText .= "preferred message format from HTML to plain text.\n\n";
  176.         if ($plainText == '') {
  177.             $plainText = strip_tags($html);
  178.         }
  179.         $msgPlainText .= $plainText . "\n";
  180.  
  181.         // HTML Version
  182.         $msgHtml = "--" . $boundary . "\n";
  183.         $msgHtml .= "Content-Type: text/html; charset=iso-8589-1\n";
  184.         $msgHtml .= "Content-Transfer-Encoding: 8bit\n";
  185.         $msgHtml .= $html . "\n";
  186.  
  187.         // Attachment Version
  188.         $attach = "--" . $boundary . "\n";
  189.         $attach .= "Content-Type: " . $filetype . "; name=\"" . $file . "\"\n";
  190.         $attach .= "Content-Transfer-Encoding: base64 \n";
  191.         // Need two end of lines
  192.         $attach .= "Content-Disposition: attachment; filename=\"" . $file . "\"\n\n";
  193.         $attach .= $data . "\n\n";
  194.  
  195.         $message = "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\"";
  196.         $message .= $msgPlainText . $msgHtml . $attach;
  197.         // Send Email
  198.         $this->send($message);
  199.     }
  200.  
  201.     /**
  202.      * return function mail()
  203.      * @param   string  $message    Content for this email
  204.      * return function mail()
  205.      * @access  private
  206.      * @access  static
  207.      */
  208.     private function send($message) {
  209.         return @mail($this->to, $this->subject, $message, $this->header);
  210.     }
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment