Skip to content

Instantly share code, notes, and snippets.

@michaelaguiar
Last active July 28, 2020 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save michaelaguiar/1271060 to your computer and use it in GitHub Desktop.
Save michaelaguiar/1271060 to your computer and use it in GitHub Desktop.
PHP - Send Email
<?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 />';
}
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