Skip to content

Instantly share code, notes, and snippets.

@jtribble
Last active August 29, 2015 14:18
Show Gist options
  • Save jtribble/2490beb8d29ecaf79b1b to your computer and use it in GitHub Desktop.
Save jtribble/2490beb8d29ecaf79b1b to your computer and use it in GitHub Desktop.
<form>
<input type="text" class="form-control" id="name-input" placeholder="Your name">
<input type="email" class="form-control" id="email-input" placeholder="Your email">
<textarea id="message-input" class="form-control" placeholder="What did you want to tell us?"></textarea>
<button type="submit" class="btn btn-default" id="submit-button">Send Message</button>
</form>
<script>
jQuery(document).ready(function($) {
var mandrillApiKey = 'abcd1234'; // your mandrill api key
var nameInput = $('#name-input'),
emailInput = $('#email-input'),
messageInput = $('#message-input'),
submitButton = $('#submit-button');
submitButton.click(function(e) {
e.preventDefault(); // prevent default form submission
sendMandrillEmail(nameInput.val(), emailInput.val(), messageInput.val());
});
function sendMandrillEmail(theirName, theirEmail, theirMessage) {
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': mandrillApiKey,
'message': {
'from_email': theirEmail,
'to': [
{
'email': '[email protected]',
'name': 'Rico Lavender',
'type': 'to'
},
{
'email': '[email protected]',
'name': 'Manny Felix',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'Felixico.co "Book Us" Submission',
'html': formatEmailHtml(theirName, theirEmail, theirMessage)
}
},
beforeSend: function() {
submitButton.html('Sending <i class="fa fa-spinner fa-spin"></i>');
}
})
.done(function(response) { // do this on success
submitButton.html('Sent <i class="fa fa-check"></i>').removeClass('btn-default').addClass('btn-success');
})
.fail(function(response) { // do this on error
submitButton.html('Error <i class="fa fa-times"></i>').removeClass('btn-default').addClass('btn-warning');
})
.always(function(response) { // do this always
console.log(response);
});
}
function formatEmailHtml(name, email, message) {
var html = '<ul>';
html += ' <li><strong>Name:</strong> ' + name + '</li>';
html += ' <li><strong>Email:</strong> ' + email + '</li>';
html += ' <li><strong>Message:</strong> ' + message + '</li>';
html += '</ul>';
return html;
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment