Skip to content

Instantly share code, notes, and snippets.

@Stefany93
Created June 21, 2019 22:05
Show Gist options
  • Save Stefany93/2ba727e0985c10f8e1486836e8338ee3 to your computer and use it in GitHub Desktop.
Save Stefany93/2ba727e0985c10f8e1486836e8338ee3 to your computer and use it in GitHub Desktop.
AMP contact form validation and submission
<?php
/*
Function siteURL() courtesy of Chris McKee - https://gist.github.com/ChrisMcKee/1284052
Get sute URL with protocol (http or https)
*/
function siteURL()
{
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'];
return $protocol.$domainName;
}
// -> Example.com
function domainName()
{
return ucfirst($_SERVER['SERVER_NAME']);
}
// cleaning the string
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// End of functions
$domain_name = domainName(); // Example.com
$siteURL = siteURL(); //https://example.com
define('RECEIVER_EMAILS', 'example@example.com');
// Message
$email_subject = "You have a new message from ".clean_string($domain_name);
$name = $_POST['name']; // required
$message = $_POST['message']; // required
$email = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$email_message = "Form details below.\n\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
// create email headers
$headers =
'Reply-To: '.$email."\r\n";
/*
AMP boilerplate CORS code and setting the content type as JSON
Since all errors / sucess messages will be encoded in JSON
*/
header("Access-Control-Allow-Origin: ".
str_replace('.', '-',''.$siteURL.'') .".cdn.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin:$siteURL");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: application/json");
/*
Check whether message, phone, name and email is filled in
Do not send the email if not. And send a JSON string to
display the errors on the front end.
*/
if(!isset($_POST['message']) || empty($_POST['message']) ||
!isset($_POST['phone']) || empty($_POST['phone']) ||
!isset($_POST['name']) || empty($_POST['name']) ||
!isset($_POST['email']) || empty($_POST['email']) ) {
$error_array = ['verifyErrors'=>[['name' => 'empty fields','message' => 'Populate All The Fields']]]; // error to be displayed in amp - mustache
header("Content-Type: application/json");
echo json_encode($error_array);
header("HTTP/1.1 400 Bad Request");
}else{
// If there are no errors, submit the form.
if(mail(RECEIVER_EMAILS, $email_subject, $email_message, $headers))
{
$data = ['submit-sucess'=>'Success!'];
echo json_encode($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment