间接通过您的服务器 - 调用 3rd Party API - 安全且推荐
您的服务器可以在正确的身份验证和授权后调用 3rd Party API。API 密钥不会向客户端公开。
node.js   -   https://www.npmjs.org/package/node-mandrill
const 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){
            
    let _name = req.body.name;
    let _email = req.body.email;
    let _subject = req.body.subject;
    let _messsage = req.body.message;
    //implement your spam protection or checks. 
    sendEmail ( _name, _email, _subject, _message );
});
然后在客户端使用 $.ajax 调用您的电子邮件 API。
直接从客户端 - 调用 3rd Party API - 不推荐
仅使用 JavaScript 发送电子邮件
简而言之:
- 注册 Mandrill 以获取 API 密钥
 
- 加载jQuery
 
- 使用 $.ajax 发送电子邮件
 
像这样 -
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
注意:请记住,任何人都可以看到您的 API 密钥,因此任何恶意用户都可能使用您的密钥发送电子邮件,这可能会耗尽您的配额。