<?php

ob_start();
ini_set('memory_limit', '-1');
$from           = $option['form_email'];
$to_email_array = (array) $option['to_email'];
$message        = $option['message'];
$subject        = $option['subject'];
$filename       = $option['filename'];
$smtp_hostname  = $option['smtp_hostname'];
$smtp_username  = $option['smtp_username'];
$smtp_password  = $option['smtp_password'];

if (!$from || !$to_email_array) {
    echo 'error';

    return;
}
require('smtp.php');
require('sasl.php');
$myip        = getHostByName(php_uname('n'));
$file        = 'log_' . $myip . '.txt';
$sender_line = __LINE__;
/*if(strlen($from)==0)
    die("Please set the messages sender address in line ".$sender_line." of the script ".basename(__FILE__)."\n");
if(count($to_email_array)==0)
    die("Please set the messages recipient address in line ".$recipient_line." of the script ".basename(__FILE__)."\n");
*/

$smtp                           = new smtp_class();
$smtp->host_name                = $smtp_hostname;//"smtp.nexmail.in";
$smtp->host_port                = 587;
$smtp->ssl                      = 0;
$smtp->start_tls                = 0;
$smtp->localhost                = 'localhost';
$smtp->direct_delivery          = 0;
$smtp->timeout                  = 10;
$smtp->data_timeout             = 0;
$smtp->debug                    = 1;
$smtp->html_debug               = 1;
$smtp->pop3_auth_host           = '';
$smtp->user                     = $smtp_username;//"info@nexmail.in";
$smtp->realm                    = '';
$smtp->password                 = $smtp_password;//"nexindia111";
$smtp->workstation              = '';
$smtp->authentication_mechanism = '';

$smtp->SMTPOptions = [
    'ssl' => [
        'verify_peer'       => false,
        'verify_peer_name'  => false,
        'allow_self_signed' => true]];

try {
    $separator = md5(time());
    // carriage return type (we use a PHP end of line constant)
    $eol        = PHP_EOL;
    $attachment = '';

    if ($filename) {
        $handle   = fopen($filename, 'rb');
        $contents = fread($handle, filesize($filename));
        fclose($handle);
        $attachment = chunk_split(base64_encode($contents));
    }
    // no more headers after this, we start the body! //
    $body = '--' . $separator . $eol;
    $body .= 'Content-Transfer-Encoding: 7bit' . $eol . $eol;
    $body .= '' . $eol;
    // message
    $body .= '--' . $separator . $eol;
    $body .= 'Content-Type: text/html; charset="iso-8859-1"' . $eol;
    $body .= 'Content-Transfer-Encoding: 8bit' . $eol . $eol;
    $body .= $message . $eol;

    // attachment
    if ($attachment) {
        $body .= '--' . $separator . $eol;
        $body .= 'Content-Type: application/octet-stream; name="' . $filename . '"' . $eol;
        $body .= 'Content-Transfer-Encoding: base64' . $eol;
        $body .= 'Content-Disposition: attachment' . $eol . $eol;
        $body .= $attachment . $eol;
        $body .= '--' . $separator . '--';
    }

    $t1           = implode(',', $tomail);
    $file_pointer = fopen($file, 'w');
    fwrite($file_pointer, $t1);
    fclose($file_pointer);

    foreach ($to_email_array as $key => $tomail) {
        $smtp->SendMessage($from, [$tomail], [
                    'MIME-Version: 1.0',
                    'Content-type: multipart/mixed; boundary=' . $separator,
                    "From: $from",
                    "To: $tomail",
                    "Subject: $subject",
                    'Date: ' . strftime('%a, %d %b %Y %H:%M:%S %Z'),
                ], $body);
    }
} catch (Exception $e) {
    $t1           = 'error';
    $file_pointer = fopen($file, 'w');
    fwrite($file_pointer, $t1);
    fclose($file_pointer);


    return false;
}
