<?php
require("../includes/app_credentials.php");
require("includes/email-credentials.php");

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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

require("../includes/db_connect.php");

if($_SERVER['REQUEST_METHOD']==='POST' && !empty($_POST['accountId']) && !empty($_POST['email']) ) {

	$accountId = $_POST['accountId'];
	$email = $_POST['email'];
	$name = "";

	$usr_sql = "SELECT * FROM users WHERE accountId =? AND email=? LIMIT 1";
	$usr_sth = $conn->prepare($usr_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
	$usr_sth->execute([$accountId, $email]);
	$users = $usr_sth->fetchAll();

	if (count($users) > 0) {
		foreach ($users as $key => $value) {
			$name = $value['fullname'];
		}
	}else {
		die("Invalid request!");
	}

	$emojis_subj = "=?utf-8?Q?=F0=9F=8C=8F=F0=9F=92=BB=F0=9F=93=88=F0=9F=92=B0?=";

	$mail = new PHPMailer();

	try {

		//Server settings  
		$mail->isSMTP();
		$mail->Host = $smtp_host;
		$mail->SMTPAuth = true;
		$mail->Username = $smtp_username;
		$mail->Password = $smtp_password;
		$mail->SMTPSecure = $smtp_encryption;
		$mail->Port = $smtp_port;

        //Recipients
		$mail->setFrom($smtp_email, $web_app_name);       
		$mail->addAddress($email, $name);

        //Content
		$mail->isHTML(true);                           
		$mail->Subject = "We welcome you to ROCKETTA! - $emojis_subj";

		$mail->Body = "<div style=\"color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0 auto!important;min-width:100%;padding:0;text-align:center!important;width:100%!important\">

		<div style=\"background:#ebeff6;border-collapse:collapse;border-spacing:0;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;height:auto;line-height:19px;margin:0;padding:0;text-align:left;vertical-align:top;width:100%\">

		<div style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<div align=\"center\" valign=\"top\" class=\"m_1863363065879357917center\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0 auto;padding:0;text-align:center;vertical-align:top;word-break:normal\">

		<center style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;min-width:580px;width:100%\">

		<div style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0 auto;padding:0;text-align:inherit;vertical-align:top;width:580px\">

		<div style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<div style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0;padding-top:30px; padding-bottom: 30px; text-align:left;vertical-align:top;word-break:normal\">

		<!-- LOGO AREA -->
		<table class=\"m_1863363065879357917row m_1863363065879357917brad-top\" style=\"background:#fff;border-collapse:collapse;border-spacing:0;border-top-left-radius:3px!important;border-top-right-radius:3px!important;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">

		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917wrapper m_1863363065879357917offset-by-one m_1863363065879357917last m_1863363065879357917pt30\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:30px;text-align:left;vertical-align:top;word-break:normal\">

		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">

		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917six m_1863363065879357917sub-columns m_1863363065879357917pb0\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;min-width:0;padding:0 0 10px;padding-bottom:0;padding-right:10px;text-align:left;vertical-align:top;width:50%;word-break:normal\">

		<a href=\"$web_base_url\" style=\"color:#2ba6cb;font-family:'Roboto','Helvetica','Arial',sans-serif!important;text-decoration:none\" target=\"_blank\">
		<img src=\"$web_base_url/img/logo.png\" class=\"m_1863363065879357917logo CToWUd\" style=\"border:none;clear:both;display:block;float:none;font-family:'Roboto','Helvetica','Arial',sans-serif!important;max-width:200px;outline:none;padding-bottom:0;padding-left:0;padding-top:0;text-decoration:none;width:auto\">
		</a>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>

		<!-- HEADER AREA -->
		<table class=\"m_1863363065879357917row\" style=\"background:#fff;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%; padding-bottom: 30px;\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917wrapper m_1863363065879357917last m_1863363065879357917offset-by-one m_1863363065879357917pt80\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:80px;text-align:left;vertical-align:top;word-break:normal\">
		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917text-pad-small m_1863363065879357917pb0\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:0;text-align:left;vertical-align:top;word-break:normal\">
		<p class=\"m_1863363065879357917title m_1863363065879357917pb20\" style=\"color:#222327;font-family:Helvetica!important;font-size:38px;font-weight:700;line-height:46px;margin:0;padding:0;padding-bottom:20px;text-align:left\">We welcome you to ROCKETTA! - \u{1F30F}\u{1F4BB}\u{1F4C8} </p>
		<p class=\"m_1863363065879357917text m_1863363065879357917pb10\" style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:30 0px 0px 0px;padding:0;padding-bottom:10px;text-align:left\"><strong style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important\">Hi $name,</strong>
		</p>
		<p class=\"m_1863363065879357917text\" style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">World class automated crypto trading bot.
		<br/><br/>
		</p>
		<p class=\"m_1863363065879357917text\" style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">Earn from the comfort of your home. Powerful AI powered crypto trading bot to help you save time, trade 24/7, and automate your trading.
		<br/> <br/>
		</p>
		<p class=\"m_1863363065879357917text\" style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">For guidance on how to start using Rocketta, to get a bot and trade, kindly with the video attached below. 
And keep updated on our YouTube page and other social media platform
		<br/> <br/>
		</p>

		<p class=\"m_1863363065879357917text\" style=\"color:#222327;font-family:Helvetica!important;font-size:18px;font-weight:400;line-height:28px;margin:0;padding:0;text-align:left\">
		The $web_app_name Team
		<br/> <br/>
		</p>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>


		<!-- Any Questions Area -->
		<table class=\"m_1863363065879357917row m_1863363065879357917brad-bot\" style=\"background:#fff;border-bottom-left-radius:3px!important;border-bottom-right-radius:3px!important;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%; border-top: 1px solid #ebeff6;\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">

		<td class=\"m_1863363065879357917wrapper m_1863363065879357917last m_1863363065879357917offset-by-one m_1863363065879357917pt40\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:40px;text-align:left;vertical-align:top;word-break:normal\">
		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
		<tbody>

		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917text-pad-small m_1863363065879357917pb40\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:40px;text-align:left;vertical-align:top;word-break:normal\">
		<p class=\"m_1863363065879357917small-text m_1863363065879357917pt5 m_1863363065879357917pb5\" style=\"color:#474a50;font-family:Helvetica!important;font-size:14px;font-weight:400;line-height:22px;margin:0;padding:0;padding-bottom:5px;padding-top:5px;text-align:left\">If you have any questions, you can contact us at&nbsp;<a href=\"mailto:$web_support_email\" style=\"color:#1c72f8!important;font-family:Helvetica!important;text-decoration:none\" target=\"_blank\">$web_support_email</a>.
		</p>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>


		<table class=\"m_1863363065879357917row m_1863363065879357917footer\" style=\"background:#ebeff6;border-collapse:collapse;border-spacing:0;display:block;font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top;width:100%\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917wrapper m_1863363065879357917last m_1863363065879357917offset-by-one m_1863363065879357917pt50\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:10px 20px 0 0;padding-left:50px;padding-right:0;padding-top:50px;text-align:left;vertical-align:top;word-break:normal\">
		<table class=\"m_1863363065879357917ten m_1863363065879357917columns\" style=\"border-collapse:collapse;border-spacing:0;font-family:'Roboto','Helvetica','Arial',sans-serif!important;margin:0;padding:0;text-align:left;vertical-align:top;width:480px\">
		<tbody>
		<tr style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important;padding:0;text-align:left;vertical-align:top\">
		<td class=\"m_1863363065879357917pb50 m_1863363065879357917text-pad-small\" style=\"border-collapse:collapse!important;color:#222;font-family:'Roboto','Helvetica','Arial',sans-serif!important;font-size:14px;font-weight:400;line-height:19px;margin:0;padding:0 0 10px;padding-bottom:0px;text-align:left;vertical-align:top;word-break:normal\">



		<div style=\"font-family:'Roboto','Helvetica','Arial',sans-serif!important\">
		<p class=\"m_1863363065879357917address-text\" style=\"color:#474a50!important;font-family:Helvetica!important;font-size:14px!important;font-weight:400;line-height:19px;margin:0;padding:0;text-align:left\">10 Wandsworth Rd, London, LND SW8 2LL</p>
		</div>
		</td>
		</tr>
		</tbody>
		</table>
		</td>
		</tr>
		</tbody>
		</table>


		</div>

		</div>

		</div>

		</center>

		</div>

		</div>

		</div>
		</div>";

		$mail->send();
		//createLog("email sent", "e", "logs");
		set_email_sent();
	} catch (Exception $e) {
		createLog("Unable to send verification mail: ".$mail->ErrorInfo, "e", "logs");
	}

}else {
	die("Invalid request");
}

function set_email_sent () {
	global $accountId, $email, $web_base_url;

	$email_type = "welcome";

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "accountId=$accountId&email=$email&email_type=$email_type");
	curl_setopt($ch, CURLOPT_URL, "$web_base_url/email/set_emails_sent");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
	curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
	curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 10); 
	curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
	curl_exec($ch);
	curl_close($ch);
}

function createLog($text, $level='i', $file='logs') {
	switch (strtolower($level)) {
		case 'e':
		case 'error':
		$level='ERROR';
		break;
		case 'i':
		case 'info':
		$level='INFO';
		break;
		case 'd':
		case 'debug':
		$level='DEBUG';
		break;
		default:
		$level='INFO';
	}
	error_log(date("[d-m-Y H:i:s]")."\t[".$level."]\t[".basename(__FILE__)."]\t".$text."\n", 3, $file);
}
?>