Articles: Processing a form => Sending it through email

nvest in a future Top model

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
Printer friendly version PDF File
Recommend this article to a friend.

Bookmark this on google Bookmark this on del.icio.us Submit this to digg Bookmark this at yahoo Bookmark this at reddit Bookmark this at furl Search technorati for links to this page 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]