Send email with Ajax in WordPress

i an new to wordpress theme development, i need to send an email with ajax/PHP in wordpress

var dataString =    'name=' + name + 
        '&email=' + email + 
        '&contact=' + contact + 
        '&technology=' + technology +
        '&budget=' + budget +
        '&details=' + details;          

   alert(dataString);


   $.ajax({  
   type: "POST",  
   url: "<?php bloginfo('template_url'); ?>/qoute-sent.php",  
   data: {name:'anas', Email: 'anas@yahoo.com'},
   datatype: "html",  
   success: function() {  
   alert(sent);
   } 

all the code work till alert(dataString); but after that ajax does not works….

Read More

this is my php file code

    $name=$_POST['name'];
    $email=$_POST['email'];
   // $contact=$_POST['contact'];
   //$technology=$_POST['technology'];
  // $budget=$_POST['budget'];
  //$details=$_POST['details'];

   //-------------for email setup----------------------------
   $to = "stylegurupk@gmail.com";
   //------------------------------------------
   $message = " n " .

   "Name ".$name." n " .
   "Email : ".$email." n " ;

    //---------------------------------- 
    $subject = "MWM Qoute Request"; 
    $headers =  'From: '.$email . "rn" .
    'Reply-To: '.$to . "rn" .
    'X-Mailer: PHP/' . phpversion();
    //echo "TO : ".$to."<br>";
    //echo "FROM : ".$email."<br>";
    //echo "<br>".$message;
    mail($to, $subject,$message,$headers);

Related posts

Leave a Reply

2 comments

  1. WordPress provides functionality and hooks to develop with AJAX to make it really easy. See the Codex.

    You should also check out wp_mail for sending emails.

    Remember that WordPress loads jQuery in noconflict mode, so your $ selector’s should be ‘jQuery’.

    To prevent abuse you should consider adding a nonce to your script. WordPress handles that too 🙂 See wp_create_nonce.

    I think this is a good reference article once you have the bones in places

  2. url: "<?php bloginfo('template_url'); ?>/qoute-sent.php", 
    

    Should the URL be “quote-sent.php”? Just checking.

    Also, have you set up the AJAX actions for WP? It won’t work without them. I’d generally do something like:

    <?php
        function my_send_email(){
            doStuff(); // etc..
            echo 'sent!';
            die();
        }
        add_action('wp_ajax_my_send_email', 'my_send_email');
        add_action('wp_ajax_nopriv_my_send_email', 'my_send_email');
    ?>
    

    Like Mark said, I’d also recommend using wp_mail.