Reputation: 55
I have a very simple form and I am trying to send emails.
<script src="https://smtpjs.com/v3/smtp.js"></script>
<script type="text/javascript">
function sendEmail() {
alert("Submitting form...");
Email.send({
Host: "smtp.mailgun.org",
Username: "[email protected]",
Password: "som-password-thing",
To: '[email protected]',
//From: document.getElementById("form_email").value,
From: "[email protected]",
Subject: "Website Form Submission",
Body: "this is body"
//Body: document.getElementById("form_name").value + " - " + document.getElementById("form_message").value,
})
.then(function (message) {
alert("Form Successfully Submitted. Thank You.")
});
}
</script>
There are no errors, but the page reloads and no email is sent, it also doesnt get to the last alert.
Upvotes: 0
Views: 306
Reputation: 11226
Your form should look like this
<form onsubmit="sendEmail">
...
</form>
and your function should look like this
function sendEmail(event) {
event.preventDefault();
...
}
By default, a form submission will redirect the page (this is how web pages used to make modifications without js). By calling event.preventDefault()
, you stop this default behavior.
Upvotes: 1