Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MailThis.to Ajax Example
// The following example POSTS data to mailthis.to, redirects the user to a confirmation page, and then sends an email (upon the successful completion of Recaptcha verification)
$.post('https://mailthis.to/test@example.com', {
email: 'foo@bar.co',
_subject: 'hi!',
message: 'Test'
}).then(function () {
location.href = 'https://mailthis.to/confirm'
});
@availit

This comment has been minimized.

Copy link

availit commented Apr 3, 2018

Is there any way you could add a feature that allows you to set the _after (redirect) url in a query string parameter for use with Ajax submissions?

For example: https://mailthis.to/confirm?after=https://myhomepage.net

@dharmyx

This comment has been minimized.

Copy link

dharmyx commented May 14, 2019

@availit There won't be a need for redirection since this is AJAX based.

@ramit-mitra

This comment has been minimized.

Copy link

ramit-mitra commented Jul 13, 2019

Will the above code work if I skip the line "location.href = 'https://mailthis.to/confirm'" ?

@xxillyap

This comment has been minimized.

Copy link

xxillyap commented Aug 6, 2019

Hello! I have just started programing so probably the solution is very simple. Anyway, as I understand it I need to add the " location.href = 'https://mailthis.to/confirm'" code in but the problem is that I am not sure where to add it. It would be amazing if anyone could help me out! Thank you!

$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(" ") >= 0) {
firstName = name
.split(" ")
.slice(0, -1)
.join(" ");
}
$this = $("#sendMessageButton");
$this.prop("disabled", true); // Disable submit button until AJAX call is complete to prevent duplicate messages
$.ajax({
url: "https://mailthis.to/illyapwork@gmail.com",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$("#success").html("

");
$("#success > .alert-success")
.html(
"×"
)
.append("");
$("#success > .alert-success").append(
"Your message has been sent. "
);
$("#success > .alert-success").append("
");
//clear all fields
$("#contactForm").trigger("reset");
},
error: function() {
// Fail message
$("#success").html("
");
$("#success > .alert-danger")
.html(
"×"
)
.append("");
$("#success > .alert-danger").append(
$("").text(
"Sorry " +
firstName +
", it seems that my mail server is not responding. Please try again later!"
)
);
$("#success > .alert-danger").append("
");
//clear all fields
$("#contactForm").trigger("reset");
},
complete: function() {
setTimeout(function() {
$this.prop("disabled", false); // Re-enable submit button when AJAX call is complete
}, 1000);
}
});
},
filter: function() {
return $(this).is(":visible");
}
});

$('a[data-toggle="tab"]').click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});

/*When clicking on Full hide fail/success boxes */
$("#name").focus(function() {
$("#success").html("");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.