admin管理员组文章数量:1316204
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 /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';
$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 <' . $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.
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';
$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 <' . $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.
Share Improve this question asked Nov 13, 2020 at 0:47 SteveSteve 351 gold badge1 silver badge4 bronze badges 3 |1 Answer
Reset to default 0The main issue wasn't with any of the server or the backend, the issue was with the function itself, mainly the parameter PHPMailer.
For some odd reason, you need to use
PHPMailer/PHPMailer/PHPMailer
or
use PHPMailer/PHPMailer/PHPMailer
at the top of the functions.php file.
本文标签: theme developmentHow to properly use AWS SES for a contact form
版权声明:本文标题:theme development - How to properly use AWS SES for a contact form? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741998290a2410491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$headers
variable, but it just gets used out of nowhere, where does it come from? You can't just append to an array without creating it first – Tom J Nowell ♦ Commented Nov 13, 2020 at 2:19