• I have an email form in WordPress like this.

    html

    <form action="" id="contactForm">
            <input type="text" id="contactName" placeholder="Name:">
            <input type="text" id="contactEmail" placeholder="Email:">
            <select id="form-subject">
              <option>Subject</option>
              <option>Accounts</option>
              <option>Bookkeeping</option>
              <option>Start Up</option>
              <option>CIS Returns</option>
              <option>Payroll</option>
              <option>Self Assessment</option>
              <option>Tax Returns</option>
              <option>VAT Returns</option>
                            <option>Other</option>
            </select>
            <textarea id="contactMessage" rows="8" cols="40" placeholder="Message:"></textarea>
                        <p id="submit">Submit</p>
                        <div id="status">
              <p></p>
            </div>
        </form>

    jquery

    $j('#submit').click(function(){
            //
            var nameVal = $j('#contactForm #contactName').val();
            var emailVal = $j('#contactForm #contactEmail').val();
            var messageVal = $j('#contactForm #contactMessage').val();
    
            //
            $j.post('wp-content/code/contactEngine.php', { theName:nameVal, theEmail:emailVal, theMessage:messageVal }, function(data){
                $j("#status p").html(data);
                $j("#status p").show().fadeOut(3500);
                if(data.indexOf('Thank You')==0) {document.forms[0].reset();}
            });
        })

    php

    <?php
    
          $errors = array();
          $required_fields = array('theName','theEmail','theMessage');
          foreach($required_fields as $fieldname){
             if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){
                $errors[] = $fieldname;
             }
          }
          if(empty($errors)){
             $name_field = Trim(stripslashes($_POST['theName']));
             $name = explode(' ', $name_field);
             $firstname = ucfirst($name[0]);
             $email_field = Trim(stripslashes($_POST['theEmail']));
             $message = Trim(stripslashes($_POST['theMessage']));
             //
             $to = "info@ttmt.org.uk";
             $subject = "Email from Website";
             $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
             //
             mail($to, $subject, $body);
    
                 //wp_mail($to, $subject, $body);   
    
             echo "Thank You $firstname";
          }else{
             echo "Please complete all fields.";
          }
    
        ?>

    The php is in a folder in the wp-content folder so it’s easy for the jquery to access it.

    $j.post('wp-content/code/contactEngine.php', { theName:nameVal, theEmail:emailVal, theMessage:messageVal }, function(data){

    The variables reach the php and echo back a reply if the form is complete or not.

    The actual email isn’t sent – Does the php mail() function work in Worpdress.

    I tried the wp_mail() function but this stops the script completely and I don’t get the echo.

    I know there are email plugins like Contact Form 7 but I don’t think they will work with this site.

    It’s a single page site with a Custom Post Type, I’m not calling any content, which I think I ned to do with Contact Form 7.

  • The topic ‘Does php mail() function work in WordPress’ is closed to new replies.