Over a million developers have joined DZone.

PHP: Email All Form Values

DZone's Guide to

PHP: Email All Form Values

Free Resource
If you need a very basic contact form and don't want to write extra code for putting the value of each field into the email, you can use this basic email. It works best if you name your form fields something legible, such as First_Name

if (isset($_POST['Submit'])) {
	// Prepare message
	$msg = "Time: " . date("m/d/y g:ia", time()) . "\n";
	foreach ($_POST as $field=>$value) {
		if ($field != "submit") $msg .= $field . ": " . $value . "\n";
	if (mail("TOEMAIL", "SUBJECT", $msg, "From: FROM_NAME 
   ")) {
		// Email was sent
	} else {
		// Erro sending email


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}