0

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.

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
    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
    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
    Nov 13, 2020 at 2:32

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.