Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package OpenCart
- * @author Daniel Kerr
- * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
- * @license https://opensource.org/licenses/GPL-3.0
- * @link https://www.opencart.com
- */
- /**
- * Mail class
- */
- include_once(DIR_SYSTEM . 'library/shared/phpmailer/PHPMailerAutoload.php');
- class Mail {
- protected $to;
- protected $from;
- protected $sender;
- protected $reply_to;
- protected $cc;
- protected $bcc;
- protected $emailtemplate;
- protected $subject;
- protected $text;
- protected $html;
- protected $attachments = array();
- public $parameter;
- /**
- * Constructor
- *
- * @param string $adaptor
- *
- */
- public function __construct($adaptor = 'mail') {
- $class = 'Mail\\' . $adaptor;
- if (class_exists($class)) {
- $this->adaptor = new $class();
- } else {
- trigger_error('Error: Could not load mail adaptor ' . $adaptor . '!');
- exit();
- }
- }
- /**
- *
- *
- * @param mixed $to
- */
- public function setTo($to) {
- $this->to = $to;
- }
- /**
- *
- *
- * @param string $from
- */
- public function setFrom($from) {
- $this->from = $from;
- }
- /**
- *
- *
- * @param string $sender
- */
- public function setSender($sender) {
- $this->sender = $sender;
- }
- /**
- *
- *
- * @param string $reply_to
- */
- public function setReplyTo($reply_to) {
- $this->reply_to = $reply_to;
- }
- /**
- *
- *
- * @param string $subject
- */
- public function setSubject($subject) {
- $this->subject = $subject;
- }
- /**
- *
- *
- * @param string $text
- */
- public function setText($text) {
- $this->text = $text;
- }
- /**
- *
- *
- * @param string $html
- */
- public function setHtml($html) {
- $this->html = $html;
- }
- /**
- *
- *
- * @param string $filename
- */
- public function addAttachment($filename) {
- $this->attachments[] = $filename;
- }
- /**
- *
- *
- */
- public function getSubject() {
- return $this->subject;
- }
- public function setCc($address) {
- $this->cc = $address;
- }
- public function setBcc($address) {
- $this->bcc = $address;
- }
- public function setEmailTemplate(EmailTemplate $oEmail) {
- $this->emailtemplate = $oEmail;
- }
- public function _sendPhpMailer() {
- if (property_exists($this, 'mail_queue') && $this->mail_queue) {
- return true; // send later via cron
- }
- if (!$this->to) {
- trigger_error('Error: E-Mail to required!');
- exit();
- }
- if (!$this->from) {
- trigger_error('Error: E-Mail from required!');
- exit();
- }
- if (!$this->sender) {
- trigger_error('Error: E-Mail sender required!');
- exit();
- }
- if (!$this->subject) {
- trigger_error('Error: E-Mail subject required!');
- exit();
- }
- if ((!$this->text) && (!$this->html)) {
- trigger_error('Error: E-Mail message required!');
- exit();
- }
- $debugMode = false;
- $mail = new PHPMailer($debugMode);
- $mail->CharSet = "UTF-8";
- $mail->Subject = $this->subject;
- if (is_array($this->to)) {
- foreach ($this->to as $to){
- $mail->AddAddress($to);
- }
- } else {
- $mail->AddAddress($this->to);
- }
- $mail->AddReplyTo($this->from, $this->sender);
- if(!empty($this->reply_to)){
- $mail->AddReplyTo($this->reply_to, $this->sender);
- } elseif(!empty($this->replyto)){
- $mail->AddReplyTo($this->replyto, $this->sender); # OC 2.0.2.0
- } else {
- $mail->AddReplyTo($this->from, $this->sender);
- }
- if($this->cc){
- $mail->AddCC($this->cc);
- }
- if($this->bcc){
- $mail->AddBCC($this->bcc);
- }
- if (!$this->html) {
- $mail->Body = $this->text;
- } else {
- $mail->MsgHTML($this->html);
- if ($this->text) {
- $mail->AltBody = $this->text;
- }
- }
- if($this->attachments){
- foreach ($this->attachments as $attachment) {
- if (file_exists($attachment)) {
- $mail->AddAttachment($attachment);
- }
- }
- }
- if ($this->protocol == 'smtp') {
- $mail->SetFrom(html_entity_decode($this->smtp_username, ENT_QUOTES, 'UTF-8'), $this->sender);
- $mail->IsSMTP();
- $mail->Host = $this->smtp_hostname;
- $mail->Port = $this->smtp_port;
- $mail->SMTPDebug = ($debugMode) ? 1 : 0;
- if($this->smtp_port == '587'){
- $mail->SMTPAuth = true;
- $mail->SMTPSecure = "tls";
- } elseif ($this->smtp_port == '465') {
- $mail->SMTPAuth = true;
- $mail->SMTPSecure = "ssl";
- }
- if (!empty($this->smtp_username) && !empty($this->smtp_password)) {
- $mail->SMTPAuth = true;
- $mail->Username = html_entity_decode($this->smtp_username, ENT_QUOTES, 'UTF-8');
- $mail->Password = html_entity_decode($this->smtp_password, ENT_QUOTES, 'UTF-8');
- }
- $mail->SMTPOptions = array(
- 'ssl' => array(
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- 'allow_self_signed' => true
- )
- );
- }
- if($debugMode){
- try {
- $mail->Send();
- } catch(phpmailerException $e) {
- trigger_error(strip_tags($e->errorMessage()));
- } catch(Exception $e) {
- trigger_error(strip_tags($e->getMessage()));
- }
- } else {
- $mail->Send();
- }
- }
- public function send() {
- $this->_sendPhpMailer();
- }
- public function _send() {
- $this->_sendPhpMailer();
- if ($this->emailtemplate) {
- $data = get_object_vars($this);
- $this->emailtemplate->sent($data);
- }
- }
- private function _send() {
- if (!$this->to) {
- throw new \Exception('Error: E-Mail to required!');
- }
- if (!$this->from) {
- throw new \Exception('Error: E-Mail from required!');
- }
- if (!$this->sender) {
- throw new \Exception('Error: E-Mail sender required!');
- }
- if (!$this->subject) {
- throw new \Exception('Error: E-Mail subject required!');
- }
- if ((!$this->text) && (!$this->html)) {
- throw new \Exception('Error: E-Mail message required!');
- }
- foreach (get_object_vars($this) as $key => $value) {
- $this->adaptor->$key = $value;
- }
- $this->adaptor->send();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement