Skip to content

Instantly share code, notes, and snippets.

@srokatonie
Last active March 14, 2018 11:29
Show Gist options
  • Save srokatonie/2ad28ed52b7267a9ec348ef3ff56b7ed to your computer and use it in GitHub Desktop.
Save srokatonie/2ad28ed52b7267a9ec348ef3ff56b7ed to your computer and use it in GitHub Desktop.
PHP Mail function
<script>
function submitForm() {
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "php/contact.php",
data: "name=" + name + "&email=" + email + "&message=" + message,
success: function(text) {
if (text == "success") {
// show success on front end
console.log("Form success");
} else {
// show error on front end
console.log("Form error");
}
}
});
}
</script>
<?php
$to = 'your@email.com';
$subject = 'Contact form';
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name\r\nReply-to: $email";
if (@mail($to, $subject, $message, $headers)) {
echo "success";
} else {
echo "Something went wrong. Please try again later.";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment