I’m using PHPMailer to send emails from a program, and when it works ok it is fine, but I need to be able to notify user that email was sent, or failed. Currently if it fails it just displays general PHP error message, and if ok just returns to page called by header statement.
The main program uses Bootstrap 4, and the email handler is pure PHP.
What the users want is an alert box to tell them the status,
The PHP code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
//use PHPMailer\PHPMailer\Exception;
require 'inc/PHPMailer/Exception.php';
require 'inc/PHPMailer/PHPMailer.php';
require 'inc/PHPMailer/SMTP.php';
include 'inc/accsessconn.php'; // standard connection
$type = $_GET['etype'];
if ($type === "inv") {
$htmlmess = $_POST['summernote1'];
$class = $_POST['classname'];
$from = $_POST['from'];
$Email = $_POST['to_email'];
$subject = $_POST['subject'];
$wl_id = $_POST['id'];
$BCC = $_POST['bcc'];
} elseif ($type === "del") {
$htmlmess = $_POST['summernote2'];
$class = "";
$from = $_POST['from2'];
$Email = $_POST['to_email2'];
$subject = $_POST['subject2'];
$wl_id = $_POST['id2'];
$BCC = $_POST['bcc2'];
} elseif ($type === "adhoc") {
$htmlmess = $_POST['summernote3'];
$class = "";
$from = $_POST['from3'];
$Email = $_POST['to_email3'];
$subject = $_POST['subject3'];
$wl_id = $_POST['id3'];
$BCC = $_POST['bcc3'];
}
$mail = new PHPMailer(TRUE);
try {
$mail->From = $from;
$mail->FromName = "Club Admin";
$mail->addAddress($Email);
$mail->addBCC($BCC);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlmess;
$mail->Send();
$errtext = "Email sent";
if ($type == "inv") {
// $comment = "Invitation to " . $class . " emailed on " . date("d/m/Y");
$invite = $class . " offered " . date("d/m/Y");
$sql = "UPDATE `waitlist` SET wl_class_offered=? where wl_ID = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('si', $invite, $wl_id);
$stmt->execute();
$stmt->close();
}
} catch (phpmailerException $e) {
$errtext = "Email NOT sent - please try again";
}
//echo '<script type="text/javascript">';
//echo ' alert($errtext)';
//echo '</script>';
header("location: wl_fulllist.php");
I was hoping the currently commented out section at end would display a javascript alert, but was just ignored.
Can anyone help?