All you need to know to send a mail using PHP Script is mail function. But I am considering you as a newbie here, a flow chart is plotted to show you the basic functionality of my script. This is very simple script. but you can make it more secure using sql injection.
According to flow chart, create two major files using PHP and HTML.
- email_form.php
- email_script.php
A form is created that provide a user interface to interact with the user for sending an email. Following is the code of Form. Save this code as “email_form.php” .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<body>
<form name="emailform" id="emailform" action="email_script.php" method="post" enctype="multipart/form-data">
<table width="360" border="0">
<tr>
<td width="57">From:</td>
<td width="287"><input type="text" name="from" id="from" /></td>
</tr>
<tr>
<td>To:</td>
<td><input type="text" name="to" id="to" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="subject" id="subject" /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="message" id="message" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnSend" id="btnSend" value="Send" /></td>
</tr>
</table>
</form>
</body>
|
When a form is ready, it should look like the following one. You can add more field into this using same method according to your need.
Finally, you need to write a PHP mail Script. I have used very simple method to make it more understandable. A simple mail() function is used to send a mail in PHP.
1 |
mail(to,subject,message,headers,parameters)
|
Save the following Script as “email_script.php”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// =====This Script For Sending an Email ====
// ===== Recieving Data From Form =====
$to = $_POST['to'];
$from = $_POST['from'];
$subject= $_POST['subject'];
$message= $_POST['message'];
//=======It is better to include Headers in your email to make it easy understandable for your recipient =====
$header = "From ".$from;
if(isset($_POST['btnSend']))
{
$res = mail($to,$subject,$message,$header);
if($res)
{
echo 'Message Sent to '.$to.'';
}
else
{
echo 'Correct your Errors';
}
}
?>
|
Make it more secure using capcha or SQL Injection to wrap the text.
Random Posts







