<?php 
session_start();
sleep(1);
require './authenticate.user.php';
error_reporting(E_ALL);
require './configuration.php';
require_once './includes/common.functions.php';
require_once ('./classes/database.php');
require_once ('./classes/functions.php');
$sqlObj = new mysqlClass;
$funcObj = new funcClass;
$conntion = $sqlObj->mysqlClass();
$id = intval($_GET['id']);
$resultSetSMTP = array();
$query = sprintf("SELECT MAILER_TYPE, TITLE, HOST, PORT, USER, PASSWORD, AUTHENTICATION	FROM ##smtp WHERE ID = '%s'",
mysqli_real_escape_string($conntion,$id));
$resultSetSMTP = $funcObj->CustomQuery($query);
if (count($resultSetSMTP))
{
	$defTitle		= $resultSetSMTP[0]['TITLE'];
	if ($resultSetSMTP[0]['MAILER_TYPE'] == 'smtp')
	{
		$defHOST 		= $resultSetSMTP[0]['HOST'];
		$defPORT 		= $resultSetSMTP[0]['PORT'];
		$defUSER 		= $resultSetSMTP[0]['USER'];
		$defPassword	= base64_decode(base64_decode($resultSetSMTP[0]['PASSWORD']));
		$defAuth		= ($resultSetSMTP[0]['AUTHENTICATION']	 == '1') ? 'true' : 'false';
	}
}
else
{
	echo "Invalid or missing parameter";
	exit;
}


function replaceEmailVars($body, $emailVar)
{
	
	$today = date("F j, Y, g:i A");
	
	$body		= str_replace('[EMAILHEADER]',			$emailVar['EMAILHEADER'],		$body);
	$body		= str_replace('[ABSPATH]',				$emailVar['ABSPATH'],			$body);
	$body		= str_replace('[DATETIME]',				$emailVar['DATETIME'],			$body);
	$body		= str_replace('[SMTP]',					$emailVar['SMTP'], 				$body);
	return $body;
}

//include_once('./classes/class.phpmailer.smtptest.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/vendor/autoload.php';
$mail = new PHPMailer(true);
$body       = file_get_contents('./templates/admin.smtp.test.tpl');
$altBody	=
"
[DATETIME]

This is an e-mail message sent automatically by  Segnant Mailer while testing the settings for  <strong>[SMTP]</strong> account.

Congratulations, your SMTP is working fine.
";
/********* EMAIL SETUP ***************/
global $absPath;
$emailVar 	= array();

$emailVar['EMAILHEADER']		= 'images/email/header.jpg';
$emailVar['ABSPATH'] 			= $absPath;
$emailVar['DATETIME']			= date("F j, Y, g:i a");
$emailVar['SMTP']				= $defTitle;

if ($resultSetSMTP[0]['MAILER_TYPE'] == 'smtp')
{
	//$mail->IsSMTP();  SMTP IS DOWN SO SEND EMAILS THROUGH SENDMAIL INSTEAD.
	//$mail->IsSendmail(); // telling the class to use SendMail transport
	$mail->IsSMTP();				
	$mail->SMTPAuth   = $defAuth;   
	$mail->Host       = $defHOST;   
	$mail->Port       = $defPORT;   
	$mail->Username   = $defUSER;  
	$mail->Password   = $defPassword;
	$mail->SetFrom($defUSER);
	if($defPORT == 465){
		$mail->SMTPSecure = 'ssl';
	}else{
		$mail->SMTPOptions = array(
		'ssl' => array(
			'verify_peer' => false,
			'verify_peer_name' => false,
			'allow_self_signed' => true
		)
	);

	}
	
	
	
}
else if ($resultSetSMTP[0]['MAILER_TYPE'] == 'sendmail')
{
	$mail->SetFrom('stevecurry@segnant.com');
	$mail->IsSendmail(); // telling the class to use SendMail transport
}
else
{
	$mail->SetFrom('stevecurry@segnant.com');
	// no need to do anything.
	// phpmailer will use the default mail() function.
}
	
$mail->Subject    = "$defTitle - SMTP Test Message";
$mail->AltBody    = "
[DATETIME]

This is an e-mail message sent automatically by  Segnant Mailer while testing the settings for  <strong>[SMTP]</strong> account.

Congratulations, your SMTP is working fine.
"; // optional, comment out and test

$records	= $funcObj->fetchParameterValue('SMTP_TEST_EMAIL_RECIPIENTS')[0]['FIELDVALUE'];

if (empty($records))
{
	echo '<h3>SMTP test email recipient(s) are not defined.</h3>
	Please visit <em>Administration</em> > <em>General Settings</em> and edit <br />
	SMTP_TEST_EMAIL_RECIPIENTS parameter and enter email address in the parameter <br />
	value textbox.
	';
	exit;
}

$allRecipients = $funcObj->str2array($records, ';');
echo "<h3>$defTitle - SMTP Test</h3><br />";
$table = '<table cellspacing="1" cellpadding="4" border="0" width="300" bgcolor="#ccc" style="margin-top: 8px;">
<tr bgcolor="#e6e6e6">
	<th align="left">Email</th>
	<th align="center">Status</th>
</tr>
';

foreach ($allRecipients as $row)
{
	$table .=  '<tr bgcolor="#ffffff">';
	if (!validate_email ($row))
	{
		$table .=  '<td width="80%" align="left">'.$row.'</td>';
		$table .=  '<td align="center"><img src="./images/email_error.png" alt="" title="Email Failed" /></td>';
	}	
	else
	{
		$mail->AddAddress($row);
		$body = replaceEmailVars($body, $emailVar);
		$mail->MsgHTML($body);
		$mail->AltBody = replaceEmailVars($altBody, $emailVar);
		
		if($mail->Send()) 
		{
			$table .=  '<td width="80%" align="left">'.$row.'</td>';
			$table .=  '<td align="center"><img src="./images/email_go.gif" alt=""  title="Email Sent" /></td>';
		} 
		else 
		{ 
			$table .=  '<td width="80%" align="left">'.$row.'</td>';
			$table .=  '<td align="center"><img src="./images/email_error.png" alt=""  title="Email Failed" /></td>';
		}
		
	}
	$table .=  '</tr>';	
}
$table .=  '</table><p>&nbsp;</p><p>&nbsp;</p>';
echo $table;
?>