PHPGurukul

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 
mail(to,subject,message,headers,parameters);

ParametersDescription
toRequired. Specifies the receiver / receivers of the email
subjectRequired. Specifies the subject of the email. Note: This parameter cannot contain any newline characters
messageRequired. Defines the message to be sent. Each line should be separated with a LF (n)not more than 70 characters
headersOptional. Specifies additional headers like From Cc and Bcc
parametersOptional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting

First, create a HTML form with three fields

  • Email
  • Subject
  • Message

Index.php

<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
</textarea>
<input type="submit" value="Send" name="send"  class="btn-primary" />
</form>

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

<?php
if($_POST['send'])
{
	$email=$_POST[email];
 $fromAddr='PHP Gurukul Programing Blog <www.phpgurukul.com>'; // 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Demo From Website</title>
</head>
<body>
<table>
<tr>
<td style="width:150px;"><b>Subject:</b></td>
<td><b>:</b></td>
<td style="text-transform:capitalize;">$_POST[subject]</td>
</tr>
<tr>
<td><b>Message</b></td>
<td><b>:</b></td>
<td style="text-transform:capitalize;">$_POST[message]</td>
</tr>
</table>
</body>
</html>
HHHHHHHHHHHHHH;
$headers= <<<TTTTTTTTTTTT
From: $fromAddr
MIME-Version: 1.0
Content-Type: text/html;
TTTTTTTTTTTT;
mail($recipientAddr,$subjectStr,$mailBodyText,$headers);
//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

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


Add comment

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

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.