<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    class email{
        public function mail_send($mailto,$subject,$message,$from_mail,$attachmentPath='',$fromLabel='Bus Ticket',&$jArray=array()) {
            if(1){
                try {
                    $mail = new PHPMailer(true);
                    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
                    $mail->isSMTP();                                      // Set mailer to use SMTP
                    $mail->Host = 'smtp.mailgun.org';  // Specify main and backup SMTP servers
                    $mail->SMTPAuth = true;                               // Enable SMTP authentication
                    $mail->Username = 'postmaster@email.lonexbd.com';                 // SMTP username
                    $mail->Password = 'e8a0fb602037d3f92da996ff5bc8a696-7efe8d73-9a379a03'; // SMTP password
                    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
                    $mail->Port = 587;                                    // TCP port to connect to

                    //Recipients
                    $mail->setFrom($from_mail, $fromLabel);
                    $mail->addAddress($mailto);     // Add a recipient
                    //$mail->addAddress('ellen@example.com');               // Name is optional
                    $mail->addReplyTo($from_mail);
                    //$mail->addCC('cc@example.com');
                    //$mail->addBCC('bcc@example.com');

                    //Attachments
                    if($attachmentPath!=''){
                        $mail->addAttachment($attachmentPath);         // Add attachments
                    }
                    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

                    //Content
                    $mail->isHTML(true);                                  // Set email format to HTML
                    $mail->Subject = $subject;
                    $mail->Body    = $message;
                    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

                    return $mail->send();
                } catch (Exception $e) {
                    $jArray[__LINE__]='Message could not be sent. Mailer Error: '. $mail->ErrorInfo;
                }
            }
            else{
                $uid = md5(uniqid(time()));
                $header = "From: ".SITE_NAME." <".$from_mail.">\n";
                $header .= "Reply-To: ".$from_mail."\n";
                $header .= "MIME-Version: 1.0\n";
                $header .= "Content-Type: text/html; charset=UTF-8\n";
                if(mail($mailto,$subject,$message,$header)){return true;}else{return false;}
            }
        }

        public function mail_body($to,$subject, $title, $mainBody,$attachment='',$fileName='',$from =''){
            $body = '
            <html> 
            <head> 
            <title>'.$subject.'</title> 
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
            <style>
            .bg {
            background:url('.URL.'/images/bg.jpg) repeat-x;
            }
            </style>
            </head> 
            <body marginheight="0" topmargin="0" marginwidth="0" bgcolor="#444444" leftmargin="0"  style="background:url('.URL.'/images/bg.jpg) repeat;"> 
            <table cellspacing="0" border="0" height="100%"  cellpadding="0" width="100%">
            <tr> 
            <td valign="top"> 
            <table cellspacing="0" border="0" align="center" cellpadding="0" width="500"> 
            <tr>
            <td valign="top"> 
            <table cellspacing="0" border="0" cellpadding="0" width="100%">
            <tr> 
            <td height="14" valign="top">  </td> 
            </tr>
            <tr>
            <td valign="top">
            <table cellspacing="0" border="0" cellpadding="0" width="100%">
            <tr>
            <td height="18" valign="top" valign="middle" style="font-size:37px; color:#FFFFFF; font-family: Gerogia, serif; font-style: italic; padding-left: 20px; font-weight:bold;">
            <img src="'.URL.'/images/logo.png" alt="'.SITE_NAME.'" title="'.SITE_NAME.'" style="display: block; ">
            </td>
            </tr>
            </table>
            </td>
            </tr>
            <tr>
            <td valign="top">
            <table cellspacing="0" border="0" cellpadding="0" width="100%">
            <tr>
            <td height="20" valign="top" bgcolor="#FF0000">&nbsp;</td>
            </tr>
            </table>
            </td>
            </tr>

            <tr>
            <td height="2"></td>
            </tr>
            <tr>
            <td valign="top">
            <table cellspacing="0" border="0" cellpadding="0" width="100%">
            <tr>
            <td valign="top" style="background-color: #FFFFFF; font-family: Gerogia, serif; font-size: 12px; color: #2b2b2b; line-height: 18px; padding-left: 20px; padding-right: 20px;">
            <h2>'.$title.'</h2>
            <p>
            '.$mainBody.'</p>
            </td>
            </tr>
            <tr>
            <td height="15" style="background-color:#FFFFFF;"></td>
            </tr>
            </table>
            </td>
            </tr>
            <tr>
            <td valign="top">
            <table cellspacing="0" border="0" cellpadding="0" width="100%">
            <tr>
            <td height="70" style="background-color:#1F3580; font-family: Arial, Helvetica, sans-serif; font-size: 11px; color:#fff;padding-left: 20px; line-height: 18px;">
            </td>
            </tr>
            <tr> 
            </tr>
            </table>
            </td>
            </tr>    
            </table> 
            </td> 
            </tr> 
            </table> 
            </body> 
            </html>';

            $this->mail_send($to,$subject,$body,($from=='')?'no-reply@sohojshop.com/':$from);
        }

    }