<?php
header("Access-Control-Allow-Origin: *");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require("Exception.php");
require("PHPMailer.php");
require("SMTP.php");

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$company = $_REQUEST['company'];
$phone = $_REQUEST['phone'];
$message = $_REQUEST['message'];

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "rajanbabu.babu9@gmail.com";
$mail->Password = "ihifhrkoebpeuhnr";
$mail->SetFrom("rajanbabu.babu9@gmail.com");
$mail->Subject = "DeliverEZ Contact Form Details";
$table = '';
if ($name != '' || $email != '' || $company != '' && $phone != '') {
	$table = '<p>Hi,</p>
<p>Find the below contact details submitted:</p>
<table style="border-collapse:collapse;border-spacing:0" class="tg">
<thead>
<tr>
<th style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">S.No</th>
<th style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">Field</th>
<th style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">Value</th>
</tr>
</thead>
<tbody>';

if($name != '') {
	$name_row = '<tr>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">1.</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">Name</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">' .$name. '</td>
	</tr>';
	$table .= $name_row;
}

if($email != '') {
	$email_row ='<tr>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">2.</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">Email</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">'. $email .'</td>
	</tr>';
	$table .= $email_row;

}
if($company != '') {
	$company_row = '<tr>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">3.</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">Company</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">' .$company. '</td>
	</tr>';
	$table .= $company_row;

}
if($phone != '') {
	$phone_row = '<tr>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">4.</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">Phone</td>
	<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;overflow:hidden;padding:10px 5px;text-align:left;vertical-align:top;word-break:normal">'. $phone .'</td>
	</tr>';
	$table .= $phone_row;

}

$table .= '</tbody>
</table>';
}
$msg = '<br/><p><b>Message:<b></p>';
if($message != '') {
	$msg .= '<p>'.$message.'</p>';	
} else {
	$msg .= '<p> Nil </p>';	
}
$mail->Body = $table.$msg;
$mail->AddAddress("rajan.rajan2203@gmail.com", "Rajan");
$mail->AddCC($email);

 if(!$mail->Send()) {
	$data = array('response' => 'failed');
 } else {
	$data = array('response' => 'success');
 }
 echo json_encode($data);

/*
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Mailer = "smtp";
$mail->Host = "mail.sivakasicrackers.co.in";
$mail->Port = "25"; // 8025, 587 and 25 can also be used. Use Port 465 for SSL.
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = "support@sivakasicrackers.co.in";
$mail->Password = "Welcome@123";

$mail->From = "support@sivakasicrackers.co.in";
$mail->FromName = "Test";
$mail->AddAddress("rajanbabu.babu9@gmail.com", "Rajan");

$mail->Subject = "Hi!";
$mail->Body = "Hi! Test mail?";
$mail->WordWrap = 50;

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
exit;
} else {
echo 'Message has been sent.';
}*/
?>