-
-
Save jonmbake/0e5b175a72ad9ba64167 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Sets error header and json error message response. | |
* | |
* @param String $messsage error message of response | |
* @return void | |
*/ | |
function errorResponse ($messsage) { | |
header('HTTP/1.1 500 Internal Server Error'); | |
die(json_encode(array('message' => $messsage))); | |
} | |
/** | |
* Pulls posted values for all fields in $fields_req array. | |
* If a required field does not have a value, an error response is given. | |
*/ | |
function constructMessageBody () { | |
$fields_req = array("name" => true, "email" => true, "message" => true); | |
$message_body = ""; | |
foreach ($fields_req as $name => $required) { | |
$postedValue = $_POST[$name]; | |
if ($required && empty($postedValue)) { | |
errorResponse("$name is empty."); | |
} else { | |
$message_body .= ucfirst($name) . ": " . $postedValue . "\n"; | |
} | |
} | |
return $message_body; | |
} | |
header('Content-type: application/json'); | |
//do Captcha check, make sure the submitter is not a robot:)... | |
$url = 'https://www.google.com/recaptcha/api/siteverify'; | |
$opts = array('http' => | |
array( | |
'method' => 'POST', | |
'header' => 'Content-type: application/x-www-form-urlencoded', | |
'content' => http_build_query(array('secret' => '28dskgjs82skdgjshsdf', 'response' => $_POST["g-recaptcha-response"])) | |
) | |
); | |
$context = stream_context_create($opts); | |
$result = json_decode(file_get_contents($url, false, $context, -1, 40000)); | |
if (!$result->success) { | |
errorResponse('reCAPTCHA checked failed!'); | |
} | |
//attempt to send email | |
$messageBody = constructMessageBody(); | |
require './vender/php_mailer/PHPMailerAutoload.php'; | |
$mail = new PHPMailer; | |
$mail->CharSet = 'UTF-8'; | |
$mail->isSMTP(); | |
$mail->Host = "smtp.gmail.com"; | |
$mail->SMTPAuth = true; | |
$mail->Username = "me@gmail.com"; | |
$mail->Password = "my!password"; | |
$mail->SMTPSecure = 'tls'; | |
$mail->Port = 587; | |
$mail->setFrom($_POST['email'], $_POST['name']); | |
$mail->addAddress("me@gmail.com"); | |
$mail->Subject = $_POST['reason']; | |
$mail->Body = $messageBody; | |
//try to send the message | |
if($mail->send()) { | |
echo json_encode(array('message' => 'Your message was successfully submitted.')); | |
} else { | |
errorResponse('An expected error occured while attempting to send the email: ' . $mail->ErrorInfo); | |
} | |
?> |
@DanielWebDev
Try this in sendmail.php and comment out the following:
$mail->Host = localhost;
/*
$mail->isSMTP();
$mail->Host = "xxx";
$mail->SMTPAuth = true;
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
*/
Is it safe to add my password from my mail, actually why needed, I saw other contact form they doesn't needs from password ?
$mail->Username = "me@gmail.com";
$mail->Password = "my!password";
Hi,
im just trying to make it work a contact form with sendmail.php , but i get an error after i press the send button, it shows me :+1
reCAPTCHA checked failed! Error codes: invalid-input-secret
but the key its the same as the key on googles recaptcha admin site
how can i know whats wrong with the code or something like that ?
Thanks
Same problem as hmontoyal "reCAPTCHA checked failed! Error codes: invalid-input-secret"
I configure the sendmail.php with the environment variables as explained. Any ideas?
i'm not getting any send action on form btn send.........no errors, no response :/
Same problem as @Showcase-Joz. i'm not getting any send action on form btn send.........no errors, no response :/
turns out it is an nginx/php-fpm problem
@dcshapiro could you solve the problem with not getting any send action? What do you mean by an nginx/php-fpm problem? I still cannot send any messages from this form. I'm just getting "Sending" on the button and nothing happens.
same problem. no send action.
did you anybody ever get this snippet work? After submitting, no error and nothing happens. ??
push - same problem
I had this same problem, for me I had to change the port setting for the TLS authentication, After I set it to the right port my emails sent.
Hello. I am trying to get the bootstrap with recapcha working on my website. I have run into some problems and wondered if you could assist me. I have downloaded and installed "jonmbake/bootstrap3-contact-form" and decided to use the above file for the "sendmail.php" I have set up an email with my hosting service and set the appropriate variables for $mail->Host, $mail->Username, $mail->Password and $mail ->addAddress. Then I would upload this to the host and execute. After pressing the "Send Feedback" button, I would get an AJAX response of "An expected error occured while attempting to send the email: SMTP connect() failed." Any help would be appreciated.