<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

class Email_m extends CI_Model {

    public function send_mail($mail_from,$mail_to,$subject,$msg){
        $setting = get_setting();

         if($setting['email_type']==2):

            $mail = new PHPMailer(true);

        try {
            //Server settings

            // $mail->SMTPDebug = SMTP::DEBUG_SERVER;   //Enable verbose debug output
            $mail->isSMTP();  //Send using SMTP
            $mail->Host       = !empty($setting['smtp_host'])?$setting['smtp_host']:'smtp.gmail.com';//'smtp.gmail.com' ssl://smtp.googlemail.com                      //Set the SMTP server to send through
            $mail->SMTPAuth   = true; //Enable SMTP authentication
            $mail->Username   = $setting['email']; //SMTP username
            $mail->Password   = $setting['password'];; //SMTP password
            $mail->SMTPSecure =$setting['smtp_port']==465 || $setting['smtp_port']==25?PHPMailer::ENCRYPTION_SMTPS:PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption

            $mail->Port       = !empty($setting['smtp_port'])?$setting['smtp_port']:465;
            //587 //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

            //Recipients
            $mail->setFrom($setting['email'],$setting['site_name']);
            $mail->addAddress($mail_to, $setting['site_name']);  


            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //Content
            $mail->isHTML(true);                                  
            $mail->Subject = $subject;
            $mail->Body    = $msg;
            $mail->AltBody = '';

            $mail->send();
            return true;
        } catch (Exception $e) {
            return false;
        }
    else:
        $this->load->library('email');

        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");
        $this->email->from($this->settings['site_name']);
        $this->email->reply_to($this->settings['smtp_mail']);
        $this->email->to($mail_to);
        $this->email->subject($subject);
        $this->email->message($msg);
                //Send email
        if($this->email->send()){
           return true;
       }else{
           return false;
       }
      endif;

    } 


    public function test_mail(){
        $setting = get_setting();

         if($setting['email_type']==2):

            $mail = new PHPMailer(true);

        try {
            // $mail->SMTPDebug = SMTP::DEBUG_SERVER;   //Enable verbose debug output
            $mail->isSMTP();  //Send using SMTP
            $mail->Host       = !empty($setting['smtp_host'])?$setting['smtp_host']:'smtp.gmail.com';//'smtp.gmail.com' ssl://smtp.googlemail.com                      //Set the SMTP server to send through
            $mail->SMTPAuth   = true; //Enable SMTP authentication
            $mail->Username   = $setting['email']; //SMTP username
            $mail->Password   = $setting['password'];; //SMTP password
            $mail->SMTPSecure =$setting['smtp_port']==465 || $setting['smtp_port']==25?PHPMailer::ENCRYPTION_SMTPS:PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption

            $mail->Port       = !empty($setting['smtp_port'])?$setting['smtp_port']:465;
            //587 //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

            //Recipients
            $mail->setFrom($setting['email'],$setting['site_name']);
            $mail->addAddress($setting['email'],$setting['site_name']);  

            //Add a recipient
            // $mail->addAddress('ellen@example.com');               //Name is optional
            // $mail->addReplyTo('info@example.com', 'Information');
            // $mail->addCC('cc@example.com');
            // $mail->addBCC('bcc@example.com');

            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //Content
            $mail->isHTML(true);                                  
            $mail->Subject = 'Test Mail';
            $mail->Body    = 'Mail testing.... Mail working fine';
            $mail->AltBody = '';

            $mail->send();
                   echo '<h4 style="margin-top:20px; color:green;">Message has been sent & working fine</h4>';
            } catch (Exception $e) {
                echo "<h4 style='margin-top:20px; color:red;'>Message could not be sent. Mailer Error: {$mail->ErrorInfo} </h4>";
            }
    else:
        $this->load->library('email');

        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");
        $this->email->from($this->settings['site_name']);
        $this->email->reply_to($this->settings['smtp_mail']);
        $this->email->to($mail_to);
        $this->email->subject($subject);
        $this->email->message($msg);
                //Send email
        if($this->email->send()){
           return true;
       }else{
           return false;
       }
      endif;

    } 

    //After payment successfull by user author will get this mail

