如何在不打开邮件客户端的情况下使用JavaScript发送电子邮件? [英] How do I send email with JavaScript without opening the mail client?

查看:150
本文介绍了如何在不打开邮件客户端的情况下使用JavaScript发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有注册按钮的HTML页面,该页面应该在不打开本地邮件客户端的情况下悄悄发送电子邮件。这里是我的HTML:

I'm writing a HTML page with a registration button that should just silently send an email without opening the local mail client. Here is my HTML:

<form method="post" action="">
    <input type="text" id="email_address" name="name" placeholder="Enter your email address..." required>
    <button onclick="sendMail(); return false">Send Email</button>
</form>

...以下是我的JavaScript代码:

... and here is my JavaScript code:

<script type="text/javascript">
  function sendMail() {
    var link = 'mailto:hello@domain.com?subject=Message from '
             +document.getElementById('email_address').value
             +'&body='+document.getElementById('email_address').value;
    window.location.href = link;
}
</script>

上面的代码工作正常......但它打开本地电子邮件客户端。如果我在 onclick 属性中删除 return 语句,如下所示:

The code above works... but it opens the local email client. If I remove the return statement in the onclick attribute like this:

<form method="post" action="">
    <input type="text" id="email_address" name="name" placeholder="Enter your email address..." required>
    <button onclick="sendMail()">Send Email</button>
</form>

...那么电子邮件根本就不会发送。我错过了什么?

... then the email is not sent at all. Am I missing something?

任何帮助都会被认识到: - )

Any help would be reeeally appreciated :-)

推荐答案

您需要服务器端支持来实现这一点。基本上你的表单应该被发布(AJAX也很好),并且服务器应该通过SMTP连接到某个邮件提供商并发送该电子邮件。

You need a server-side support to achieve this. Basically your form should be posted (AJAX is fine as well) to the server and that server should connect via SMTP to some mail provider and send that e-mail.

甚至如果可以直接使用JavaScript(即来自用户计算机)发送电子邮件,则用户仍然必须连接到某个SMTP服务器(例如gmail.com),提供SMTP凭证等。这通常在服务器端(在您的应用程序中),它知道这些凭证。

Even if it was possible to send e-mails directly using JavaScript (that is from users computer), the user would still have to connect to some SMTP server (like gmail.com), provide SMTP credentials, etc. This is normally handled on the server-side (in your application), which knows these credentials.

这篇关于如何在不打开邮件客户端的情况下使用JavaScript发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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