Last active
July 28, 2020 20:54
-
-
Save michaelaguiar/1271060 to your computer and use it in GitHub Desktop.
PHP - Send Email
This file contains hidden or 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 protected]>, NAME <[email protected]>' . "\r\n"; | |
$headers .= 'From: NAME <[email protected]>' . "\r\n"; | |
$headers .= 'Cc: [email protected]' . "\r\n"; | |
$headers .= 'Bcc: [email protected]' . "\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 />'; | |
} | |
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
$error .= 'Please enter a valid email address.'; | |
} | |
// 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