Javascript 如何使用JS发送带附件的邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11273466/
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 with attachment using JS?
提问by Mausmee Rai
Can anybody tell me how to send mail with an attachment using JavaScript?
谁能告诉我如何使用 JavaScript 发送带有附件的邮件?
回答by 18bytes
You have two options to send email:
您有两种发送电子邮件的选项:
- Use server side implementation to send email, access it in Javascript via XMLHttpRequest
Open the local email client from JavaScript and user can send email with pre-populated data.
var link = "mailto:[email protected]"; // In addition to this you can add subject or body as parameter . // For e.g. // "mailto:[email protected]?subject=test subject&body=my text" window.location.href = link;
- 使用服务器端实现发送电子邮件,通过 XMLHttpRequest 在 Javascript 中访问它
从 JavaScript 打开本地电子邮件客户端,用户可以发送带有预填充数据的电子邮件。
var link = "mailto:[email protected]"; // In addition to this you can add subject or body as parameter . // For e.g. // "mailto:[email protected]?subject=test subject&body=my text" window.location.href = link;
回答by grimmi
With pure JavaScript, you can't send e-mails from the client. Remember, JavaScript is executed at the client (i. e. the user's browser).
使用纯 JavaScript,您无法从客户端发送电子邮件。请记住,JavaScript 在客户端(即用户的浏览器)执行。
回答by Utkanos
JavaScript is a client-side language. It is not concerned with (indeed, cannot) send e-mail, with or without an attachment. You'll need something server-side for that.
JavaScript 是一种客户端语言。它不关心(实际上,不能)发送带有或不带有附件的电子邮件。为此,您需要一些服务器端的东西。
JavaScript can merely invoke the server-side script to send the e-mail, by requesting it, say, over AJAX, but it is not JavaScript which sends the e-mail.
JavaScript 只能调用服务器端脚本来发送电子邮件,例如通过 AJAX 请求它,但它不是发送电子邮件的 JavaScript。
This is akin to people mistakenly writing things like "I have some JavaScript which is getting some info from my database."It is not - it is requesting a server-side script which is getting the info from the database.
这类似于人们错误地编写诸如“我有一些 JavaScript 可以从我的数据库中获取一些信息”之类的内容。它不是 - 它正在请求从数据库获取信息的服务器端脚本。
回答by PruitIgoe
You can't send an email directly from Javascript. You could use it to do an AJAX call to send mail from whatever server side language you are using.
您不能直接从 Javascript 发送电子邮件。您可以使用它来执行 AJAX 调用,以从您使用的任何服务器端语言发送邮件。
If you were using PHP:
如果您使用的是 PHP: