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
  • A http 500 error is just a super generic something went wrong message, did you look in your PHP error log to discover what the actual error message was? Also I see you use the $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
  • I'm currently working on the debugging area and fixing what's giving me an issue. I wanted to see if anyone had any similar problems to mine. I've looked towards similar SMPT issues and they didn't help much, so I thought I would shoot my shot and see if someone already had the same issue. – Steve Commented Nov 13, 2020 at 2:30
  • To add on the debugging log, it tells me the argument to my function, custom_mailer(), must be an instance of PHPMailer, instance of PHPMailer\PHPMailer\PHPMailer given. Working on that issue – Steve Commented Nov 13, 2020 at 2:32
Add a comment  | 

1 Answer 1

Reset to default 0

The 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