In this tutorial you will learn about the PHP Sending Emails and its application with practical example.
The PHP mail() Function
The PHP mail() function is used to send emails from inside a script.
Syntax:
1 |
mail(to,subject,message,headers,parameters) |
to | The recipient’s email address.Multiple recipients can be specified. |
subject | The email’s subject line. |
message | The email body(Original Message). |
headers | Additional header fields such as “From”, “Cc”, “Bcc” etc. |
parameters | Any additional parameters. |
As soon as the mail function is called PHP will attempt to send the email then it will return true if successful or false if it is failed.
Mail() In Action
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 28 29 30 31 32 33 34 35 36 |
<?php if(isset($_POST["Submit"])){ $to = "askus@w3adda.com"; $subject = "Ask us mail"; $from=$_POST["email"]; $message=$_POST["message"]; $headers = "From: $from";mail($to,$subject,$message,$headers); echo "Email sent."; } ?> <form id="form1" name="form1" method="post" action="" > <table width="95%" border="0" align="center" cellpadding="5" cellspacing="1"> <tr class="cream"> <td colspan="2" align="left" valign="top"> <strong>Ask Us </strong> </td> </tr> <tr> <td align="right" valign="top">E-mail : </td> <td align="left" valign="top"><input name="email" type="text" id="email" ></td> </tr> <tr> <td align="right" valign="top">Message : </td> <td align="left" valign="top"> <textarea name="message" cols="40" rows="5" id="message"></textarea> </td> </tr> <tr> <td align="right" valign="top"> </td> <td align="left" valign="top"> <input name="Submit" type="submit" id="Submit" value="Submit" > </td> </tr> </table> </form> |