DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP Email From Form Script

08.19.2009
| 1915 views |
  • submit to reddit
        // description of your code here

<?php 
//$to = "stephen@ums-uk.com";
$to = "****@****.co.uk";
$subject = 'Someone has made contact via the website';
$headers = "From: ****@****.co.uk\r\nReply-To: ".$_POST['email'];

if(isset($_POST['email']) && $_POST['valid'] == '') {
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Someone has made contact via the website:

Name: <?php echo $_POST['name'];?>
Contact Number: <?php echo $_POST['phone'];?>
Email Address: <?php echo $_POST['email'];?>
Details:
<?php echo nl2br($_POST['message']);?>

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h4>Someone has made contact via the website:</h4>
<p>
<table>
<tr><td>Name:</td><td><?php echo $_POST['name'];?></td></tr>
<tr><td>Contact Number:</td><td><?php echo $_POST['phone'];?></td></tr>
<tr><td>Email Address:</td><td><?php echo $_POST['email'];?></td></tr>
<tr><td valign="top">Message:</td><td><?php echo nl2br($_POST['message']);?></td></tr>
</table>
</p>

--PHP-alt-<?php echo $random_hash; ?>--
<?

//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

$SentEmail = 'True';
}?>