    public function get_paid_invoice_to_admin($data,$payment_type){
        $setting = get_setting();
        $user_info = get_all_user_info_id($data['user_id']);
        $mail_body = '<div class="col-md-8">
                            <table class="table" style="width: 60%; max-width: 100%; margin-bottom: 20px;border-spacing: 0;border-collapse:collapse;">
                                <thead>
                                    <tr style="border:1px solid #ddd;">
                                        <th style="border:1px solid #ddd;padding: 10px;" class="">Sl</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Description</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Package name</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Qty</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Price</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr style="border:1px solid #ddd; height: 200px;">
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">#1</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;"><p> <b> '.$user_info['username'].'</b> Payment Paid  successfully </p>,
                                        <p>Payment via '.$payment_type.' </p>
                                        <p>Transaction ID: '.$data['txn_id'].'</p>
                                         <p>This account will expired on <b>'.$user_info['end_date'].'</b></p>
                                        </td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">'.$user_info['package_name'].'</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">1</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">'.$data['price'].'</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">'.get_currency('icon').$data['price'].'</td>
                                    </tr>
                                    <tr>
                                        <td style="border:1px solid #ddd;padding: 10px;" colspan="4"></td>
                                        <td style="border:1px solid #ddd;padding: 10px;">Total</td>
                                        <td style="border:1px solid #ddd;padding: 10px;">'.get_currency('icon').$data['price'].' /-  <em><b>Paid</b></em></td>
                                    </tr>
                                 
                                </tbody>
                            </table>
                        </div>';

        $subject = 'Payment Confirmation invoice mail from user';
        $mail_body = $this->mail_body($mail_body);
       $send = $this->send_mail($user_info['email'],$setting['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
    }


    // this mail will get all new user
    public function send_purchase_invoice($username){
        $setting = get_setting();
        $user_info = get_all_user_info_slug($username);

        $mail_body = '<div class="col-md-8">
                            <table class="table" style="width: 60%; max-width: 100%; margin-bottom: 20px;border-spacing: 0;border-collapse:collapse;">
                                <thead>
                                    <tr style="border:1px solid #ddd;">
                                        <th style="border:1px solid #ddd;padding: 10px;" class="">Sl</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Description</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Package name</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Qty</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Price</th>
                                        <th style="border:1px solid #ddd;padding:10px;" class="">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr style="border:1px solid #ddd; height: 200px;">
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">#1</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">
                                        <p>Thank you <b> '.$user_info['username'].'</b> for registration on <b>'.$setting['site_name'].'</b> </p>
                                            <p>Please payment to via continue your account live.</p>
                                        </td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">'.$user_info['package_name'].'</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">1</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">'.$user_info['price'].'</td>
                                        <td style="border:1px solid #ddd;padding: 10px;vertical-align: text-top;">'.get_currency('icon').$user_info['price'].'</td>
                                    </tr>
                                    <tr>
                                        <td style="border:1px solid #ddd;padding: 10px;" colspan="4"></td>
                                        <td style="border:1px solid #ddd;padding: 10px;">Total</td>
                                        <td style="border:1px solid #ddd;padding: 10px;">'.get_currency('icon').$user_info['price'].' /-</td>
                                    </tr>
                                 
                                </tbody>
                            </table>
                        </div>';

        $subject = 'Purchase invoice mail';
        $mail_body = $this->mail_body($mail_body);
        $send = $this->send_mail($setting['email'],$user_info['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
    }

    /**
      ***  registration confirm mail
    **/ 
    public function send_registration_mail($data,$password,$package){
        $setting = get_setting();
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#f3f3f3; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Congratulations </em> <b>'.$data['username'].'<b></td>
                                    </tr>
                                    <tr>
                                        <td>Name: </td>
                                        <td> <b>'.$data['username'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Email: </td>
                                        <td> <b>'.$data['email'].'</b></td>
                                    </tr>

                                    <tr>
                                        <td>Account Type: </td>
                                        <td> <b>'.$package['type_name'].'</b></td>
                                    </tr>
                                    <tr>
                                        <td>Password: </td>
                                        <td> <b>'.$password.'</b></td>
                                    </tr>
                                    <tr>
                                        <td> 
                                        <h4>Click Verify button to verify your account</h4>
                                        <p><a href="'.base_url('admin/home/verifiy/'.$data['username']).'" style="padding: 10px 20px;background: #82b440;color: #fff;display: inline-block;"> <b>Verify </b></a></p>
                                         </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
            $subject = !empty($setting['reg_subject'])?$setting['reg_subject']:'Registration verification Mail';
            $mail_body = $this->mail_body($mail_body);
           $send =  $this->send_mail($setting['email'],$data['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
        
    }


    /**
      ** After registration Admin will get this mail as new user
    **/

    public function new_user_mail($user,$package)
    {
        $setting = get_setting();
        $msg = '<b>New User</b> <br> User name: <b>'.$user['username'].'</b><br> Email:'.$user['email'].'<br> Account Type: <b>'.$package['type_name'].'</b>';
         $subject = 'New User Email purchase mail';
         $msg = $this->mail_body($msg);
          $send =   $this->send_mail($user['email'],$setting['email'],$subject,$msg);//from,to,sub,msg.   
        if($send){
            return true;
        }else {
            return false;
        }    
    }



    //send reset code to user email/ password recovery mail 
     public function send_recovery_mail($user)
    {
        $setting = get_setting();
        $msg = '<h4>Password Recovery Mail from '.$setting['site_name'].'</h4>
        Hello '.$user['username'].'<br> Use this <b> <u style="background: #ddd;padding: 10px 30px;font-size: 25px;font-weight: bold;letter-spacing: 15px;margin: 10px;display: inline-block;">'.$user['password'].'</u> </b> password to login '.$setting['site_name'].'<p>Don\'t share this password anyone</p>';
       
        $subject = !empty($setting['recovery_subject'])?$setting['recovery_subject']:'Recovery Password';
        $msg = $this->mail_body($msg);
        $send = $this->send_mail($setting['email'],$user['email'],$subject,$msg);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
        
    }

      public function home_contact_mail($data){
       $setting = get_setting();
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#f3f3f3; padding:10px;">
                        <div class="col-md-12">
                         <div class="mail_header" style="background: #4CAF50;padding:10px; color:#fff;width: 59%">
                            <h5 style="padding: 0; margin: 0;">'.$data['subject'].'</h5>
                            <h3 style="padding: 0; margin: 0;">'.$setting['site_name'].'</h3>
                        </div>
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Contact Mail form </em> <b>'.$data['name'].'<b></td>
                                    </tr>
                                    <tr>
                                        <td>Name: </td>
                                        <td> <b>'.$data['name'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Email: </td>
                                        <td> <b>'.$data['email'].'</b></td>
                                    </tr>

                                    <tr>
                                        <td>Message: </td>
                                        <td> <b>'.$data['message'].'</b></td>
                                    </tr>
                                    
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
        $subject = 'Contact Mail';
       $send =  $this->send_mail($data['email'],$setting['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
        
    }


    //Resend verification email
     public function resend_verify_mail($username)
    {
        $setting = get_setting();
        $u_info = get_all_user_info_slug($username);
        $msg = '<h4> Account Verification Mail from - '.$setting['site_name'].'</h4>
        <h4>Hello '.$u_info['username'].'</h4>
        <p> Click  verifiy button to verify your account.</p>
         <a href="'.base_url('admin/home/verifiy/'.$u_info['username']).'" style="padding: 10px 20px;background: #82b440;color: #fff;display: inline-block;">Verify</a>';
        
        $subject = 'Resend Account Verification Mail';
        $msg = $this->mail_body($msg);
        $send = $this->send_mail($setting['email'],$u_info['email'],$subject,$msg);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
    }



    //after stripe/paypal payment success  this mail to user

    public function send_payment_paid_email($data,$payment_via){
        $setting = get_setting();
        $user_info = get_all_user_info_id($data['user_id']);

        $mail_body = '<div class="invoice">
                <div class="invoice_heading" style=" width:89%;text-align: left;padding: 5px 10px; background: #dddd;margin-bottom: 17px;">
                    <h4 style="font-size: 30px;padding: 0;margin: 0;">Invoice</h4>
                </div>
                <div class="" style="margin-bottom: 10px; overflow:hidden;">
                        <table class="table" style="width: 45%;float: left;">
                            <tbody>
                                <tr><td><strong style="width: 150px; display: inline-block;">Name:</strong> '.$user_info['username'].'</td></tr>
                                <tr><td><strong style="width: 150px; display: inline-block;">Email:</strong>'.$user_info['email'].'</td></tr>
                                <tr><td><strong style="width: 150px; display: inline-block;">paid via:</strong>'.$payment_via.'</td></tr>
                            </tbody>
                        </table>
                        <table class="table" style="width: 55%;float: left;">
                            <tbody>
                                <tr><td><strong style="width: 150px; display: inline-block;">Date:</strong> '.full_time(d_time()).'</td></tr>
                                <tr><td><strong style="width: 150px; display: inline-block;">Transaction ID:</strong> '.$data['txn_id'].'</td></tr>
                                <tr><td><strong style="width: 150px; display: inline-block;">From:</strong>'.$setting['site_name'].'</td></tr>
                            </tbody>
                        </table>
                </div>
                
                <table class="table" style="width: 90%; max-width: 100%; margin-bottom: 20px; margin-top: 30px; border-spacing: 0;border-collapse:collapse;">
                    <thead>
                        <tr style="background: #ddd;">
                            <th style="padding: 10px;border:1px solid #ddd;">Sl</th>
                            <th style="padding: 10px;border:1px solid #ddd; width: 65%">Description</th>
                            <th style="padding: 10px;border:1px solid #ddd;">Package</th>
                            <th style="padding: 10px;border:1px solid #ddd;">Qty</th>
                            <th style="padding: 10px;border:1px solid #ddd;">Price</th>
                            <th style="padding: 10px;border:1px solid #ddd;">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr style=" height: 200px;">
                            <td style="padding: 10px;vertical-align: text-top;border:1px solid #ddd;">#1</td>
                            <td style="padding: 10px;vertical-align: text-top;border:1px solid #ddd;">
                                <p>Thank you <b> '.$user_info['username'].'</b>. </p>
                                <p>Your payment has been completed successfully.</p>
                                 <p>Your account will expired on <b>'.$user_info['end_date'].'</b></p>
                            </td>
                            <td style="padding: 10px;vertical-align: text-top;border:1px solid #ddd;">'.$user_info['package_name'].'</td>
                            <td style="padding: 10px;vertical-align: text-top;border:1px solid #ddd;">1</td>
                            <td style="padding: 10px;vertical-align: text-top;border:1px solid #ddd;">'.get_currency('icon').$data['price'].'</td>
                            <td style="padding: 10px;vertical-align: text-top;border:1px solid #ddd;">'.get_currency('icon').$data['price'].'</td>
                        </tr>
                        <tr>
                            <td style="border:1px solid #ddd;padding: 10px;" colspan="4"></td>
                            <td style="border:1px solid #ddd;padding: 10px;">Total</td>
                            <td style="border:1px solid #ddd;padding: 10px;">'.get_currency('icon').$data['price'].' /-  <em><b>Paid</b></em></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            ';
       
       
        $subject = empty($setting['paypal_subject'])?$setting['paypal_subject']:'Payment Confirmation';
        $mail_body = $this->mail_body($mail_body);
       $send =  $this->send_mail($setting['email'],$user_info['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
    }


    /* Offline payment request from user to admin
    ================================================== */
     public function offline_payment_request_mail($slug,$account_slug,$txn_id){
        $setting = get_setting();
        $u_info = get_all_user_info_slug($slug);
        $package_info = get_id_by_package_slug($account_slug);

        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#f3f3f3; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>An Offline payment request from </em> <b>'.$u_info['username'].'<b></td>
                                    </tr>
                                    <tr>
                                        <td>Name: </td>
                                        <td> <b>'.$u_info['username'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Email: </td>
                                        <td> <b>'.$u_info['email'].'</b></td>
                                    </tr>

                                    <tr>
                                        <td>Account Type: </td>
                                        <td> <b>'.$package_info['type_name'].'</b></td>
                                    </tr>
                                    <tr>
                                        <td>Price: </td>
                                        <td> <b>'.get_currency('icon').$package_info['price'].'</b></td>
                                    </tr>

                                     <tr>
                                        <td>Request ID: </td>
                                        <td> <b>'.$txn_id.'</b></td>
                                    </tr>
                                    
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
       

        $subject = 'Offline payment request';
        $mail_body = $this->mail_body($mail_body);
       $send =  $this->send_mail($setting['email'],$u_info['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
        
    }


    /*  Account expire reminder mail to user
    ================================================== */
    public function expire_reminder_mail($username,$day_left)
    {
        $setting = get_setting();
        $u_info = get_all_user_info_slug($username);
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#f3f3f3; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Account Expire reminder from </em> <b>'.$setting['site_name'].'<b></td>
                                    </tr>
                                    <tr>
                                        <td>Name: </td>
                                        <td> <b>'.$u_info['username'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Email: </td>
                                        <td> <b>'.$u_info['email'].'</b></td>
                                    </tr>
                                    
                                    <tr>
                                        <td>End Date: </td>
                                        <td> <b>'.full_time($u_info['end_date']).'</b></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <td> <b>'.$day_left.'</b></td>
                                    </tr>
                                    
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
        
        $subject = 'Expire reminder mail';
        $mail_body = $this->mail_body($mail_body);
       $send =  $this->send_mail($setting['email'],$u_info['email'],$subject,$mail_body);//from,to,sub,msg.
       if($send){
            return true;
        }else {
            return false;
        }
    }


    /*  Account expired mail to user
    ================================================== */

    public function account_expire_mail($username,$day_left)
    {
        $setting = get_setting();
        $u_info = get_all_user_info_slug($username);
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#f3f3f3; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Account Expire reminder from </em> <b>'.$setting['site_name'].'<b></td>
                                    </tr>
                                    <tr>
                                        <td>Name: </td>
                                        <td> <b>'.$u_info['username'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Email: </td>
                                        <td> <b>'.$u_info['email'].'</b></td>
                                    </tr>
                                    
                                    <tr>
                                        <td>End Date: </td>
                                        <td> <b>'.full_time($u_info['end_date']).'</b></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <td> <b> Your account already expired</b></td>
                                    </tr>
                                    
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
        $subject = 'Expire reminder mail';
        $mail_body = $this->mail_body($mail_body);
        $send = $this->send_mail($setting['email'],$u_info['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
        
    }


    /* send  New user notification to user with password (user create by admin)
    ================================================== */
    
    public function send_new_user_by_admin_mail($data,$package,$password){
        $setting = get_setting();
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#f3f3f3; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Congratulations </em> <b>'.$data['username'].'<b> </td>
                                    </tr>
                                    <tr>
                                        <td>Name: </td>
                                        <td> <b>'.$data['username'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Email: </td>
                                        <td> <b>'.$data['email'].'</b></td>
                                    </tr>

                                    <tr>
                                        <td>Account Type: </td>
                                        <td> <b>'.$package['type_name'].'</b></td>
                                    </tr>
                                    <tr>
                                        <td>Password: </td>
                                        <td> <b>'.$password.'</b></td>
                                    </tr>
                                    <tr>
                                        <td> 
                                            <h4>Account created by author</h4>
                                         </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
            $subject = !empty($setting['reg_subject'])?$setting['reg_subject']:'Create account by Author';
            $mail_body = $this->mail_body($mail_body);
            $send = $this->send_mail($setting['email'],$data['email'],$subject,$mail_body);//from,to,sub,msg.
        if($send){
            return true;
        }else {
            return false;
        }
    }



    /*    start user mail area
    ================================================== */
    public function user_mail($mail_from,$mail_to,$subject,$msg,$user_id){
        $user_setting = get_user_settings($user_id);
        $setting = get_setting();
         if($user_setting['email_type']==2):

            $mail = new PHPMailer(true);

        try {
            //Server settings

            // $mail->SMTPDebug = SMTP::DEBUG_SERVER;   //Enable verbose debug output
            $mail->isSMTP();  //Send using SMTP
            $mail->Host       = !empty($user_setting['smtp_host'])?$user_setting['smtp_host']:'smtp.gmail.com';//'smtp.gmail.com' ssl://smtp.googlemail.com                      //Set the SMTP server to send through
            $mail->SMTPAuth   = true; //Enable SMTP authentication
            $mail->Username   = $user_setting['contact_email']; //SMTP username
            $mail->Password   = $user_setting['smtp_password']; //SMTP password
            $mail->SMTPSecure =$setting['smtp_port']==465 || $setting['smtp_port']==25?PHPMailer::ENCRYPTION_SMTPS:PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption

            $mail->Port       = !empty($setting['smtp_port'])?$setting['smtp_port']:465;
            //587 //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

            //Recipients 
            $mail->setFrom($mail_to,$setting['site_name']);
            $mail->addReplyTo($mail_from, $setting['site_name']);
            $mail->addAddress($mail_to);  

            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //Content
            $mail->isHTML(true);                                  
            $mail->Subject = $subject;
            $mail->Body    = $msg;
            $mail->AltBody = '';

            $mail->send();
            return true;
        } catch (Exception $e) {
            echo "<h4 style='margin-top:20px; color:red;'>Message could not be sent. Mailer Error: {$mail->ErrorInfo} </h4>";
        }
    else:
        $this->load->library('email');

        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");
        $this->email->from($setting['site_name']);
        $this->email->reply_to($mail_from);
        $this->email->to($mail_to);
        $this->email->subject($subject);
        $this->email->message($msg);
        //Send email
       if($this->email->send()){
         return true;
       }else{
         return false;
       }
      endif;

    } 

     

    public function appointment_mail($slug,$data){
        $setting = get_setting();
        $u_info = get_user_info_by_slug($slug);
        $u_settings = get_user_settings($u_info['id']);
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#eee; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Hello </em> <b>'.$u_info['username'].',<b> You have an appointment form '.$data['email'].'</td>
                                    </tr>
                                    <tr>
                                        <td>Title: </td>
                                        <td> <b>'.$data['title'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Message: </td>
                                        <td> <b>'.$data['message'].'</b></td>
                                    </tr>

                                     <tr>
                                        <td>Date: </td>
                                        <td> <b>'.full_time($data['date']).'</b></td>
                                    </tr>

                                    
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
        $subject = 'Appointment Mail';
        $mail_body = $this->mail_body($mail_body);
        $send = $this->user_mail($data['email'],$u_settings['contact_email'],$subject,$mail_body,$u_info['id']);//from,to,sub,msg,user_id.
        if($send){
            return true;
        }else {
            return false;
        }
    }


    public function user_contact($data){
        $setting = get_setting();
        $u_info = get_user_info_by_slug($data['slug']);
        $u_settings = get_user_settings($u_info['id']);
        $mail_body = '<div class="container">
                    <div class="row mail_content" style="border:1px solid #d6d6d6; background-color:#eee; padding:10px;">
                        <div class="col-md-12">
                            <div class="mail_template">
                                <table>
                                    <tr>
                                        <td><em>Hello </em> <b>'.$u_info['username'].',<b> You have a contact Mail form <b>'.$data['email'].'</b></td>
                                    </tr>
                                    <tr>
                                        <td>username: </td>
                                        <td> <b>'.$data['username'].'</b></td>
                                    </tr>
                                    <tr>
                                        <td>email: </td>
                                        <td> <b>'.$data['email'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>subject: </td>
                                        <td> <b>'.$data['subject'].'</b></td>
                                    </tr>
                                     <tr>
                                        <td>Message: </td>
                                        <td> <b>'.$data['message'].'</b></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>';
        $subject = 'Contact Mail';
        $mail_body = $this->mail_body($mail_body);
        $send = $this->user_mail($data['email'],$u_settings['contact_email'],$subject,$mail_body,$u_info['id']);//from,to,sub,msg,user_id.
        if($send){
            return true;
        }else {
            return false;
        }
    }
   
    public function mail_body($body){
        $data = '
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html lang="en" dir="rtl" dir="'.direction().'">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </head>
        <body>
            '.$body.'
        </body>
        </html>
        ';
    return $data;
    }

}

