Advertisement
Guest User

Untitled

a guest
Jun 1st, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.05 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package     OpenCart
  4.  * @author      Daniel Kerr
  5.  * @copyright   Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
  6.  * @license     https://opensource.org/licenses/GPL-3.0
  7.  * @link        https://www.opencart.com
  8. */
  9.  
  10. /**
  11. * Mail class
  12. */
  13. include_once(DIR_SYSTEM . 'library/shared/phpmailer/PHPMailerAutoload.php');
  14.  
  15. class Mail {
  16.     protected $to;
  17.     protected $from;
  18.     protected $sender;
  19.     protected $reply_to;
  20.     protected $cc;
  21.     protected $bcc;
  22.     protected $emailtemplate;
  23.     protected $subject;
  24.     protected $text;
  25.     protected $html;
  26.     protected $attachments = array();
  27.     public $parameter;
  28.  
  29.     /**
  30.      * Constructor
  31.      *
  32.      * @param   string  $adaptor
  33.      *
  34.     */
  35.     public function __construct($adaptor = 'mail') {
  36.         $class = 'Mail\\' . $adaptor;
  37.        
  38.         if (class_exists($class)) {
  39.             $this->adaptor = new $class();
  40.         } else {
  41.             trigger_error('Error: Could not load mail adaptor ' . $adaptor . '!');
  42.             exit();
  43.         }  
  44.     }
  45.    
  46.     /**
  47.      *
  48.      *
  49.      * @param   mixed   $to
  50.      */
  51.     public function setTo($to) {
  52.         $this->to = $to;
  53.     }
  54.    
  55.     /**
  56.      *
  57.      *
  58.      * @param   string  $from
  59.      */
  60.     public function setFrom($from) {
  61.         $this->from = $from;
  62.     }
  63.    
  64.     /**
  65.      *
  66.      *
  67.      * @param   string  $sender
  68.      */
  69.     public function setSender($sender) {
  70.         $this->sender = $sender;
  71.     }
  72.    
  73.     /**
  74.      *
  75.      *
  76.      * @param   string  $reply_to
  77.      */
  78.     public function setReplyTo($reply_to) {
  79.         $this->reply_to = $reply_to;
  80.     }
  81.    
  82.     /**
  83.      *
  84.      *
  85.      * @param   string  $subject
  86.      */
  87.     public function setSubject($subject) {
  88.         $this->subject = $subject;
  89.     }
  90.    
  91.     /**
  92.      *
  93.      *
  94.      * @param   string  $text
  95.      */
  96.     public function setText($text) {
  97.         $this->text = $text;
  98.     }
  99.    
  100.     /**
  101.      *
  102.      *
  103.      * @param   string  $html
  104.      */
  105.     public function setHtml($html) {
  106.         $this->html = $html;
  107.     }
  108.    
  109.     /**
  110.      *
  111.      *
  112.      * @param   string  $filename
  113.      */
  114.     public function addAttachment($filename) {
  115.         $this->attachments[] = $filename;
  116.     }
  117.    
  118.     /**
  119.      *
  120.      *
  121.      */
  122.     public function getSubject() {
  123.         return $this->subject;
  124.     }
  125.    
  126.     public function setCc($address) {
  127.         $this->cc = $address;
  128.     }
  129.    
  130.     public function setBcc($address) {
  131.         $this->bcc = $address;
  132.     }
  133.    
  134.     public function setEmailTemplate(EmailTemplate $oEmail) {
  135.         $this->emailtemplate = $oEmail;
  136.     }
  137.    
  138.     public function _sendPhpMailer() {
  139.         if (property_exists($this, 'mail_queue') && $this->mail_queue) {
  140.             return true; // send later via cron
  141.         }
  142.  
  143.         if (!$this->to) {
  144.             trigger_error('Error: E-Mail to required!');
  145.             exit();        
  146.         }
  147.  
  148.         if (!$this->from) {
  149.             trigger_error('Error: E-Mail from required!');
  150.             exit();                
  151.         }
  152.  
  153.         if (!$this->sender) {
  154.             trigger_error('Error: E-Mail sender required!');
  155.             exit();                
  156.         }
  157.  
  158.         if (!$this->subject) {
  159.             trigger_error('Error: E-Mail subject required!');
  160.             exit();                
  161.         }
  162.  
  163.         if ((!$this->text) && (!$this->html)) {
  164.             trigger_error('Error: E-Mail message required!');
  165.             exit();                
  166.         }
  167.        
  168.         $debugMode = false;
  169.                
  170.         $mail  = new PHPMailer($debugMode);
  171.        
  172.         $mail->CharSet = "UTF-8";
  173.                
  174.         $mail->Subject = $this->subject;
  175.        
  176.         if (is_array($this->to)) {
  177.             foreach ($this->to as $to){
  178.                 $mail->AddAddress($to);
  179.             }
  180.         } else {
  181.             $mail->AddAddress($this->to);
  182.         }
  183.        
  184.         $mail->AddReplyTo($this->from, $this->sender);
  185.        
  186.         if(!empty($this->reply_to)){
  187.             $mail->AddReplyTo($this->reply_to, $this->sender);
  188.         } elseif(!empty($this->replyto)){
  189.             $mail->AddReplyTo($this->replyto, $this->sender); # OC 2.0.2.0
  190.         } else {
  191.             $mail->AddReplyTo($this->from, $this->sender);
  192.         }
  193.        
  194.         if($this->cc){
  195.             $mail->AddCC($this->cc);
  196.         }
  197.        
  198.         if($this->bcc){
  199.             $mail->AddBCC($this->bcc);
  200.         }
  201.        
  202.         if (!$this->html) {
  203.             $mail->Body = $this->text;
  204.         } else {
  205.             $mail->MsgHTML($this->html);
  206.             if ($this->text) {
  207.                 $mail->AltBody = $this->text;
  208.             }
  209.         }
  210.  
  211.         if($this->attachments){
  212.             foreach ($this->attachments as $attachment) {
  213.                 if (file_exists($attachment)) {
  214.                     $mail->AddAttachment($attachment);
  215.                 }
  216.             }
  217.         }
  218.  
  219.         if ($this->protocol == 'smtp') {
  220.             $mail->SetFrom(html_entity_decode($this->smtp_username, ENT_QUOTES, 'UTF-8'), $this->sender);
  221.            
  222.             $mail->IsSMTP();
  223.             $mail->Host = $this->smtp_hostname;
  224.             $mail->Port = $this->smtp_port;
  225.             $mail->SMTPDebug = ($debugMode) ? 1 : 0;
  226.             if($this->smtp_port == '587'){
  227.                 $mail->SMTPAuth = true;
  228.                 $mail->SMTPSecure = "tls";
  229.             } elseif ($this->smtp_port == '465') {
  230.                 $mail->SMTPAuth = true;
  231.                 $mail->SMTPSecure = "ssl";
  232.             }
  233.             if (!empty($this->smtp_username)  && !empty($this->smtp_password)) {
  234.                 $mail->SMTPAuth = true;
  235.                 $mail->Username = html_entity_decode($this->smtp_username, ENT_QUOTES, 'UTF-8');               
  236.                 $mail->Password = html_entity_decode($this->smtp_password, ENT_QUOTES, 'UTF-8');   
  237.             }
  238.  
  239.             $mail->SMTPOptions = array(
  240.                 'ssl' => array(
  241.                     'verify_peer' => false,
  242.                     'verify_peer_name' => false,
  243.                     'allow_self_signed' => true
  244.                 )
  245.             );
  246.         }
  247.        
  248.         if($debugMode){
  249.             try {
  250.                 $mail->Send();
  251.             } catch(phpmailerException $e) {
  252.                 trigger_error(strip_tags($e->errorMessage()));
  253.             } catch(Exception $e) {
  254.                 trigger_error(strip_tags($e->getMessage()));
  255.             }
  256.         } else {
  257.             $mail->Send();
  258.         }  
  259.     }
  260.    
  261.     public function send() {
  262.         $this->_sendPhpMailer();
  263.     }
  264.  
  265.     public function _send() {
  266.         $this->_sendPhpMailer();
  267.  
  268.         if ($this->emailtemplate) {
  269.             $data = get_object_vars($this);
  270.  
  271.             $this->emailtemplate->sent($data);
  272.         }
  273.     }
  274.  
  275.     private function _send() {
  276.         if (!$this->to) {
  277.             throw new \Exception('Error: E-Mail to required!');
  278.         }
  279.  
  280.         if (!$this->from) {
  281.             throw new \Exception('Error: E-Mail from required!');
  282.         }
  283.  
  284.         if (!$this->sender) {
  285.             throw new \Exception('Error: E-Mail sender required!');
  286.         }
  287.  
  288.         if (!$this->subject) {
  289.             throw new \Exception('Error: E-Mail subject required!');
  290.         }
  291.  
  292.         if ((!$this->text) && (!$this->html)) {
  293.             throw new \Exception('Error: E-Mail message required!');
  294.         }
  295.        
  296.         foreach (get_object_vars($this) as $key => $value) {
  297.             $this->adaptor->$key = $value;
  298.         }
  299.        
  300.         $this->adaptor->send();
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement