Sending Email Using Php-Mail-Function

The mail() function allows you to send emails directly from a script.

Syntax:


mail(to,subject,message,headers,parameters);

to    =>  Required. Specifies the receiver / receivers of the email
subject =>  Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters
message  =>    Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters.

headers  =>  Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).
Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file.

parameters  =>  Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option)


See below example  :

<?php

   $to = "om@gmail.com";
  
 
   $subject = "This is subject";
  
 
   $message = "This is simple text message.";
  
 
   $header = "From:omnetworkom@gmail.com \r\n";
  
 
   $retval = mail ($to,$subject,$message,$header);
  
 
   if( $retval == true ) 
 

   {

      echo "Message sent successfully...";

   }

   else

   {

      echo "Message could not be sent...";

   }

?>


========================================================================
contact form code to send email using Mail() function  :

(1)write code for  contactform.html :

<h2>Send us your Complain<em><em></h2>
<h3>
<form action=sendmail.php  method=post>
Name:<input id="name" name="name" type="text"   placeholder="Enter your Name"  class="required"><br><br>


EMAIL:<input id="email" name="email" type="text"       placeholder="Enter your Email id"   class="required"><br><br>


PHONE:<input id="phone"    name="phone"     type="text"  placeholder="Enter your Phone"   class="required" ><br><br>


Message:<textarea  rows=5  cols=30  id="message"      class="required">Enter your Query & Message </textarea><br></br>

<input name="myBtn" type="submit" value="Submit Data"    > <br><br>

<div id="status"></div>
                    </form>
                  </div>


(2)write code for  sendmail.php  file:

<?php

$to="om@gmail.com";

$subject="Complain & Enquiry from users";

$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];

$msg="Name:$name/Email:$email/Phone:$phone/Message:$message";

$mail=mail($to,$subject,$msg);

if($mail)

{


echo "mail sent succssfully";

}
else
{
echo "mail not sent successfully";

}

?>

========================================================================
contact form code with Ajax to send email using Mail() function:

step 1: write code for  contactus.html    file : 

<!DOCTYPE html>
<html lang="en">
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "sendmail.php";
    var fn = document.getElementById("name").value;

    var en = document.getElementById("email").value;

var pn = document.getElementById("phone").value;

var pd = document.getElementById("message").value;



    var vars = "name="+fn+"&email="+en+"&phone="+pn+"&message="+pd;

    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
    var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
    }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>

 </head>

<body>

<h2>Send us your Complain<em><em></h2>
<h3>
Name:<input id="name" name="name" type="text"   placeholder="Enter your Name"  class="required"><br><br>


EMAIL:<input id="email" name="email" type="text"       placeholder="Enter your Email id"   class="required"><br><br>


PHONE:<input id="phone"    name="phone"     type="text"  placeholder="Enter your Phone"   class="required" ><br><br>


Message:<textarea  rows=5  cols=30  id="message"      class="required">Enter your Query & Message </textarea><br></br>

<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>

<div id="status"></div>
                    </form>
                  </div>
              
</body>
</html>

step 2:   write code for   sendmail.php  file  :

<?php

$to="om@gmail.com";

$subject="Complain & Enquiry from users";

$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];

$msg="Name:$name/Email:$email/Phone:$phone/Message:$message";

$mail=mail($to,$subject,$msg);

if($mail)

{


echo "mail sent succssfully";

}
else
{
echo "mail not sent successfully";

}


?>

Comments

Popular posts from this blog

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies

3 Best Applications that Organize your Chat and Messaging Accounts

Japan Style: Architecture Interiors Design