How to send Mail Using PHP

In this tutorial I will explain how to send mail using PHP mail function.The mail() function allows you to send emails directly from a script.
View demo

PHP Mail Function 
[table caption=”” width=”720″ colwidth=”200|520″ colalign=”center|left”] Parameters,Description
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)not more than 70 characters.
headers, Optional. Specifies additional headers like From Ccand Bcc.
parameters, Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting).
[/table] First create a html form with three fileds

  • Email
  • Subject
  • Message


<p style="color:#FF0000; font-size:12px;">
<?php echo $_SESSION['msg'];?><?php echo $_SESSION['msg']="";?></p>
<form name="mail" action="" method="post">
Email :<input type="email" name="email" value="" class="form-control" required />
Subject  :<input type="text" name="subject" value="Demo from PHP GURUKUL Programing Blog" class="form-control" readonly />
Message : <textarea name="message" rows="6" class="form-control" readonly>
This is sample text for testing.
From : PHP GURUKUL Programing Blog
<input type="submit" value="Send" name="send"  class="btn-primary" />

PHP CODE put this code top on the index.php page

 $fromAddr='PHP Gurukul Programing Blog <>'; // the address to show in From field.
 $recipientAddr = $_POST[email];
 $subjectStr = 'PHP Gurukul demo from Website';
$mailBodyText = <<<HHHHHHHHHHHHHH
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Demo From Website</title>
<td style="width:150px;"><b>Subject:</b></td>
<td style="text-transform:capitalize;">$_POST[subject]</td>
<td style="text-transform:capitalize;">$_POST[message]</td>
$headers= <<<TTTTTTTTTTTT
From: $fromAddr
MIME-Version: 1.0
Content-Type: text/html;
//echo "($recipientAddr,$subjectStr,$mailBodyText,$headers)";
$_SESSION['msg']="Your Information has been send sucessfully...";
View demo  
Download Source Code(Send mail using PHP Function)
Size: 26.6 KB
Version: V 1.0

Recommended Tutorials for you

Related posts

PHP – Data Types

Anuj Kumar

Useful functions for arrays in PHP

Anuj Kumar

How to Insert Data in Database using PHP OOPS Concept

Anuj Kumar

How to fetch data in excel or generate excel file in PHP

Anuj Kumar

How to get yesterday and tomorrow date in php

Anuj Kumar

PHP Echo / Print

Anuj Kumar

How to Concatenate two or more string in PHP

Anuj Kumar

PHP-Basic Development Concepts

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.