使用 JavaScript 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7097363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:57:38  来源:igfitidea点击:

Send email with JavaScript

javascriptemail

提问by user23557676

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

可能重复:
如何使用 javascript 或 jquery 发送电子邮件

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

如何使用 JavaScript 发送电子邮件,以便 Gmail、Yahoo 等可以阅读?

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="[email protected]";

           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()" />

回答by jfriend00

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.

如果您真的想通过 javascript 发送电子邮件,那么您需要一个可以进行 ajax 调用的协作服务器进程。javascript ajax 调用会将电子邮件的内容传送到服务器,服务器将发送实际的电子邮件。

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.

浏览器客户端实际上并不发送电子邮件。mailto: 功能允许浏览器与用户选择的电子邮件程序进行通信,以要求该程序代表用户发送电子邮件。此功能可能存在也可能不存在或在任何给定浏览器中工作 - 您无法知道。例如,我的妻子使用 Yahoo 邮件和 mailto:链接对她不起作用,因为她的计算机上没有电子邮件客户端程序来与浏览器协调发送电子邮件。在我看来,如今有如此多的网络电子邮件客户端,依靠浏览器中的 mailto: 功能是一个坏主意。

If you don't have your own server that can do this for you, then there are some services out there like http://wufoo.comthat 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.

如果您没有自己的服务器可以为您执行此操作,那么有一些服务,例如http://wufoo.com提供电子邮件发送服务(某些部分是免费的),您可能会通过以下方式提供给他们javascript/ajax。要以这种方式使用 Wufoo,您必须按照 wufoo 期望的方式构建一个 wufoo 表单,将其包含在您的页面中(可以隐藏),填写(可以保持隐藏),然后通过以下方式提交ajax 和 wufoo 服务器会将电子邮件发送到您想要的目的地。

回答by Tejs

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

你没有。在纯客户端机制中,您将一个带有mailto:前缀的链接放在那里。又名:

 mailto:[email protected]?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.

这就是你正在做的事情,除了用户必须点击它;您不能以编程方式调用发送电子邮件。此外,您也无法控制 gmail 或 yahoo 对收到的电子邮件执行的操作。