Last active
February 4, 2016 00:52
-
-
Save beaucharman/5166329 to your computer and use it in GitHub Desktop.
Takes data from a from and creates and email, also adds an attachment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple Capture Form | |
* | |
* <input type="hidden" name="first_name" value="" /> | |
* <input type="hidden" name="form_submitted" value="true" /> | |
* | |
* May need to consider using https://github.com/PHPMailer/PHPMailer | |
* with an SMTP server if mail from this form goes to spam folder | |
*/ | |
/* Email header vars */ | |
$to = "Accounts <[email protected]>"; | |
$from = "Enquiries <[email protected]>"; | |
$replyTo = "Reply-To <[email protected]>"; | |
$subject = "form submission"; | |
/* Legit email validation vars */ | |
$request_method_is_post = $_SERVER['REQUEST_METHOD'] == 'POST'; | |
$form_was_summitted = isset($_POST['formSubmitted']); | |
$user_is_human = isset($_POST['isHuman']) && !$_POST['isHuman']; | |
if ($request_method_is_post && $form_was_summitted && $user_is_human) { | |
/* Email content vars */ | |
$name = (isset($_POST['name'])) ? trim(htmlentities(strip_tags($_POST['name']))) : false; | |
$email = (isset($_POST['email'])) ? trim(htmlentities(strip_tags($_POST['email']))) : false; | |
$phoneNumber = (isset($_POST['phone'])) ? trim(htmlentities(strip_tags($_POST['contact']['phone']))) : false; | |
$employer = (isset($_POST['employer'])) ? trim(htmlentities(strip_tags($_POST['employer']))) : false; | |
/** | |
* Build the email | |
*/ | |
if($name && $email && $phoneNumber && $employer) { | |
$body_content = array( | |
"Great news! You have an email from $name.", | |
"They can be contacted via email: $email, or phone: $phoneNumber", | |
"The employer the listed was: $employer" | |
); | |
$body = implode('<br />', $body_content); | |
$headers = array( | |
"MIME-Version: 1.0", | |
"Content-Type: text/html; charset=iso-8859-1", | |
"From: {$from}", | |
"Reply-To: {$replyTo}", | |
"X-Mailer: PHP/" . phpversion() | |
); | |
/** | |
* Attempt to send mail | |
*/ | |
$send_mail = mail($to, $subject, $body, implode("\r\n", $headers)); | |
if ($send_mail) { | |
$feedback_message = 'Thank you for your enquiry!'; | |
$feedback_type = 'success'; | |
http_response_code(200); | |
} else { | |
$feedback_message = 'Sorry, there was an problem submitting your enquiry'; | |
$feedback_type = 'failed'; | |
http_response_code(500); | |
} | |
} else { | |
$feedback_message = 'Please ensure you input all your details'; | |
$feedback_type = 'failed'; | |
http_response_code(403); | |
} | |
} else { | |
$feedback_message = 'Sorry, there was an problem submitting your enquiry'; | |
$feedback_type = 'failed'; | |
http_response_code(403); | |
} | |
$output = json_encode(array('type' => $feedback_type, 'text' => $feedback_message)); | |
exit($output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment