You have seen
html form whose send e-mails based on content entered in the form and now you want the same form on your web site. Ok, no problem.
Let's start with
html file which containing the form where user writes his message and send by pressing the [Send] button.
Please note that we have one input field named “send” with value parameter set to 1. This field we will use later when sending form to server to check that the form is truly sent.
Also in our example we using POST method for sending the data and for action of form we use the same file with form - $_SERVER['PHP_SELF']
PHP mail() function offer some parameters. Function will return true on success and false on fail. Parameter $headers is not obligate. The main format of mail() function is:
All of this we can put in one
PHP function for more functionality and clear code.
Variable
$to is used for specifying address on which we want to send e-mails.
Variable
$subject is used for specifying subject of e-mails.
Variable
$headers is used for specifying headers of e-mails.
[Not required]
<?php
function send_mail($from_name, $from_email, $message) {
// on which e-mail must send messages
$to = "contact@mysite.com";
// subject of the message
$subject = "Message from Mysite.com";
// headers for setting From: and Reply-To:
$headers .= "From: $from_name <$from_email>\n";
$headers .= "Reply-To: $from_name <$from_email>\n";
// try to send mail
$send_mail = mail($to, $subject, $message, $headers);
// check and return result
if ($send_mail) {
return true;
} else {
return false;
}
}
?>
What happening when form is sent to web server? Here we need to check that the $send variable is set to 1 and insert
PHP code which execute some e-mail function. And now let’s see how will look final code of our e-mail form
<?php
// our e-mail function
function send_mail($from_name, $from_email, $message) {
$to = "contact@mysite.com";
$subject = "Message from Mysite.com";
$headers .= "From: $from_name <$from_email>\n";
$headers .= "Reply-To: $from_name <$from_email>\n";
$send_mail = mail($to, $subject, $message, $headers);
if ($send_mail) {
return true;
} else {
return false;
}
}
// check that the form is truly sent
if ($_POST['send'] == 1) {
// get data from the form
$from_name=$_POST['from_name'];
$from_email=$_POST['from_email'];
$message=$_POST['message'];
// send e-mail
$try_mail = send_mail($from_name, $from_email, $message);
// output the result
if ($try_mail) {
echo "Your e-mail message was sent!";
} else {
echo "Your e-mail message was not sent!";
}
}
?>
<html>
<head>
<title>Sending e-mails with PHP mail() function</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="from_name">
<input type="text" name="from_email">
<textarea name="message"></text>
<input type="hidden" name="send" value="1">
<input type="submit" value="Send">
</form>
</body>
</html>
posted on 2009-Jul-12 | 11:28:09 PM
posted on 2009-Jul-20 | 10:47:47 PM
posted on 2010-Feb-19 | 10:28:39 AM
posted on 2010-Feb-19 | 12:25:21 PM
posted on 2010-Apr-16 | 10:03:58 AM
posted on 2010-Apr-17 | 02:28:48 AM