Skip to content

Instantly share code, notes, and snippets.

@beaucharman
Last active February 4, 2016 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beaucharman/5166329 to your computer and use it in GitHub Desktop.
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.
<?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 <accounts@website.com.au>";
$from = "Enquiries <enquiries@website.com.au>";
$replyTo = "Reply-To <reply-to@website.com.au>";
$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