Skip to content

Instantly share code, notes, and snippets.

@alexweissman
Last active March 5, 2016 20:01
Show Gist options
  • Save alexweissman/bb399eb389366a80c0a4 to your computer and use it in GitHub Desktop.
Save alexweissman/bb399eb389366a80c0a4 to your computer and use it in GitHub Desktop.
My website's contact form (for UserFrosting v0.3.1)
Put this in userfrosting/schema/forms/ and delete this comment
{
"name" : {
"validators" : {
"length" : {
"min" : 1,
"max" : 200,
"message" : "Please enter a name between 1 and 200 characters."
},
"required" : {
"message" : "Please specify your name."
}
}
},
"email" : {
"validators" : {
"required" : {
"message" : "Please specify your email address."
},
"length" : {
"min" : 1,
"max" : 150,
"message" : "Please enter an email address between 1 and 150 characters."
},
"email" : {
"message" : "That does not appear to be a valid email address."
}
}
},
"phone" : {
"validators" : {
"length" : {
"min" : 1,
"max" : 50,
"message" : "The phone number must be between 1 and 50 characters."
}
} ,
"default" : "Not specified"
},
"message" : {
"validators" : {
"required" : {
"message" : "Please enter a message"
}
},
"sanitizers" : {
"purify" : {}
}
}
}
{# Put this file in the pages/ subdirectory for your theme #}
{% extends "layouts/layout-jumbotron.twig" %}
{% set page_group = "contact" %}
{# Set page properties (page.*) here. #}
{% block page %}
{# By putting this in a special block, we ensure that it will be set AFTER the default values are set in the parent template,
but BEFORE the page itself is rendered. #}
{% set page = page | merge({
"title" : "Contact",
"description" : "Send me a message."
}) %}
{{ parent() }}
{% endblock %}
{% block content %}
<h1>Contact Me</h1>
<div class='alert alert-warning'><i class='fa fa-question-circle'></i> Looking for help with UserFrosting? Check out the <a href="http://www.userfrosting.com/#help">help section</a> of the website, and determine which option best describes your situation. Please do <b>not</b> contact me through this form for UserFrosting related questions or issues.</div>
<form id="contact-form" method="post" action="{{ site.uri.public }}/contact" role="form">
<div class="row">
<div id="userfrosting-alerts" class="col-lg-12">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p class="lead">I'd love to hear from you - so long as you're a real person! Send me a message through this form, and I'll try to get back to you soon.</p>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="control-label">Name</label>
<div class="input-group"><span class="input-group-addon"><i class="fa fa-fw fa-edit"></i></span>
<input type="text" class="form-control " name="name" autocomplete="off" value="" placeholder="First and last name"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group ">
<label class="control-label">Email</label>
<div class="input-group"><span class="input-group-addon"><i class="fa fa-fw fa-envelope"></i></span>
<input type="text" class="form-control " name="email" autocomplete="off" value="" placeholder="Email address"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group ">
<label class="control-label">Phone (optional)</label>
<div class="input-group"><span class="input-group-addon"><i class="fa fa-fw fa-phone"></i></span>
<input type="text" class="form-control" name="phone" autocomplete="off" value="" placeholder="Telephone number"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label>Message</label>
<textarea class='form-control' name="message" rows='8' placeholder="Say it, don't spray it!"></textarea>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-12">
<div>
<button type="submit" class="btn btn-success " data-loading-text="Please wait..." >
<i class='fa fa-paper-plane'></i> Send
</button>
</div>
</div>
</div>
<div class="collapse">
<label>Spiderbro: Don't change me bro, I'm tryin'a catch some flies!</label>
<input name="spiderbro" id="spiderbro" value="http://"/>
</div>
</form>
{% endblock %}
{% block page_scripts %}
<script>
$(document).ready(function() {
// Process form
ufFormSubmit($("#contact-form"),
{{ validators | raw }},
$("#userfrosting-alerts"),
function(data, statusText, jqXHR) {
// Reload the page
window.location.reload();
}
);
});
</script>
{% endblock %}
// Add this to public/index.php
/**
* Renders the contact form.
*/
$app->get('/contact/?', function () use ($app) {
$schema = new \Fortress\RequestSchema($app->config('schema.path') . "/forms/contact.json");
$app->jsValidator->setSchema($schema);
$app->render('pages/contact.twig', [
'validators' => $app->jsValidator->rules()
]);
});
/**
* Processes the contact form submission.
*/
$app->post('/contact/?', function () use ($app) {
// POST: name, email, [phone], message, spiderbro
$post = $app->request->post();
// Get the alert message stream
$ms = $app->alerts;
// Check the honeypot. 'spiderbro' is not a real field, it is hidden on the main page and must be submitted with its default value for this to be processed.
if (!$post['spiderbro'] || $post['spiderbro'] != "http://"){
error_log("Possible spam received:" . print_r($app->request->post(), true));
$ms->addMessage("danger", "Aww hellllls no!");
$app->halt(500); // Don't let on about why the request failed ;-)
}
// Load the request schema
$requestSchema = new \Fortress\RequestSchema($app->config('schema.path') . "/forms/contact.json");
// Set up Fortress to process the request
$rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post);
// Sanitize data
$rf->sanitize();
// Validate, and halt on validation errors.
if (!$rf->validate())
$app->halt(400);
// Get the filtered data
$data = $rf->data();
// Set up and send email
$twig = $app->view()->getEnvironment();
$template = $twig->loadTemplate("mail/contact.twig");
$notification = new \UserFrosting\Notification($template);
$notification->from(
$app->config('email')['admin'],
'alexanderweissman.com contact form',
$data['email'],
$data['name']
);
$notification->addEmailRecipient(
$app->config('email')['admin'],
'Alexander Weissman', [
"data" => $data
]
);
try {
$notification->send();
$app->alerts->addMessage("success", "Ok, your message has been sent!");
} catch (\phpmailerException $e){
$app->alerts->addMessage("warning", "Your request was received, but we're having trouble with our mail servers at the moment. If you don't hear from us in 8 hours, please submit another request.");
error_log('Mailer Error: ' . $e->errorMessage());
$app->halt(500);
}
});
{# Put this file in the mail/ subdirectory of your theme (or default theme) #}
{% block subject %}
alexanderweissman.com - message from {{ data.name }}
{% endblock %}
{% block body %}
<p>
Name: {{ data.name }}<br>
Phone number: {{ data.phone }}
</p>
<p>
{{ data.message }}
</p>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment