PHP Scripts – WordPress Tutorials

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.

  1. email_form.php
  2. 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';
}

}
?&gt;

Make it more secure using capcha or SQL Injection to wrap the text.

Random Posts


Categories: PHP, Web Development
  • Madiha Akram

    nyc ……

  • http://www.goedkoopepc.com fluppen

    Hoi, I am a newbie indeed. Can you tell me exactly where in the script I have to write my emailadress

    • http://www.computersneaker.com/ Mudasir Nazir

      right after the email variable. Change where i have provided $email=”abc@gmail.com”;
      You need to change that section and you will be up.

      • Matt

        I don’t see anything that says $email? I think I’m missing something..

  • Rushit

    W3SCHOOLS…………………
    it hav the same solutions……

  • janey

    Hello,
    Could you please write about how to use smtp mail server to send emails? (not from localhost)Thanks

  • Guest

    Hi, nice tutorial thanks. Have can I send and email to the visitor who filled out the form and one to myself telling me someone has filled out the form..

    Thanks

  • Alan Storry

    Hi, nice tutorial thanks. How can I send an email to the visitor who filled out the form and one to myself telling me someone has filled out the form..