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

时间:2021-09-03 07:01:58

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:hello@domain.com?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 return statement in the onclick attribute like this:

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

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

任何帮助将得到真诚的赞赏:-)

7 个解决方案

#1


13  

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凭据等。这通常是在服务器端(在您的应用程序中),它知道这些凭据。

#2


14  

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请求,该进程从您的服务器发送邮件。

#3


6  

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': 'YOUR@EMAIL.HERE',
          'to': [
              {
                'email': 'RECIPIENT@EMAIL.HERE',
                '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: 'noreply@yourdomain.com',
            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。

#4


3  

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调用...

#5


1  

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文件。

#6


0  

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有一个特殊的代码,可以直接发送电子邮件而无需打开邮件客户端。

#7


0  

I think you can use smtpjs.com

我想你可以使用smtpjs.com

#1


13  

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凭据等。这通常是在服务器端(在您的应用程序中),它知道这些凭据。

#2


14  

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请求,该进程从您的服务器发送邮件。

#3


6  

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': 'YOUR@EMAIL.HERE',
          'to': [
              {
                'email': 'RECIPIENT@EMAIL.HERE',
                '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: 'noreply@yourdomain.com',
            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。

#4


3  

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调用...

#5


1  

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文件。

#6


0  

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有一个特殊的代码,可以直接发送电子邮件而无需打开邮件客户端。

#7


0  

I think you can use smtpjs.com

我想你可以使用smtpjs.com