-1

Possible Duplicate:
how to send email by using javascript or jquery

How can I send an email with JavaScript, that is able to be read by Gmail, Yahoo, etc.?

This is what I have:

<script language="javascript">

       function SendEmail()
       {
           var body = escape(document.Form.textfieldName.value +"\n"+document.Form.textfieldEmail.value +"\n"+document.Form.textfieldMessage.value);

    var sTo="testmail@mail.com";

           var subject = "This is SyntaxHelpSub";

                           window.location.href =
    "mailto:"+sTo+"?subject="+subject+"&body="+body;

               }
</script>

        <div class="formfields">Name: <input class="tbox" id="textfieldName" type="text">

         Email: <input class="tbox" id="textfieldEmail" type="text">

<br><br>

Message: <input class="tbox" id="textfieldMessage" type="text" style="width: 343px">
    </div>

<br>

<input type="button" value="Send!" onclick="SendEmail()" />
Community
  • 1
  • 1
user23557676
  • 421
  • 4
  • 7
  • 13
  • 4
    :D I don't think you can if you mean sending emails without invoking an app of some kind which takes the job over. – Shef Aug 17 '11 at 18:07
  • possible duplicate:http://stackoverflow.com/questions/1415205/how-to-send-email-by-using-javascript-or-jquery and http://stackoverflow.com/questions/3296402/how-do-i-send-email-using-javascript – SRC Aug 17 '11 at 18:08
  • 3
    Unless you're using server-side JavaScript (i.e. Node.js) you wont be able to actually *send* an email via JS. The best you could do is construct a `mailto` link that the user could click. – zzzzBov Aug 17 '11 at 18:08
  • http://stackoverflow.com/questions/2718017/how-to-send-email-through-javascript-without-using-a-program/2718550#2718550 Short answer: not possible. – Miriam Aug 17 '11 at 18:11
  • You're using the mailto schema. This will open the default mail manager on the user's computer. These emails will be readable to Gmail and other email providers. Also, you can't just call that function. The user has to click on a link with the mailto schema for it to work. – Some Guy Aug 17 '11 at 18:12
  • There seems to be a new solution at the horizon. It's called [EmailJS](http://www.emailjs.com). They claim that no server code is needed. You can request an invitation. – Christiaan Westerbeek May 18 '15 at 16:20

2 Answers2

4

If you really want to send an email via javascript, then you need a cooperating server process that you can make an ajax call to. The javascript ajax call will deliver the contents of the email to the server and the server will send out the actual email.

Browser clients do not actually send emails. The mailto: functionality allows a browser to communicate with the user's chosen email program to ask that program to send out an email on the user's behalf. This functionality may or may not be present or working in any given browser - you have no way of knowing. For example, my wife uses Yahoo mail and mailto: links never work for her because there is no email client program on her computer to coordinate with the browser to send out an email. In my opinion, these days with so many web email clients, it is a bad idea to rely on mailto: functionality in a browser.

If you don't have your own server that can do this for you, then there are some services out there like http://wufoo.com that offer email sending services (some parts of free) and you could likely deliver to them via javascript/ajax. To use Wufoo in this way, you'd have to build a wufoo form the way wufoo expects it to work, include it in your page (it can be hidden), fill it in (it can stay hidden), then submit it via ajax and the wufoo servers would send out the email to your desired destination.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
2

You don't. In pure client side mechanics, you put a link out there with the mailto: prefix. AKA:

 mailto:someaddress@gmail.com?subject=SomeText

Which is what it looks like you are doing, except that the user must click on this; you cannot invoke a send email programmatically. In addition, you also dont have any control over what gmail or yahoo does with email it receives.

Tejs
  • 40,736
  • 10
  • 68
  • 86