如何从JavaScript发送电子邮件 [英] How to send an email from JavaScript

查看:160
本文介绍了如何从JavaScript发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的网站能够发送电子邮件而不刷新页面。所以我想使用Javascript。

I want my website to have the ability to send an email without refreshing the page. So I want to use Javascript.

<form action="javascript:sendMail();" name="pmForm" id="pmForm" method="post">
Enter Friend's Email:
<input name="pmSubject" id="pmSubject" type="text" maxlength="64" style="width:98%;" />
<input name="pmSubmit" type="submit" value="Invite" />

这是我想要调用的功能,但我不知道该怎么做javascript功能从我所做的研究中,我发现一个使用mailto方法的例子,但我的理解是,实际上并不直接从网站发送。

Here is how I want to call the function, but I'm not sure what to put into the javascript function. From the research I've done I found an example that uses the mailto method, but my understanding is that doesn't actually send directly from the site.

所以我的问题是在哪里可以找到什么放在JavaScript函数中直接从网站发送电子邮件。

So my question is where can I find what to put inside the JavaScript function to send an email directly from the website.

function sendMail() {
    /* ...code here...    */
}


推荐答案

您不能直接使用javascript发送电子邮件。

You can't send an email directly with javascript.

然而,您可以打开用户的邮件客户端:

You can, however, open the user's mail client:

window.open('mailto:test@example.com');

还有一些参数可以预先填写主题和正文:

There are also some parameters to pre-fill the subject and the body:

window.open('mailto:test@example.com?subject=subject&body=body');






另一个解决方案是做ajax调用您的服务器,以便服务器发送电子邮件。小心不要让任何人通过您的服务器发送任何电子邮件。


Another solution would be to do an ajax call to your server, so that the server sends the email. Be careful not to allow anyone to send any email through your server.

这篇关于如何从JavaScript发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