Skip to content

Instantly share code, notes, and snippets.

@tohansg
Created March 18, 2014 21:50
Show Gist options
  • Save tohansg/9630498 to your computer and use it in GitHub Desktop.
Save tohansg/9630498 to your computer and use it in GitHub Desktop.
PHP mail() function
<?php
if(isset($_POST["email"]) && !empty($_POST["email"])){
//if(!$_POST) exit;
$email = $_POST['email'];
$name = $_POST['name'];
$comments = $_POST['comments'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error .= "Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
//send the mail
/*$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "hello@gurinderhans.me";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
*/
$to = "hello@gurinderhans.me";
$from = $email;
$subject = 'Hey there!';
$message = "".$message."";
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
if(mail($to, $subject, $message, $headers)){
echo "Message successfully sent";
} else{
echo "Your message wasn't sent. Please try again later.";
}
/*if(mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}*/
}
}
?>
@tohansg
Copy link
Author

tohansg commented Mar 18, 2014

Try using this for mail()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment