PHP 5 mail () function
How mail function works
PHP can automate the sending of Internet mail easily and simply using mail() function, It's allow to send directly.
Its simplest method to send a text email, mail function is generally used in a feedback and contact us form on the website.
Syntax of mail() function
mail ( $to , $subject , $message , $headers , $parameters ) ;
Parameter of mail function | |
---|---|
Parameter |
Description |
To | *Specify the receiver, or receivers email address. |
Subject | *Subject of the E-mail. |
Message | *Message to be sent each line should be separated with a CRLF (\r\n) . |
Header | It is used to add extra headers (From, Cc, and Bcc). |
Parameters | It is used to add extra parameter. |
*:- All are required parameter.
You can directly pass all the required argument and send the mail as shown below.
Mail function example
<?php mail( "xyz@test.com", "wishing you", "Hi xyz, Good morning?" ); ?>
Basic example of mail () function
<?php $to = "xyz@domain.com"; $sub = "My subject"; $txt = "Hello world!"; mail($to,$sub,$txt); ?>
Save anyone of the above script with any name, when it's run message will send automatically to the given email ID.
Contact form with mail function
Create a feedback form to send the feedback of the visitors to the admin with the help of mail function.
Example of mail function
<html> <head> </head> <?php if (!isset($_POST["sub"])) { ?> <form method="post" action=" < ?php echo $_SERVER['PHP_SELF']; ?> "> <table> <tr> <td>Name</td> <td><input name="name" type="text" required/></td> </tr> <tr> <td>Email</td> <td><input name="email" type="email" required/></td> </tr> <tr> <td>Messege</td> <td><textarea name="mess" cols="15" rows="10" required ></textarea></td> </tr> <tr> <td><input name="res" type="reset" value="Reset"></td> <td><input name="sub" type="submit" value="Submit" ></td> </tr> </table> </form> <?php } else { if (isset($_POST["name"])) { $from = $_POST["name"]; // sender $subject = $_POST["email"]; $message = $_POST["mess"]; mail("ptutorial3@gmail.com",$subject,$message,"From: $from\n"); echo "Thanks"; } }
- Example
- Run
In the above example, create simple HTML forms and processing PHP script to access this information to store on variables and use these variables in the mail function to send specified email ID.