Contact is simple API for submitting e-mail messages. It's useful for cases when the only dynamic thing on static site is contact form.
Contact is based on Slim Framework. All dependencies are managed using Composer, so you need to have it installed in order to download them. Just run:
$ composer install
To change default validation messages, field labels, recipient email addres etc., just modify config.json
file.
If submitted data is valid:
$ curl -i http://contact/?name=Test+Tosteron&sender=test@tosteron.com&message=Hello!
Status: HTTP/1.1 201 Created
{
"message": "Thank you for your feedback!",
"errors": []
}
If there's one or more errors:
$ curl -i http://contact/?name=&sender=&message=
Status: HTTP/1.1 400 Bad Request
{
"message":"Please check errors below each field.",
"errors": {
"sender": "Sender should not be blank.",
"name": "Name should not be blank.",
"message": "Message should not be blank."
}
}
Below is example JavaScript code showing how API can be used.
$('.contact-form').on('submit', function(event) {
var $form = $(this);
$.ajax({
url: $form.attr('action'),
data: $form.serialize()
})
.always(function() {
$('.success, .error', $form).remove();
})
.done(function(data) {
$form.get().reset();
$('<div>')
.addClass('success')
.text(data.message)
.prependTo($form);
})
.fail(function(xhr) {
var data = xhr.responseJSON;
$.each(data.errors, function(field, error) {
$('[name="' + field + '"]', $form).after($('<p>').addClass('error').text(error));
});
});
event.preventDefault();
});