I'm currently working on a WordPress contact form, which is created as a plugin. I need the form after submit to send an email without reloading the page. When it reloads everything is ok. The problem is when submit button is pressed and e.preventDefault() is active the browser shows me 500 (Internal Server Error) with admin-ajax.php = 0

P.S The post is edited

    add_action( 'admin_footer', 'html_form_code' ); // Write our JS below here
    function html_form_code() {
    <form action="<?php esc_url( $_SERVER['REQUEST_URI'] )?>" method="post" class="contact-form" id="contact-form" >
        <div class=header-contact>
            <p><h2>Contact Form</h2></p>
        <div class=input-containers>
            <input type="text" id="name" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="" size="40" placeholder="Име и фамилия"/>
        <div class=input-containers>
            <input type="email" id="email" name="cf-email" value="" size="40" placeholder="Поща"/>
        <div class=input-containers>
            <input type="text" id="subject" name="cf-subject" pattern="[a-zA-Z ]+" value="" size="40" placeholder="Относно"/>
        <div class=input-containers>
            <textarea rows="10" id="message" cols="35" name="cf-message" placeholder="Текст"></textarea>
        <div class=input-containers>
            <input type="submit" name="cf-submitted" value="Send" id="submitForm">
        <p id="verify" style="display:none;">Your message has been sent.<br /><br /></p> 

     var name =  jQuery('#name').val();
     var email =  jQuery('#email').val();
     var subject =  jQuery('#subject').val();
     var message =  jQuery('#message').val();
               url: '<?php echo admin_url('admin-ajax.php'); ?>',
               type: "POST",
                  action: 'send_email', 
                  name: name,
                  email: email,
                  subject: subject,
                  message: message,
        alert("Email Sent.");
    add_action( 'wp_ajax_send_email', 'deliver_mail' );
    add_action( 'wp_ajax_nopriv_send_email', 'deliver_mail' );

    function deliver_mail() {
        require_once "wp-includes/class-phpmailer.php";
        if (isset($_POST["cf-submitted"])) {

            // sanitize form values
            $name    = sanitize_text_field( $_POST["cf-name"] );
            $email   = sanitize_email( $_POST["cf-email"] );
            $subject = sanitize_text_field( $_POST["cf-subject"] );
            $message = esc_textarea( $_POST["cf-message"] );

            // get the blog administrator's email address

            //$to = "email@gmx.com";
            $headers = "From: $name <$email>" . "\r\n";

            // Localhost
            $mail = new PHPMailer(true);
            $mail->IsSMTP(); // telling the class to use SMTP
            $mail->CharSet = 'UTF-8';

            $mail->SMTPDebug = 0;                     // enables SMTP debug information (for testing)
            $mail->SMTPAuth = true;                  // enable SMTP authentication
            $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
            $mail->Host = "mail.gmx.com";      // sets GMX as the SMTP server for example: mail.gmx.com
            $mail->Port = 465;                 // set the SMTP port for the GMX server

            $mail->Username = $email;
            $mail->Password = 'pass';

            $mail->SetFrom($email, $name);

            $mail->Subject = $subject;

            $headers .= "Content-Type: text/html; charset=utf-8";
            $headers .= "Content-Transfer-Encoding: 8bit";

            try {
                $msg = "An email has been sent for verfication.";
                $msgType = "success";

                wp_safe_redirect( home_url(), 302 );

            } catch (Exception $ex) {
                $msg = $ex->getMessage();
                $msgType = "warning"; 

                wp_safe_redirect( home_url(), 302 );

    add_action( 'init', function() {
        if ( ! empty( $_POST['form_submitted'] ) ) {

    function cf_shortcode() {

        return ob_get_clean();
    add_shortcode( 'contact_form_second', 'cf_shortcode' );
  • 1
    First you forgot the action parameter in your ajax call. Second the url in ajax call is not correct. Why do you call "deliver_mail()" in the init hook? WP Codex: AJAX in Plugins – user141080 Jul 21 '18 at 13:12
  • I downvoted because there was no effort to debug the code idownvotedbecau.se/nodebugging. edit your question to add more informations. – mmm Jul 21 '18 at 13:20
  • I copied the full plugin code, this is all the information which is possible... – J.D. Jul 21 '18 at 13:25
  • It is in the init hook because it prevents from "Cannot modify header information"... – J.D. Jul 21 '18 at 13:28
  • Okay....but if you send your form via ajax you do not need that. – user141080 Jul 21 '18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.