After switching to production hosting environment, had to temporarily implement SMTP on sendemail.php. Implemented per documentation and email was received as intended, but I'm no longer getting the front-end success message as I was on our staging server without the SMTP setup. On the front-end, the form gets stuck with the spinning animation.
sendemail.php
'firstrecipient@ourdomain.com', // Your Email Address
'name' => 'First Recipient' // Your Name
);
$toemails[] = array(
'email' => 'secondrecipient@anotherdomain.com', // Your Email Address
'name' => 'Second Recipient' // Your Name
);
// Form Processing Messages (THIS IS NOT GETTING PASSED CORRECTLY)
$message_success = 'We have **successfully** received your Message and will get Back to you as soon as possible.';
// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret
$mail = new PHPMailer();
// If you intend you use SMTP, add your SMTP Code after this Line
$mail->IsSMTP();
$mail->Host = "mail.ourdomain.com"; //Confirmed correct
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Port = 25; //Confirmed correct
$mail->Username = "username@ourdomain.com";//confirmed correct
$mail->Password = "OuRpAsSwOrD";//confirmed correct (none of the comments in this section are in our actual code)
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if( $_POST['template-contactform-email'] != '' ) {
$name = isset( $_POST['template-contactform-name'] ) ? $_POST['template-contactform-name'] : '';
$email = isset( $_POST['template-contactform-email'] ) ? $_POST['template-contactform-email'] : '';
$phone = isset( $_POST['template-contactform-phone'] ) ? $_POST['template-contactform-phone'] : '';
$company = isset( $_POST['template-contactform-company'] ) ? $_POST['template-contactform-company'] : '';
$subject = isset( $_POST['template-contactform-subject'] ) ? $_POST['template-contactform-subject'] : '';
$message = isset( $_POST['template-contactform-message'] ) ? $_POST['template-contactform-message'] : '';
$subject = isset($subject) ? $subject : 'New Message From Contact Form';
$botcheck = $_POST['template-contactform-botcheck'];
if( $botcheck == '' ) {
$mail->SetFrom( $email , $name );
$mail->AddReplyTo( $email , $name );
foreach( $toemails as $toemail ) {
$mail->AddAddress( $toemail['email'] , $toemail['name'] );
}
$mail->Subject = $subject;
$name = isset($name) ? "Name: $name
" : '';
$email = isset($email) ? "Email: $email
" : '';
$phone = isset($phone) ? "Phone: $phone
" : '';
$company = isset($company) ? "Firm or Company Name: $company
" : '';
$message = isset($message) ? "Message: \n$message
" : '';
$referrer = $_SERVER['HTTP_REFERER'] ? '
This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';
$body = "$name $email $phone $company $message $referrer";
// Runs only when File Field is present in the Contact Form
if ( isset( $_FILES['template-contactform-file'] ) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK ) {
$mail->IsHTML(true);
$mail->AddAttachment( $_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name'] );
}
// Runs only when reCaptcha is present in the Contact Form
if( isset( $_POST['g-recaptcha-response'] ) ) {
$recaptcha_response = $_POST['g-recaptcha-response'];
$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );
$g_response = json_decode( $response );
if ( $g_response->success !== true ) {
echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
die;
}
}
// Uncomment the following Lines of Code if you want to Force reCaptcha Validation
// if( !isset( $_POST['g-recaptcha-response'] ) ) {
// echo '{ "alert": "error", "message": "Captcha not Submitted! Please Try Again." }';
// die;
// }
$mail->MsgHTML( $body );
$sendEmail = $mail->Send();
if( $sendEmail == true ):
echo '{ "alert": "success", "message": "' . $message_success . '" }';
else:
echo '{ "alert": "error", "message": "Email **could not** be sent due to some Unexpected Error. Please Try Again later.
**Reason:**
' . $mail->ErrorInfo . '" }';
endif;
} else {
echo '{ "alert": "error", "message": "Bot **Detected**.! Clean yourself Botster.!" }';
}
} else {
echo '{ "alert": "error", "message": "Please **Fill up** all the Fields and Try Again." }';
}
} else {
echo '{ "alert": "error", "message": "An **unexpected error** occured. Please Try Again later." }';
}
?>The form:
Send Message
