Skip to content

Instantly share code, notes, and snippets.

@jonmunson
Created August 31, 2014 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonmunson/fc8ff9bd4472da7852de to your computer and use it in GitHub Desktop.
Save jonmunson/fc8ff9bd4472da7852de to your computer and use it in GitHub Desktop.
Send email from server
<?php
// Use this when posting a form via ajax
// - this file should be above the webroot
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$messsage = ($_GET['message']) ?$_GET['message'] : $_POST['message'];
//define the receiver of the email
$to = 'admin@domain.com';
//define the subject of the email
$subject = 'Contact form from domain.com';
//define the message to be sent. Each line should be separated with \n
$message = '
Name: ' . $name . '
Email: ' . $email . '
Message: ' . $messsage . '
';
$headers = 'From: webmaster@domain.com' . "\r\n" .
'Reply-To: webmaster@domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment