PHP - Send Email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Configuration | |
$myEmail = 'YOUR EMAIL HERE'; | |
$error = ''; | |
// To send HTML mail, the Content-type header must be set | |
$headers = 'MIME-Version: 1.0' . "\r\n"; | |
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; | |
// Additional headers | |
$headers .= 'To: NAME <email@example.com>, NAME <email@example.com>' . "\r\n"; | |
$headers .= 'From: NAME <email@example.com>' . "\r\n"; | |
$headers .= 'Cc: email@example.com' . "\r\n"; | |
$headers .= 'Bcc: email@example.com' . "\r\n"; | |
// Get form fields | |
$name = stripslashes($_POST['name']); | |
$phone = trim($_POST['phone']); | |
$email = trim($_POST['email']); | |
$comment = trim($_POST['comment']); | |
// Format Message | |
$subject = 'MESSAGE SUBJECT'; | |
$msg = ' | |
<strong>Name:</strong><br />'.$name.'<br /><br /> | |
<strong>Phone Number:</strong><br />'.$phone.'<br /><br /> | |
<strong>Email:</strong><br />'.$email.'<br /><br /> | |
<strong>Comment:</strong><br />'.$comment; | |
// Validation | |
if (empty($name)) { | |
$error .= 'Please enter your name.<br />'; | |
} | |
// ## FIX: eregi is deprecated in PHP 5.3 | |
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) { | |
$error .= 'Please enter a valid email address.<br />'; | |
} | |
// Send Message | |
if ($error !== '') { | |
echo $error; | |
} else { | |
mail($myEmail,$subject,$msg,$headers); | |
echo 'success'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment