Ernie,
It works fine, however what I have noticed is that if multiple messages are submitted at the same time by different site visitors, or even if the forms are submitted within like 30 seconds - 1 minute apart, the mail is not always getting sent. is that normal for PHPMailer? Regardless though, here is the final code I used:
error_reporting(E_ALL); // TEMP ! ! ! ! ! ! ! ! ! ! !
ini_set(“display_errors”, 1); // TEMP ! ! ! ! ! ! ! ! ! ! !
//get form data submitted
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];
$products_interested_in = “”;
$message = $_POST[‘message’];
$errors = ‘’;
//gather error message is form data incomplete
if(empty($_POST[‘firstname’]) ||
empty($_POST[‘lastname’]) ||
empty($_POST[‘email’]) ||
empty($_POST[‘phone’]))
{
$errors .= "Error: Please fill in all required fields.";
exit($errors);
}
if(empty($_POST[‘message’]))
{
$message = “No message provided”;
}
//check for valid email address
if (!preg_match(
“/^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,})$/i”, $email))
{
$errors .= “Error: You’ve entered an invalid email address. Please try again.”;
exit($errors);
}
//gather info on products interested in
if(isset($_POST[‘checkboxes’])) {
foreach($_POST[‘checkboxes’] as $selected){
$products_interested_in .= ", " . $selected;
}
$products_interested_in = substr($products_interested_in, 1, strlen($products_interested_in) - 1);
} else {
$products_interested_in = “No Products Were Indicateed by the Sender.”;
}
//put the body of the email together
$email_body = “A visitor to your website has sent you their contact information:”
"<strong>Name:</strong> " . $firstname . " " . $lastname
"<strong>Email:</strong> ". $email
"<strong>Phone:</strong> " . $phone
"<strong>Products Interested In:</strong> " . $products_interested_in
"<strong>Message:</strong> " . $message;
// Import PHPMailer classes into the global namespace
ini_set(‘include_path’, ‘PHPMailer’);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require “…/PHPMailer/src/PHPMailer.php”;
require “…/PHPMailer/src/SMTP.php”;
require “…/PHPMailer/src/Exception.php”;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don’t have access to that
date_default_timezone_set(“America/Chicago”);
//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom("[email protected]", “CompanyName Corporate”);
//Set an alternative reply-to address
$mail->addReplyTo($email, $firstname . " " . $lastname);
//Set who the message is to be sent to
$mail->addAddress("[email protected]", “OwnersName”);
//Set the subject line
$mail->Subject = “Contact Form Submission Notification”;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$temp_HTML =
"
Website Form Submission
$email_body
";
$mail->msgHTML($temp_HTML);
//plain text body alternative
$mail->AltBody = $email_body;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: ". $mail->ErrorInfo;
} else {
echo “”;
}