PHP Tutorial - Sending Email
This section builds on the forms section
by adding new functionality. This time, we will not only collect data,
we'll emailed it. The PHP mail() function handles this.
The Form
<form action="hello.php" method="POST">
Your first name <input type="text" name="name">
Your favorite color <input type="text" name="color">
Your email address <input type="text" name="email">
<input type="submit" value="Send Info">
</form>
|
|
The above form allows people to enter their name, favorite color
and email address. Once they click the button, that data is sent
to hello.php for processing.
hello.php
<?php
$senderemail = $_POST['email'];
$sendername = $_POST['name'];
$sendercolor = $_POST['color'];
$recipient = "test@example.com";
$subject = "Your subject line goes here";
$mailheader = "From: $senderemail\n";
$mailheader .= "Reply-To: $senderemail\n\n";
$message = "Sender's name: $sendername\n";
$message .= "Sender's favorite color: $sendercolor\n\n";
mail($recipient, $subject, $message, $mailheader) or die ("Failure");
?>
|
|
The above PHP generates the email, sending it to test@example.com
with "Your subject line goes here" in the subject line.
The From & Reply-to are set to the senders address.
The actual email contains something like the below.
Sender's name: Barney
Sender's favorite color: purple
|
|
You would replace the $recipient value with your own email address.
PHP support is enabled on all web site hosting plans.