<?php
// For send of email selected
// $_SESSION['mail_select']  Id of email_contact selected
// $_SESSION['mail_where']  Condition where last select of email_contact
$select_record = $_SESSION['mail_select'];
$count = count($_SESSION['mail_select']);
if ($count == 0 ) { //  All Record of List
	$sql=$_SESSION['mail_sql'];
	if ($_SESSION['mail_where'] <> ''){
		$sql.= " and dateEmail is null";
	} else {
		$sql.= " where dateEmail is null";
	}
	$resql=db_query($sql,$conn);
	while($row=db_fetch_array($resql))
		{
			$select_record[] = $row['idemail_contact'];
		}
	$_SESSION['mail_select'] = $select_record;
}

// field of Config
$GMAIL_Username = $_SESSION['config'][array_search('GMAIL_Username', array_column($_SESSION['config'], 'name'))][value];
$GMAIL_Password = $_SESSION['config'][array_search('GMAIL_Password', array_column($_SESSION['config'], 'name'))][value];
$GMAIL_setFrom_email = $_SESSION['config'][array_search('GMAIL_setFrom_email', array_column($_SESSION['config'], 'name'))][value];$GMAIL_Username = $_SESSION['config'][array_search('GMAIL_Username', array_column($_SESSION['config'], 'name'))][value];
$GMAIL_setFrom_name = $_SESSION['config'][array_search('GMAIL_setFrom_name', array_column($_SESSION['config'], 'name'))][value];
$EDITOR_UrlBase0 = $_SESSION['config'][array_search('EDITOR_UrlBase0', array_column($_SESSION['config'], 'name'))][value];
$EDITOR_BaseVirtual0 = $_SESSION['config'][array_search('EDITOR_BaseVirtual0', array_column($_SESSION['config'], 'name'))][value];


//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require __DIR__.'/../phpmailer_6.1/autoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

// Activo condificacción utf-8
$mail->CharSet = 'UTF-8';

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_OFF;

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $GMAIL_Username;

//Password to use for SMTP authentication
$mail->Password = $GMAIL_Password;
$mail->Password = "qfbN0qUj2MteedU5Yv9f";

//Set who the message is to be sent from
$mail->setFrom($GMAIL_setFrom_email,$GMAIL_setFrom_name);

//Set an alternative reply-to address
// $mail->addReplyTo('fernandohumanes@gmail.com', 'First Last');

//Set the subject line
$mail->Subject = $masterData['subject'];

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$message = str_replace('"'.$EDITOR_BaseVirtual0,'"'.$EDITOR_UrlBase0,$masterData['message']);

// $mail->msgHTML($message);

// Attachments
$filesArray = my_json_decode($masterData['attachments']);
foreach ($filesArray as $File) {
	$mail->addAttachment(__DIR__."/../".$File["name"],$File["usrName"],'base64', $File["type"]);
}

$count_send = 0;
foreach ($_SESSION['mail_select'] as &$contact) {
	$sql = "SELECT contact.name, contact.surname, email_contact.email FROM contact join email_contact
					on (contact.contact_id = email_contact.contact_contact_id and email_contact.idemail_contact = $contact)";
	$resql=db_query($sql,$conn);
	while($row=db_fetch_array($resql)) {
		$count_send = $count_send + 1;
		// Message customization
		$message0 = str_replace('{name}',$row['name'],$message);
		$message0 = str_replace('{surname}',$row['surname'],$message0);
		$message0 = str_replace('{email}',$row['email'],$message0);

		$mail->msgHTML($message0);
		//  $mail->AltBody = $mail->html2text($message0); // Text plain alternative
		$mail->addAddress($row['email'], $row['name'].' '.$row['surname']);
		$status = $mail->send();
		// Updating the email sending record
		$sql2 = "update email_contact set dateEmail = now(), Status = '$status' where idemail_contact = $contact"; 
		$resql2=db_query($sql2,$conn);
	}
}
// delete variables of session
unset($_SESSION['mail_select']); 
unset($_SESSION['mail_where']);


?>