Mail using JS

IS there any way I can send e-mail using javascript…let me explain why exactly I need this.

I am using ajax requests in my app…when a request fails the error callback is called…my intention is that when this is the case an e-mail is sent to the administrator with details about the error.

What can I do?

best is to send the mail from within the server script that encounters the error.

if it’s something like HTTP 404 or 500, you can only use AJAX to call another server script that sends the email for you. client-side JavaScript was never intended to send emails, so while it may be possible, it’s more work than it’s worth.

you say to send the mail from the script that encounters the error…

Yes…but if the request fails any script will be unreachable either way.

Put the AJaX call that sends the email from the server in a try/catch. If the try fails, the catch will call the email script.

HTH,

:slight_smile:

that only works if there’s an error in the JavaScript code. if an AJAX request returns with a HTTP 4xx or 5xx there will be no error thrown unless you manually throw an error.

and as of jQuery 3, errors won’t make it out of the ajax component since it’s a Promise.

I suppose you are referring to the ajax call that makes the request to the server…cause there is not any ajax call made from within the script.

In other words put the code in the server server script in a try/catch.Is this what you suggest.

I am confused…

Or put the ajax call(the JS code) in a try/catch?

Here is where I was thinking of. Put the code that could fail inside a try, and have a catch use AJaX to call a server-side script to generate the email.

However, as @Dormilich states, the AJaX request will not necessarily fail, as it will still return a code (which AJaX would consider a success, regardless of the returned value.)

:slight_smile:

I suppose you mean the JS/client code…this I should put in a try block

what about if I make an ajax request to a script on the server everytime the error callback is fired…and this script will send the e-mail to the admin…

I am worry about the overhead though…if error callback fires it will be 2 ajax requests back-to-back.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.