<?php
/**
 * Created by PhpStorm.
 * User: Arslan
 * Date: 1/7/2022
 * Time: 4:32 PM
 */

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

if(isset($_POST['fname'])) {

    $name = $_POST['fname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $organization = $_POST['organization'];
    $designation = $_POST['designation'];
    $city = $_POST['city'];
    $gender = $_POST['gender'];
    $participation = $_POST['participation'];
    $sess = "";
    $fee = $_POST['fee'];

    ?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="utf-8" />
        <link href="main.css" rel="stylesheet">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
        <script src="main.js"></script>
    </head>

    <body>
    <div class="jumbotron text-center">
        <h1 class="display-3">Thank You!</h1>
        <p class="lead"><strong>Please check your email</strong> for further instructions on how to complete your registration.</p>
        <hr>
        <p>
            Having trouble? <a href="mailto:events@ips.net.pk">Contact us</a>
        </p>
        <p class="lead">
            <a class="btn btn-primary btn-sm" href="https://ips.org.pk" role="button">Continue to homepage</a>
        </p>
    </div>
    </body>
    </html>
    <?php

    foreach ($_POST['sessions'] as $session){
        $sess = $sess . ',' . $session;
    }

    $sess = substr($sess,1);

    require 'phpMailer/PHPMailer.php';
    require 'phpMailer/SMTP.php';
    require 'phpMailer/OAuth.php';

//Create an instance; passing `true` enables exceptions
    $mail = new PHPMailer(true);

    try {
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host = 'box5633.bluehost.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth = true;                                   //Enable SMTP authentication
        $mail->Username = 'events@ips.net.pk';                     //SMTP username
        $mail->Password = 'Events@2018';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
        $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

        //Recipients
        $mail->setFrom('events@ips.net.pk');
        $mail->addAddress('events@ips.net.pk', 'Institute of Policy Studies');     //Add a recipient
        //$mail->addAddress('ellen@example.com');               //Name is optional
        //$mail->addReplyTo('info@example.com', 'Information');
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');

        //Attachments
        //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = 'Contemporary Debates Registration';
        $mail->Body = '<b>Registration Details</b><br>
                        Name: ' . $name . '<br>
                        Organization: ' . $organization . '<br>
                        Designation: ' . $designation . '<br>
                        Email: ' . $email . '<br>
                        Phone: ' . $phone . '<br>
                        Participation: ' . $participation . '<br>
                        Gender: ' . $gender . '<br>
                        City: ' . $city . '<br>
                        Sessions: ' . $sess;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        //echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }

    try{
        $mail->clearAllRecipients( ); // clear all
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host = 'box5633.bluehost.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth = true;                                   //Enable SMTP authentication
        $mail->Username = 'events@ips.net.pk';                     //SMTP username
        $mail->Password = 'Events@2018';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
        $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

        //Recipients
        $mail->setFrom('events@ips.net.pk');
        $mail->addAddress($email, $name);     //Add a recipient
        //$mail->addAddress('ellen@example.com');               //Name is optional
        //$mail->addReplyTo('info@example.com', 'Information');
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');

        //Attachments
        //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = 'Contemporary Debates Registration';
        $mail->Body = '<b>Thank you!</b><br>
                        Please pay PKR <br>' . $fee . ' on given account number
                        <p class="text-center">Bank Account Details:<br>
                                    Title: <b>Institute of Policy Studies- IPS Press</b><br>
                                    Branch code: <b>0301</b><br>
                                    Account No: <b>0103442105</b><br>
                                    IBAN: <b>PK85MEZN0003010103442105</b><br>
                            Meezan Bank Limited<br>
                            Jinnah Avenue, Blue Area, Islamabad.
                            </p><br>
                            And send receipt on events@ips.net.pk';
        $mail->AltBody = '';

        $mail->send();
        //echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    } catch (Exception $e){

    }
}

?>