PHP provides a convenient way to send email with the mail() function.
Syntax of mail function:
mail(to,subject,message,headers,parameters)
- to -- Required. Specifies the recipient's email address or addresses.
- subject -- Required. Specifies the email's subject line.
- message -- Required. Specifies the actual email body . Each line should be separated with \n.
- headers -- Optional. Specifies additional headers such as "From", "Cc", "Bcc", etc. The additional headers should be separated with (\r\n).
- parameters -- Specifies any additional parameters.It is optional.
Example of PHP Mail function
<h3>Contact Form</h3>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} else { // the user has submitted the form
// Check if the "from" input field is filled out
if (isset($_POST["from"])) {
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// send mail
mail("kapil@example.com",$subject,$message,"From: $from\n");
echo "Thank you for contacting us.";
}
}
?>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} else { // the user has submitted the form
// Check if the "from" input field is filled out
if (isset($_POST["from"])) {
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// send mail
mail("kapil@example.com",$subject,$message,"From: $from\n");
echo "Thank you for contacting us.";
}
}
?>
No comments:
Post a Comment