How to Use Simple Mail in PHP

Introduction

I will describe simple mail In PHP. For sending mail in PHP, I am using the mail() function. This function accepts five parameters. I have designed a simple contact mail form and sent mail from localhost. PHP e-mail code is used here. This code uses configuration settings in the php.ini file. With PHP you can create contact forms on your website. The mail() function is part of the PHP core.

Syntax

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

 

Parameter Description
To  Indicates the Receiver.
Subject  Specific subject of the E-mail.
Message  Define the message to be send.
Headers  Additional headers like, From, Cc, and Bcc.

Example

<?php

if(isset($_POST["Submit"])){

$to = "[email protected]";

$subject = "Contact mail";

$from=$_POST["Email"];

$message=$_POST["Message"];

$headers = "$from"; mail($to,$subject,$message,$headers);

echo "Email successfully sent.";

}

?>

<html>

<head>

</head>

<body>

<form name="form" method="post" action="" >

 <table bgcolor=#ffffcc align=center>

 <tr><td colspan=2><strong>Contact us using this form:</strong></td></tr>

 <tr><td><font color=red>*</font> Name:</td><td><input size=25 name="Name"></td></tr>

 <tr><td><font color=red>*</font> Email:</td><td><input size=25 name="Email"></td></tr>

   <tr><td colspan=2>Message:</td></tr>

 <tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35 id="message"></textarea></td></tr>

 <tr><td colspan=2 align=center><input name="Submit" type="submit" id="Submit" value="Submit" ></td></tr>

 </table>

 </form>

</body>

</html>

This code is used for mail in PHP but you can use this code temporarily in our project and this script works in a live project. You cannot send mail from localhost otherwise this error is generated.

contect-form-in-php.jpg

contect-form2-in-php.jpg

contect-form1-in-php.jpg


Similar Articles