如何在不打开邮件客户端的情况下使用 JavaScript 发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14268796/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:13:34  来源:igfitidea点击:

How do I send email with JavaScript without opening the mail client?

javascripthtmlhtml-email

提问by j3d

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:

我正在编写一个带有注册按钮的 HTML 页面,该页面应该在不打开本地邮件客户端的情况下静默发送电子邮件。这是我的 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>

... and here is my JavaScript code:

...这是我的 JavaScript 代码:

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

The code above works... but it opens the local email client. If I remove the returnstatement in the onclickattribute like this:

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

<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 :-)

任何帮助将不胜感激:-)

采纳答案by Tomasz Nurkiewicz

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.

您需要服务器端支持来实现这一点。基本上,您的表单应该发布(AJAX 也可以)到服务器,并且该服务器应该通过 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(即来自用户计算机)发送电子邮件,用户仍然必须连接到某个 SMTP 服务器(如 gmail.com),提供 SMTP 凭据等。这通常在服务器端(在您的应用程序中),它知道这些凭据。

回答by Quentin

You cannot cause the user's browser to send email silently. That would be a horrible security problem as any website could use their system as a spam relay and/or harvest their email address.

您不能让用户的浏览器静默发送电子邮件。这将是一个可怕的安全问题,因为任何网站都可以将其系统用作垃圾邮件中继和/或获取其电子邮件地址。

You need to make an HTTP request to a server side process (written in the language of your choice) which sends the mail from your server.

您需要向从您的服务器发送邮件的服务器端进程(以您选择的语言编写)发出 HTTP 请求。

回答by rahulroy9202

Directly From Client

直接来自客户



Send an email using only JavaScript

仅使用 JavaScript 发送电子邮件

in short: 
1. register for Mandrill to get an API key
2. load jQuery
3. use $.ajax to send an email

Like this -

像这样 -

function sendMail() {
    $.ajax({
      type: 'POST',
      url: 'https://mandrillapp.com/api/1.0/messages/send.json',
      data: {
        'key': 'YOUR API KEY HERE',
        'message': {
          'from_email': '[email protected]',
          'to': [
              {
                'email': '[email protected]',
                'name': 'RECIPIENT NAME (OPTIONAL)',
                'type': 'to'
              }
            ],
          'autotext': 'true',
          'subject': 'YOUR SUBJECT HERE!',
          'html': 'YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!'
        }
      }
     }).done(function(response) {
       console.log(response); // if you're into that sorta thing
     });
}

https://medium.com/design-startups/b53319616782

https://medium.com/design-startups/b53319616782

Note: Keep in mind that your API key is visible to anyone, so any malicious user may use your key to send out emails that can eat up your quota.

注意:请记住,任何人都可以看到您的 API 密钥,因此任何恶意用户都可能使用您的密钥发送电子邮件,这可能会耗尽您的配额。



Indirect via Your Server- secure

通过您的服务器间接- 安全



To overcome the above vulnerability, you can modify your own server to send the email after session based authentication.

为了克服上述漏洞,您可以修改自己的服务器以在基于会话的身份验证后发送电子邮件。

node.js- https://www.npmjs.org/package/node-mandrill

node.js- https://www.npmjs.org/package/node-mandrill

var mandrill = require('node-mandrill')('<your API Key>'); 

function sendEmail ( _name, _email, _subject, _message) {
    mandrill('/messages/send', {
        message: {
            to: [{email: _email , name: _name}],
            from_email: '[email protected]',
            subject: _subject,
            text: _message
        }
    }, function(error, response){
        if (error) console.log( error );
        else console.log(response);
    });
}

// define your own email api which points to your server.

app.post( '/api/sendemail/', function(req, res){

    var _name = req.body.name;
    var _email = req.body.email;
    var _subject = req.body.subject;
    var _messsage = req.body.message;

    //implement your spam protection or checks. 

    sendEmail ( _name, _email, _subject, _message );

});

and then use use $.ajax on client to call your email API.

然后在客户端使用 $.ajax 调用您的电子邮件 API。

回答by Ads

You can't do it with client side script only... you could make an AJAX call to some server side code that will send an email...

您不能仅使用客户端脚本来执行此操作...您可以对某些将发送电子邮件的服务器端代码进行 AJAX 调用...

回答by Louis

I think you can use smtpjs.com

我想你可以使用smtpjs.com

回答by PlantTheIdea

There needs to be some type of backend framework to send the email. This can be done via PHP/ASP.NET, or with the local mail client. If you want the user to see nothing, the best way is to tap into those by an AJAX call to a separate send_email file.

需要某种类型的后端框架来发送电子邮件。这可以通过 PHP/ASP.NET 或本地邮件客户端来完成。如果您不希望用户看到任何内容,最好的方法是通过 AJAX 调用对单独的 send_email 文件进行访问。

回答by Tiago Salzmann

You need to do it directly on a server. But a better way is using PHP. I have heard that PHP has a special code that can send e-mail directly without opening the mail client.

您需要直接在服务器上执行此操作。但更好的方法是使用 PHP。听说PHP有一个特殊的代码,可以不用打开邮件客户端直接发送邮件。