-1

I was handed a website to work on and I'm not very familiar with AJAX. I was hoping there was a simple solution to the URL: portion of the Ajax in order to send an email. I'd rather not use a PHP script in place of what's already there but if needed I can.

This is for a website that's mostly bootstrapped with some simple HTML code and naming conventions are standard.

<script>
    $("#btnSend").click(function () {

        var email = $('#txtFromEmail').val();

        if (($('#txtName').val().length === 0) || (email.length === 0) ||
            ($('#txtSubject').val().length === 0) || ($('#txtBody').val().length === 0)) {
            alert("Please fill out all of the form.");
        } else {
            var emailModel = {
                Name: $('#txtName').val(),
                FromEmail: $('#txtFromEmail').val(),
                Subject: $('#txtSubject').val(),
                Body: $('#txtBody').val()
            };

            $.ajax({
                url: "@Url.Action("Contact", "Main")",
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(emailModel),
                success: function (result) {
                    if (result.message.length == 0) {
                        alert("Message Sent Successfully!");
                    } else {
                        alert("Message failed to send. Please try again or call the Sports Hub");
                    }
                }
            });
        }
    });
</script>
sao
  • 1,835
  • 6
  • 21
  • 40
  • If you change the double quotes to single quotes for the value of the url variable it fixes your error `url: '@Url.Action("Contact", "Main")',` This isn't PHP. JavaScript cannot send mail. It needs a server-side language or something like NodeJS to send mail. – daddygames Oct 21 '19 at 15:58
  • What is the HTML? what is your actual question/issue? Ajax does not send email, your server sends email. – Mark Schultheiss Oct 21 '19 at 15:59
  • @daddygames - no that is pretty standard .Net MVC code when used in a view single quotes will actually break it as it is c# code inside that – Mark Schultheiss Oct 21 '19 at 16:01
  • Okay, fixed that, it's sever side yes. Does it then send an email associated with the one linked to the email server? or where is "contact" and "main" coming from? – Postman507 Oct 21 '19 at 16:01
  • @daddygames — The existing double quotes are fine: the content of `@Url.Action(...)` will be replaced server-side by ASP.NET. – Quentin Oct 21 '19 at 16:01
  • What is your server side code for that MainController Contact method in the url helper there? – Mark Schultheiss Oct 21 '19 at 16:02
  • I'm not sure, I think it was using the previous hosting's email server before I switched the hosting. – Postman507 Oct 21 '19 at 16:13
  • There are some javascript email solutions in some of the answers here: https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript – tshimkus Oct 21 '19 at 16:19
  • I understand how the code works and that it's not necessary to change the quotes. However, the image provided in the OP showed squigglies indicating an error at this line and I was simply trying to provide a way to resolve that in the editor. - that image has since been removed from the post. – daddygames Oct 21 '19 at 17:09

3 Answers3

2

Ajax can make HTTP requests. That is all.

If you want to send email then you'll need to either:

  • Use the user's email client (and not Ajax) which is highly unreliable
  • Make an HTTP request to a web service that will send the email

You could write the web service in PHP, or you could use a different programming language (you seem to be using ASP.NET so any ASP.NET compatible language will do), or you could find a third-party hosted service.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

There is no way you can send an email directly from Javascript.

Either you use php or open directly the mail client from the browser:

window.open('mailto:test@example.com');

If you want to pass more parameters you can use the following string:

window.open('mailto:test@example.com?body=body&subject=subject');
  • I believe it was server side before using a mail client from the last hosting service – Postman507 Oct 21 '19 at 16:06
  • This answer was stolen, verbatim, from another user on the following link -> https://stackoverflow.com/questions/7381150/how-to-send-an-email-from-javascript – daddygames Oct 21 '19 at 17:07
0

you can use an email web service to do this. i replaced my PHP page because it was a pain setting it up when i migrated from godaddy to aws. check out formspree. you will be able to integrate thru the front-end. on your first email you'll have to accept formspree emails, once you do, all other emails will be fwd to you.

sao
  • 1,835
  • 6
  • 21
  • 40