|
<?php |
|
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer; |
|
use PHPMailer\PHPMailer\OAuth; |
|
use League\OAuth2\Client\Provider\Google; |
|
|
|
require 'vendor/autoload.php'; |
|
require 'vendor/google/recaptcha/src/autoload.php'; |
|
|
|
$fromEmail = 'marko@idzan.eu'; //Email for From field |
|
$fromName = 'Marko Idzan'; //Name in From |
|
|
|
$sendToEmail = 'marko@idzan.eu'; //Send CF data to this mail |
|
$sendToName = 'Idzan Web Site'; //CF data name |
|
|
|
$subject = 'New message from contact form'; |
|
|
|
$fields = array('name' => 'Name', 'email' => 'Email', 'message' => 'Message'); |
|
|
|
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!'; |
|
|
|
$errorMessage = 'There was an error while submitting the form. Please try again later'; |
|
|
|
$recaptchaSecret = '***secret***'; |
|
|
|
error_reporting(E_ALL); |
|
|
|
try { |
|
if (!empty($_POST)) { |
|
|
|
if (!isset($_POST['g-recaptcha-response'])) { |
|
throw new \Exception('ReCaptcha is not set.'); |
|
} |
|
|
|
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost()); |
|
|
|
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); |
|
|
|
if (!$response->isSuccess()) { |
|
throw new \Exception('ReCaptcha was not validated.'); |
|
} |
|
|
|
$emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>"; |
|
$emailTextHtml .= "<table>"; |
|
|
|
foreach ($_POST as $key => $value) { |
|
if (isset($fields[$key])) { |
|
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>"; |
|
} |
|
} |
|
|
|
$emailTextHtml .= "</table><hr>"; |
|
|
|
$mail = new PHPMailer; |
|
|
|
$mail->setFrom($fromEmail, $fromName); |
|
$mail->addAddress($sendToEmail, $sendToName); |
|
$mail->addReplyTo($from); |
|
|
|
$mail->isHTML(true); |
|
|
|
$mail->Subject = $subject; |
|
$mail->Body = $emailTextHtml; |
|
$mail->msgHTML($emailTextHtml); |
|
|
|
$mail->isSMTP(); |
|
|
|
$mail->SMTPDebug = 0; |
|
$mail->Debugoutput = 'html'; |
|
|
|
$mail->Host = 'smtp.gmail.com'; |
|
$mail->Port = 587; |
|
$mail->SMTPSecure = 'tls'; |
|
$mail->SMTPAuth = true; |
|
$mail->AuthType = 'XOAUTH2'; |
|
|
|
$email = 'marko@idzan.eu'; //Email which get validation, learn more at PHPMailer |
|
$clientId = 'myclientId'; |
|
$clientSecret = 'myclientSecret'; |
|
$refreshToken = 'refreshToken'; |
|
|
|
$provider = new Google( |
|
[ |
|
'clientId' => $clientId, |
|
'clientSecret' => $clientSecret, |
|
] |
|
); |
|
|
|
$mail->setOAuth( |
|
new OAuth( |
|
[ |
|
'provider' => $provider, |
|
'clientId' => $clientId, |
|
'clientSecret' => $clientSecret, |
|
'refreshToken' => $refreshToken, |
|
'userName' => $email, |
|
] |
|
) |
|
); |
|
|
|
$mail->SMTPOptions = array( |
|
'ssl' => array( |
|
'verify_peer' => false, |
|
'verify_peer_name' => false, |
|
'allow_self_signed' => true |
|
) |
|
); |
|
|
|
if (!$mail->send()) { |
|
throw new \Exception('I could not send the email.' . $mail->ErrorInfo); |
|
} |
|
|
|
$responseArray = array('type' => 'success', 'message' => $okMessage); |
|
} |
|
} catch (\Exception $e) { |
|
// $responseArray = array('type' => 'danger', 'message' => $errorMessage); |
|
$responseArray = array('type' => 'danger', 'message' => $e->getMessage()); |
|
} |
|
|
|
|
|
// if requested by AJAX request return JSON response |
|
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
|
$encoded = json_encode($responseArray); |
|
|
|
header('Content-Type: application/json'); |
|
|
|
echo $encoded; |
|
} |
|
// else just display the message |
|
else { |
|
echo $responseArray['message']; |
|
} |