javascript 如何使用javascript发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9543329/
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
How to send mail using javascript
提问by station
I have a webpage with a text box where the user can enter his email id. I want to send mail to the mail Id Entered.
我有一个带有文本框的网页,用户可以在其中输入他的电子邮件 ID。我想向输入的邮件 ID 发送邮件。
<form action="mailto:[email protected]" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>
The problem is I want mailto:[email protected]
to be taken from the text box.Then a message should appear saying that email has been sent successfully. Can I do that using javascript?
问题是我想mailto:[email protected]
从文本框中取出。然后应该会出现一条消息,说明电子邮件已成功发送。我可以使用 javascript 做到这一点吗?
回答by icktoofay
Browser-side JavaScript cannot send email on its own. The best it can do is try to open the user's email application for them so they can send the email. For example:
浏览器端 JavaScript 无法自行发送电子邮件。它可以做的最好的事情是尝试为他们打开用户的电子邮件应用程序,以便他们可以发送电子邮件。例如:
location.href = 'mailto:' + encodeURIComponent(emailAddress) +
'?subject=' + encodeURIComponent(subject) +
'&body=' + encodeURIComponent(body);
You could hook the submit
event of the form, prevent the default action, and execute that code, and the end result would be they could fill out the form, click submit, their email client opens, and they can click send.
您可以挂钩submit
表单的事件,阻止默认操作,然后执行该代码,最终结果是他们可以填写表单,单击提交,打开电子邮件客户端,然后单击发送。
Try iton JSFiddle.
试试吧上的jsfiddle。
回答by Gordon Gustafson
First of all, emails cannot be sent using javascript. It is a client side scripting language only, and cannot send an email without first contacting a server and 'asking' it to send the email using PHP or some other server-side language.
首先,不能使用 javascript 发送电子邮件。它只是一种客户端脚本语言,如果不先联系服务器并“要求”它使用 PHP 或其他一些服务器端语言发送电子邮件,就无法发送电子邮件。
I'm not sure what you mean by saying you want mailto:[email protected]
to be taken from the textbox. mailto:
is just a protocol that causes the browser to open up the user's default email program to send a mail to the given address. You can easily create a mailto: link that the user can click on to do this or pull the email address from an existing mailto: link, but the email still has to be sent some other way.
我不确定你说你想mailto:[email protected]
从文本框中取出是什么意思。mailto:
只是一个协议,它使浏览器打开用户的默认电子邮件程序以向给定地址发送邮件。您可以轻松创建一个 mailto: 链接,用户可以单击该链接来执行此操作,或者从现有的 mailto: 链接中提取电子邮件地址,但电子邮件仍然必须以其他方式发送。
There are plenty of elaborate ways to tell the user a message has been sent succesfully, but the easiest and most direct is probably a simple alert box:
有很多精心设计的方法可以告诉用户消息已成功发送,但最简单和最直接的方法可能是一个简单的警告框:
alert("Your email has been sent succesfully.");