Javascript 我可以使用 HTML5 发送客户端电子邮件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5467395/
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
Can I use HTML5 to send a client-side email?
提问by Gus
I want to send an email in HTML5. I don't want to force the user to open a mail client, I want to send the email directly from the web page.
我想用 HTML5 发送电子邮件。我不想强迫用户打开邮件客户端,我想直接从网页发送电子邮件。
On a side note, is there any way at all to do this in JavaScript? I know it's probably not possible, just wondering if there are any crafty ways to pull it off going completely through the client.
附带说明一下,有什么办法可以在 JavaScript 中做到这一点吗?我知道这可能是不可能的,只是想知道是否有任何巧妙的方法可以完全通过客户端实现它。
回答by Mark At Ramp51
In short NO not directly from the client (excluding hacks).
总之不直接来自客户端(不包括黑客)。
you could make an ajax call to your server and send an email.
您可以对您的服务器进行 ajax 调用并发送电子邮件。
the problem with doing it from the client and not using a mail client is complicated. For example most consumer ISPs have their own SMTP relay that all outbound mail on port 25 must be transmitted over. You website will have trouble obtaining the proper information to do this. Secondly the webbrowser doesn't understand the SMTP protocol and neither does the XMLHttpRequest object.
从客户端执行此操作而不使用邮件客户端的问题很复杂。例如,大多数消费者 ISP 都有自己的 SMTP 中继,必须通过端口 25 上的所有出站邮件进行传输。您的网站将无法获得正确的信息来执行此操作。其次,网络浏览器不了解 SMTP 协议,XMLHttpRequest 对象也不了解。
So if you are a hacker ninja, maybe you can figure something out with ActiveX, Java Applets, or flash, but you basically would have to be operating directly with a tcp socket and issuing SMTP protocol commands over that socket.
因此,如果您是一名黑客忍者,也许您可以使用 ActiveX、Java Applets 或 flash 来解决问题,但您基本上必须直接使用 tcp 套接字进行操作并通过该套接字发出 SMTP 协议命令。
There are many obstacles to overcome, in fact I don't know how to do it, but where there is will there is a way. Don't be surprised that if you do find a hack, it may be plugged swiftly by the major browser vendors.
有很多障碍要克服,其实我不知道该怎么做,但是有方法就有方法。不要感到惊讶,如果你确实发现了一个黑客,它可能会被主要的浏览器供应商迅速插入。
回答by JTew
Yes it is possible. But not practical ** See Edit 2
对的,这是可能的。但不实用**见编辑2
Some HTML5 implementations include support for websockets, essentially a tcp connection to a server. Overlay some send/recv code and you can build a SMTP client.
一些 HTML5 实现包括对 websockets 的支持,本质上是一个到服务器的 tcp 连接。叠加一些发送/接收代码,您就可以构建一个 SMTP 客户端。
In fact it looks like nodejs and websocket support has been used to implement a smtp client ... see here ...
事实上,它看起来像 nodejs 和 websocket 支持已经被用来实现一个 smtp 客户端......见这里......
You would still need a smtp server, username, password, etc just like a standard smtp client in order for it to work.
您仍然需要一个 smtp 服务器、用户名、密码等,就像标准的 smtp 客户端一样,才能使其工作。
Using this method for spam would be unlikely as your smtp provider could easily cancel your account.
使用这种方法处理垃圾邮件是不太可能的,因为您的 smtp 提供商可以轻松取消您的帐户。
=== EDIT ===
=== 编辑 ===
Actually you could build a server less version, it would have to also implement name server lookups to find mx records. Chances are however that any decent SMTP servers maintain spamlist blacklist tables and connecting from an random ip address would see the email commonly marked as spam.
实际上,您可以构建一个无服务器版本,它还必须实现名称服务器查找以查找 mx 记录。然而,任何体面的 SMTP 服务器都有可能维护垃圾邮件黑名单表,并且从随机 IP 地址连接会看到通常标记为垃圾邮件的电子邮件。
Also talking to smtp servers that require secure mail connections could be difficult.
与需要安全邮件连接的 smtp 服务器交谈也可能很困难。
As others have mentioned there are malicious uses to this implementation like sending spam. I guess it is possible you could be a HTML5 botnet creator but I would have thought that you would know most of this already :)
正如其他人所提到的,此实现存在恶意用途,例如发送垃圾邮件。我猜你有可能成为 HTML5 僵尸网络的创建者,但我原以为你已经知道大部分内容了 :)
=== EDIT 2 ===
=== 编辑 2 ===
As Mark At Ramp51 mentioned, Handshaking is required with websockets. This was something I wasn't aware of. You would have to hack the websocket implementation to bypass handshaking.
正如 Mark At Ramp51 所提到的,websockets 需要握手。这是我不知道的事情。您必须破解 websocket 实现才能绕过握手。
The correct way is to have the web server forward the email.
正确的方法是让网络服务器转发电子邮件。
回答by SLaks
This is not possible.
这不可能。
Instead, you should use AJAX to send the email on the server.
相反,您应该使用 AJAX 在服务器上发送电子邮件。
回答by Kirk Beard
You can't send the email using JavaScript alone. You'll need some form of server side processing (PHP, ASP, etc) to send the actual email.
您不能单独使用 JavaScript 发送电子邮件。您需要某种形式的服务器端处理(PHP、ASP 等)来发送实际的电子邮件。
There's a good tutorial on setting up an ajax form here: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
这里有一个关于设置 ajax 表单的很好的教程:http: //net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/
It doesn't include the PHP (or ASP, etc) for sending the email, but there are plenty of tutorials out there for how to send an email using PHP.
它不包括用于发送电子邮件的 PHP(或 ASP 等),但是有很多关于如何使用 PHP 发送电子邮件的教程。
回答by zhibirc
Send email directly from Javascript
From official resource:
来自官方资源:
How does it work?
它是如何工作的?
Connect your email service Choose from a wide variety of email services. We support both transactional email services (Mailgun, Mailjet, Mandrill, SendGrid, Amazon SES and Postmark) and personal email services (AOL, Gmail, FastMail, iCloud, Mail.ru, Outlook, Yahoo, Yandex and Zoho).
Create email templates Choose from a list of our template designs, or easily build your own. Templates are parametrized, so that you can further customize them via Javascript.
Send email with our Javascript API Add our Javscript SDK, and start sending emails!
连接您的电子邮件服务 从各种电子邮件服务中进行选择。我们支持交易电子邮件服务(Mailgun、Mailjet、Mandrill、SendGrid、Amazon SES 和 Postmark)和个人电子邮件服务(AOL、Gmail、FastMail、iCloud、Mail.ru、Outlook、Yahoo、Yandex 和 Zoho)。
创建电子邮件模板从我们的模板设计列表中进行选择,或轻松构建您自己的模板。模板是参数化的,因此您可以通过 Javascript 进一步自定义它们。
使用我们的 Javascript API 发送电子邮件 添加我们的 Javscript SDK,然后开始发送电子邮件!
Here's what a typical call looks like:
下面是一个典型的调用:
var service_id = 'my_mandrill';
var template_id = 'feedback';
var template_params = {
name: 'John',
reply_email: '[email protected]',
message: 'This is awesome!'
};
emailjs.send(service_id,template_id,template_params);
All existing APIs require using a secret key, which you obviously wouldn't want to share in your front-end code. Specified service overcome this by allowing sending only predefined templates, so for a "Share with a friend" feature you'd create a template called "share".
所有现有的API需要使用密钥,你显然不希望与他人分享你前端代码。指定的服务通过只允许发送预定义的模板来克服这个问题,因此对于“与朋友共享”功能,您需要创建一个名为“共享”的模板。
回答by Samuel Neff
You can't do it purely through client side code.
你不能纯粹通过客户端代码来做到这一点。
You can do it with a server callback, AJAX.
您可以使用服务器回调 AJAX 来实现。