Skip to content

Instantly share code, notes, and snippets.

@marcofugaro
Last active February 1, 2024 08:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcofugaro/a9aaecc4e188e6acfeef to your computer and use it in GitHub Desktop.
Save marcofugaro/a9aaecc4e188e6acfeef to your computer and use it in GitHub Desktop.
simple php mail snippet with PHPMailer through SMTP and Google reCAPTCHA
var form = $('#form');
form.find('[type="submit"]').on('click', function(e) {
var isFormValid = form[0].checkValidity();
var isCaptchaOk = $('#g-recaptcha-response').val();
if(isFormValid && isCaptchaOk) {
e.preventDefault();
var dataObj = {
name: $.trim(form.find('input[name="name"]').val()),
email: $.trim(form.find('input[name="email"]').val()),
message: $.trim(form.find('textarea').val()),
captcha : grecaptcha.getResponse()
};
$(this).addClass('disabled');
form.find('.error-captcha').remove();
ajaxSend(dataObj);
} else if(!isCaptchaOk) {
if(isFormValid) e.preventDefault();
if(form.find('.error-captcha').length == 0) {
form.find('.g-recaptcha').parent().append('<span class="error-captcha">Please fill up the captcha.</span>');
}
} else if(isCaptchaOk) {
if(isFormValid) e.preventDefault();
form.find('.error-captcha').remove();
}
});
var ajaxSend = function(dataObj) {
$.ajax({
type: 'POST',
url: 'send.php',
data: dataObj,
statusCode: {
200: function () {
toastr['success']('Message sent successfully.'); //I use toastr https://github.com/CodeSeven/toastr
form.find('input:not([type="submit"]), textarea').val('');
form.find('input:checkbox').attr('checked', false);
},
400: function() { toastr['error']('400 Client Error.'); },
403: function() { toastr['error']('403 Bad Request.'); },
404: function() { toastr['error']('404 Not Found.'); },
500: function() { toastr['error']('500 Server Error.'); }
},
complete: function() {
form.find('[type="submit"]').removeClass('disabled');
}
});
};
<?php
require 'PHPMailer/PHPMailerAutoload.php';
if ($_SERVER["REQUEST_METHOD"] != "POST") {
http_response_code(403); // header(':', true, 403); if php version < 5.4
echo "403 Forbidden";
exit;
}
if (!isset($_POST["email"]) || !isset($_POST["message"])) {
http_response_code(400); // header(':', true, 400); if php version < 5.4
echo "400 Client Error";
exit;
}
if(!isset($_POST["captcha"]) || !($captcha=$_POST["captcha"])) {
http_response_code(400); // header(':', true, 400); if php version < 5.4
echo "400 Bad Captcha";
exit;
}
$secret = "YOUR SECRET";
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]), true);
if(!isset($response["success"]) || !$response["success"]) {
http_response_code(400); // header(':', true, 400); if php version < 5.4
echo "400 Client Error";
exit;
}
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp1.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your.access.email@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('your.email@gmail.com', 'Contact Form Site.com');
$mail->addReplyTo($_POST["email"], $_POST['name']);
$mail->addAddress('your.email@gmail.com');
$mail->isHTML(true);
$mail->Subject = 'Contact form message from the website';
$mail->Body = 'Name: ' . $_POST['name'] . '<br /><br />' . 'Message:' . '<br />' . $_POST['message'];
$mail->AltBody = 'Name: ' . $_POST['name'] . 'Message: ' . $_POST['message'];
if(!$mail->send()) {
header(':', true, 500);
echo '500 Server Error: ' . $mail->ErrorInfo;
exit;
}
header(':', true, 200);
echo 'Message sent successfully';
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment