How to send e-mail from form in wordpress

50,640

Solution 1

To send mail from a form with following fields, open a new file and copy these lines and save (e.g. mail.php):

<?php
 //sending mail
 if(isset($_POST['sub']))
 {
   $uname=$_POST['uname']; 
   $mailid=$_POST['mailid'];
   $phone=$_POST['phone'];
   $message=$_POST['message'];
   if(mail($uname,$mailid,$phone,$message))
   {
     echo "mail sent";
   }
   else
   {
     echo "mail failed";
   }
 }
?>
<form name="frm" method="post" action="#">
 <label for="uname">Name:</label>
 <input type="text" value="" name="uname" id="uname"><br/>
 <label for="mailid">Email:</label>
 <input type="text" value="" name="mailid" id="mailid"><br/>
 <label for="mobile">Phone:</label>
 <input type="text" value="" name="phone" id="phone"><br/>
 <label for="message">Message:</label>                                   
 <input type="text" value="" name="message" id="message"><br/>
 <input type="submit" value="Submit" name="sub" id="sub">
</form>

Solution 2

It will have to be much more complicated than what you have at the moment.

My first recommendation would be to use some, rather top-heavy, form plugins, But you will save yourself a lot of time and trouble like that.

http://www.deliciousdays.com/cforms-plugin/

http://wordpress.org/extend/plugins/contact-form-7

My 2nd recommendation would be to follow a popular tutorial for a full custom form to email script.

http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme

Note: Beware of spam bots and various other possible dangers of just putting a public form up on the web. If's very possible that you will have a hole in your custom code and a could cause some damage if exploited by the right person.

Share:
50,640
saad iqbal
Author by

saad iqbal

Updated on September 02, 2021

Comments

  • saad iqbal
    saad iqbal almost 3 years

    I am new in programing and WordPress. I want to send a e-mail from my form with a message, phone, ect. Can anyone tell me how can i do that? Here is my code so far...

     <div id="form">
    
    
     <?php 
              add_filter('wp_mail_content_type','set_content_type');
              function set_content_type($content_type){
              return 'text/html';
              }    
    
        ?>
             <div id="name">
             <p id="username"> Name:&nbsp; </p>
             <input type="text" name="name" class="textfield">
             </div>
             <div id="name">
             <p id="username"> Email: &nbsp; </p>
             <input type="text" name="name" class="textfield">
             </div>
             <div id="name">
             <p id="username"> Phone:&nbsp;  </p>
             <input type="text" name="name" class="textfield">
             </div>
             <div id="name">
             <p id="username"> Message: </p>
             <input type="text" name="message" class="textarea">
         </div>
        <input type="button" value="SEND" id="btn"> 
      </div>
    


    • saad iqbal
      saad iqbal about 12 years
      i use this function for sending mail but not working <?php add_filter('wp_mail_content_type','set_content_type'); function set_content_type($content_type){ return 'text/html'; }
    • Francisco Corrales Morales
      Francisco Corrales Morales about 10 years
  • Francisco Corrales Morales
    Francisco Corrales Morales about 10 years
    how can I make this a function ?, like so I can call it from jQuery, it sends an email, then return a message with success/fail. ?
  • Francisco Corrales Morales
    Francisco Corrales Morales about 10 years
    can I send emails from my local computer ?
  • Francisco Corrales Morales
    Francisco Corrales Morales about 10 years
    can I send emails from my local computer ?
  • Foxinni
    Foxinni about 10 years
    @FranciscoCorrales Yes you can, but you need to setup Postfix if you are on MAMP. foxinni.com/screens/Postfix-20140512-093049.png See: blog-en.mamp.info/2009/09/…
  • Luca Reghellin
    Luca Reghellin almost 8 years
    Isn't the mail function wrongly called twice on the code above?