How to properly use AWS SES for a contact form?
Overall Goal: To have my contact form send me the details to my personal email
How do I want to achieve it?: Not using any plugins
Issue: When I use my custom PHPMailer function located in the functions.php, the console tells me that there is a
POST http://websitename.test/wp-admin/admin-ajax.php 500 (Internal Server Error) ...................... jquery.js?ver=1.12.4-wp:formatted:4206
I am using Laragon for my local server. Whenever I comment out my custom function that uses PHPMailer, Laragon catches the email. But whenever I add the custom function back in, it gives me with the error above.
Here is my custom PHPMailer function:
add_action('phpmailer_init', 'custom_mailer');
/**
* Overrides the default PHPMailer to use STMP with AWS SES
*
*/
function custom_mailer(PHPMailer $phpmailer)
{
$phpmailer-isSMTP();
$phpmailer-SMTPAuth = true;
$phpmailer-Username = 'USERNAME';
$phpmailer-Password = 'PASSWORD';
$phpmailer-Host = 'email-smtp.ca-central-1.amazonaws.com';
$phpmailer-SetFrom('[email protected]', 'FName LName');
$phpmailer-Port = 587;
$phpmailer-SMTPSecure = 'tls';
}
The Username, Password, and SetFrom are of course changed here to keep them secure. I assure you that the username and password are exactly the same from the credentials given in the AWS SES Console.
The jQuery in my contact form is:
(function($){
$('#enquiry').submit( function(event) {
event.preventDefault();
var endpoint = '?php echo admin_url('admin-ajax.php'); ?';
var form = $('#enquiry').serialize();
var formData = new FormData;
formData.append('action', 'enquiry');
formData.append('nonce', '?php echo wp_create_nonce('ajax-nonce'); ?');
formData.append('enquiry', form);
$.ajax(endpoint, {
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function() {
$('#enquiry').fadeOut(200);
$('#success_message').text('Thank you for your enquiry!').show();
$('#enquiry').trigger('reset');
},
error: function(error) {
alert(error.responseJSON);
$('#enquiry').trigger('reset');
}
});
});
})(jQuery)
And the PHP code that deals with the POST/AJAX is:
/**
* AJAX for Contact Us Form
*
* */
add_action('wp_ajax_enquiry', 'enquiry_form');
add_action('wp_ajax_nopriv_enquiry', 'enquiry_form');
/**
* Enquiry Form Function
*/
function enquiry_form()
{
if (!wp_verify_nonce($_POST['nonce'], 'ajax-nonce')){
wp_send_json_error('Nonce is incorrect', 401);
die();
}
$formdata = [];
wp_parse_str($_POST['enquiry'], $formdata);
// Admin Email
$admin_email = get_option('admin_email');
// Email Headers
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: websitename.com ' . $admin_email . '';
$headers[] = 'Reply-to:' . $formdata['email'];
// Who are we sending the email to?
$send_to = $admin_email;
// Subject
$subject = Enquiry from . $formdata['fname'] . . $formdata['lname'];
// Message
$message = '';
foreach ($formdata as $index = $field) {
$message .= 'strong' . $index . '/strong: ' . $field . 'br/';
}
try {
if (wp_mail($send_to, $subject, $message, $headers)) {
wp_send_json_success('Email Sent!');
} else {
wp_send_json_error('Email Error');
}
} catch (Exception $error) {
wp_send_json_error($error-getMessage());
}
wp_send_json_success($data);
}
Please Advise.