Articles: Processing a form => Sending it through email
A lot of users have requested a simple introduction to using PHP to send an email containing the content of a form. A simple multi-usable approach is therefore presented as following:
Added: 2004-05-25 22:04:15 - Modified: 2004-05-25 20:08:42 - Level: Beginner
![]()
Recommend this article to a friend.
Toggle more
As the processing-script uses $_POST it is important to specify a method in the form tag
<form action="/myfile.php" method="post">
The actual .php file will have content as following
<?
$email_adresse = "user@host";
$subject = "This is the subject";
$body = "";
foreach($_POST as $a => $b)
{
$body .= "$a: $b\n";
}
if(mail($email_adresse, $subject, $body)){echo "Mail sent successfully";} else {echo "An error occured";}
?>
This initiate a foreach [ http://www.php.net/foreach ] loop that will loop through the $_POST array and append the data to the $body variable. Therefore this script can be used for many purposes, as you can use several forms and the same script. Happy coding
Related articles:
A simple Form processor (Perl)
[Sitemap]

