<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Require PHPMailer files
require_once __DIR__ . '/../vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/../vendor/phpmailer/src/SMTP.php';
require_once __DIR__ . '/../vendor/phpmailer/src/Exception.php';

// Check if the form is submitted
$response = '';
if (isset($_POST['email']) && !empty($_POST['email'])) {
    $email = htmlspecialchars($_POST['email']);
    $name = htmlspecialchars($_POST['name'] ?? '');
    $message = htmlspecialchars($_POST['message'] ?? '');

    try {
        // Initialize PHPMailer for the first email
        $mail = new PHPMailer(true);
		$mail->isSMTP();
		$mail->Host       = 'smtp.mail.us-east-1.awsapps.com';
		$mail->SMTPAuth   = true;
		$mail->SMTPSecure = 'ssl';
		$mail->Port       = 465;

		$mail->Username   = 'contact@finesolartools.com'; // YOUR Gmail/SMTP email
		$mail->Password   = 'n-8u@3JATU@I';           // YOUR Gmail/SMTP app password

		$mail->setFrom('contact@finesolartools.com', 'Finesolartools');
		$mail->addAddress($email, $name);
		$mail->addReplyTo('contact@finesolartools.com', 'Finesolartools');

        // Email content for the user
        $mail->IsHTML(true);
        $mail->Subject = "Unsubscribe";
        $mail->Body='
		  <body style="margin:0; padding:0; background-color:#f1f2f5; font-family:Roboto, Arial, sans-serif; color:#1e3557;">
			<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color:#f1f2f5; margin:0; padding:0; margin-bottom:30px;">
			  <tr>
				<td align="center" style="padding:0; margin:0;">
				
				 <table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0"
													style="background-color:#0b1a4a; padding:40px 0; margin-top:30px;">
													<tr><td></td></tr>
												  </table>

				  <table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0"
					style="background-color:#ffffff; border-radius:6px; max-width:600px; width:100%;">

					<tr>
					  <td align="center" style="padding:20px 20px;">

						<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
						  <tr>
							<td align="center" style="padding:20px 0;">
							  <h3 style="font-size: 22px;">Finesolartools</h3>
							</td>
						  </tr>
						</table>

						<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
						  <tr>
							<td align="left" style="font-size:14px; line-height:22px; color:#1e3557;">

							  <p style="margin:0 0 15px 0;"><b>Hi,</b></p>

							  <p style="margin:0 0 15px 0;">We are sorry to see you go.</p>

								<p style="margin:0 0 15px 0;">This email confirms that you have been successfully unsubscribed from Finesolartools communications. You will no longer receive emails, updates, or newsletters from us.</p>

								<p style="margin:0 0 15px 0;">If this was a mistake or you change your mind in the future, you are always welcome to subscribe again through our website.</p>

							

							  <p style="margin:0;"><b>Best,</b><br><b>Finesolartools</b></p>
							</td>
						  </tr>
						</table>

						<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top:30px;">
						  <tr>
							<td align="center" style="font-size:12px; color:#566881;">
							  <p style="margin:0 0 10px 0;">
								You are receiving this email because you unsubscribed Finesolartools.
							  </p>
							</td>
						  </tr>
						</table>

					  </td>
					</tr>
				  </table>

				</td>
			  </tr>
			</table>
		  </body>';

        // Send the email to the user
        if ($mail->send()) {
            $response = "Successfully unsubscribed our newsletter.";
        } else {
            $response = "Unable to send the email.";
        }

        // Initialize PHPMailer for the admin notification email
        $mail2 = new PHPMailer(true);

        // SMTP settings for the admin notification email
        $mail2->isSMTP();
        $mail2->Host       = 'smtp.mail.us-east-1.awsapps.com';
		$mail2->SMTPAuth   = true;
		$mail2->SMTPSecure = 'ssl';
		$mail2->Port       = 465;

		$mail2->Username   = 'contact@finesolartools.com';
		$mail2->Password   = 'n-8u@3JATU@I';

		$mail2->setFrom('contact@finesolartools.com', 'Finesolartools');
		$mail2->addAddress('contact@finesolartools.com', 'Finesolartools');

        // Email content for the admin
        $mail2->IsHTML(true);
        $mail2->Subject = "unsubscribe details";
        $mail2->Body='
		  <body style="margin:0; padding:0; background-color:#f1f2f5; font-family:Roboto, Arial, sans-serif; color:#1e3557;">
			<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color:#f1f2f5; margin:0; padding:0; margin-bottom:30px;">
			  <tr>
				<td align="center" style="padding:0; margin:0;">
				
				  <table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0"
					style="background:linear-gradient(80deg, rgb(42,204,222) 0%, rgb(214,83,119) 50%, rgb(130,33,215) 100%); padding:40px 0; margin-top:30px;">
					<tr><td></td></tr>
				  </table>

				  <table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0"
					style="background-color:#ffffff; border-radius:6px; max-width:600px; width:100%;">

					<tr>
					  <td align="center" style="padding:20px 20px;">

						<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
						  <tr>
							<td align="center" style="padding:20px 0;">
							  <h3 style="font-size: 22px;">Finesolartools</h3>
							</td>
						  </tr>
						</table>

						<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
						  <tr>
							<td align="left" style="font-size:14px; line-height:22px; color:#1e3557;">

							  <p style="margin:0 0 15px 0;"><b>unsubscribe details,</b></p>

							  <p style="margin:0 0 15px 0;">
							Following user has unsubmitted their subscribe details:
							  </p>

							  <p style="margin:0 0 15px 0;">
							
								Email: '.$email.'

							  </p>

							  <p style="margin:0;">Regards,<br>Finesolartools</p>
							</td>
						  </tr>
						</table>

					  </td>
					</tr>
				  </table>

				</td>
			  </tr>
			</table>
		  </body>';

        // Send the notification email to the admin
        $mail2->send();
    } catch (Exception $e) {
        $response = "Error: " . $mail->ErrorInfo;
    }
} else {
    $response = "All fields are required, please fill the form again.";
}

// Return the response as plain text
echo $response;
?>
