Reputation: 495
I have tried to use this php code for a web site contact us page. Error messages for field validation are working fine. However its not working the $result messages properly as its sending the wrong message.
<?php
if (isset($_POST["submit"]) && $_POST["submit"] == 'Send')
{
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$human = isset($_POST['human']) ? intval($_POST['human']) : '';
$error = array();
// Check if name has been entered
if (empty($name)) {
$error['name'] = 'Please enter your name';
}
// Check if email has been entered and is valid
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error['email'] = 'Please enter a valid email address';
}
//Check if message has been entered
if (empty($message)) {
$error['message'] = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$error['human'] = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (empty($error)) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'User');
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
// From here the code is not working properly
if ($mail->send()) {
$result = '<div class="alert alert-success">Sorry there is a problem by sending your message. Please try again later.</div>';
} else {
//echo 'Mailer Error: ' . $mail->ErrorInfo;exit;
$result = '<div class="alert alert-success">Thank you for your message and we will get back to you as soon as possible.</div>';
}
}
}
?>
Can you identify the error of this code?
Thanks in advance.
Upvotes: 0
Views: 8590
Reputation: 12391
you have added incorrect message.
on if ($mail->send()) {
you have returned error message.
change the check to !$mail->send()
that's it.
if (!$mail->send()) {
If that doesn't work, try this to debug:
if(!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message has been sent successfully";
}
Upvotes: 4
Reputation: 7294
Try to print the error
using ErrorInfo
function
if(!$mail->Send()) {// Send not send
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Upvotes: 0