问题
i'm needing some help with this PHPmailer config code to provide simple form validation in case for any reason the Jquery validation fails or if Javascript is disabled.
the SUCCESS part of this does work!
meaning, the form submission sends both emails as expected, & shows "Email sent!" to the submitter.
the ERROR part of this does not work!
meaning, if any field is left empty, the form submission does not send any emails, and does not show anything but a blank page to the submitter.
EDIT: correction - i just did another round of testing & discovered that the 1st Catch is catching if the Email field is empty. but that's it. i would rather the 2nd catch catch that, along with the other required fields. that must be something built-in to PHPMailer. /edit.
1) i want to have the custom error message in the 2nd catch
display to the submitter if the form fields that i want to be required are empty. In this example, I'd like to require the "name" & "email" fields (more in real form) but not the "message" field.
somebody suggested i use the function validateEmpty
code you see below the catches
, but i can't get it to work. do i have it in the wrong placement within the script or doing something else wrong with it?
2) it looks to me like this function validateEmpty
part is just for the "name" field, do i have to duplicate it for the "email" & any other required fields? or is there a better way to just add which form fields i want as "required" to this one function piece?
3) it's also been suggested that i log any error messages using use error_log()
. how do i do that? is that what produces the text file of error messages i've seen elsewhere in the ftp directory?
4) someone also suggested using an $error & $success flag
. what is that & how do i do it if it's different than doing what i've got going on here?
5) lastly, i can easily test the 2nd Catch by just submitting the form with empty fields. i'm not sure how i can test the 1st Catch; is that possible or even worth doing?
<?php
if (isset($_POST['submit'])) {
date_default_timezone_set('US/Central');
require 'PHPMailer-5.2.26/PHPMailerAutoload.php';
function sendemail(
$SK_emailTo,
$SK_emailSubject,
$SK_emailBody
) {
$mail = new PHPMailer(true);
$mail->setFrom('myEmail@gmail.com', 'My Name');
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress($SK_emailTo);
$mail->Subject = $SK_emailSubject;
$mail->Body = $SK_emailBody;
$mail->isHTML(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'myEmail@gmail.com';
$mail->Password = 'myPwd';
return $mail->send();
} //end function sendemail
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
try {
sendemail(
'myEmail@address.com',
'First email subject',
'Form results to me...
<br><br>'.$message
);
sendemail(
$email,
'Second email subject',
'Confirmation email to person who submitted the form...
<br><br>'.$message
);
echo 'Email sent!';
} //end try
catch (phpmailerException $e) { //catches PHPMailer errors
echo 'There is a problem; the message did NOT send. Please go back and check that you have filled in all the required fields and there are no typos in your email address.';
echo $e->errorMessage();
}
catch (Exception $e) { //catches validation errors
echo 'There is a problem; the message did NOT send. Please either go back and try again or contact us at email@address.com';
echo $e->getMessage();
}
function validateEmpty($string, $name = 'name') {
$string = trim($string);
if ($string == '') {
throw new Exception(sprintf('%s is empty.', $name));
}
}
} //end if submit
?>
thank you for your time & expertise!
PS: yes, this is a duplication of another post that i think got so confusing people stopped helping on it... Form to Email PHP Validation fallback using PHPMailer
来源:https://stackoverflow.com/questions/49216555/phpmailer-fallback-validation