如何使用 JavaScript 通过 Mandrill 发送电子邮件?

2023-11-24

我已关注this有关如何使用 JavaScript 和 Mandrill 发送电子邮件的指南,但我在控制台中收到此错误:Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mandrillapp.com/api/1.0/messages/send.json. This can be fixed by moving the resource to the same domain or enabling CORS.

这是我的代码:

$('#submitEmail').click(function() {
  $.ajax({
    type: "POST",
    url: "https://mandrillapp.com/api/1.0/messages/send.json",
    data: {
      'key': 'my_api_key',
      'message': {
        'from_email': '[email protected]',
        'to': [{
          'email': '[email protected]',
          'name': 'RECIPIENT NAME (OPTIONAL)',
          'type': 'to'
        }],
        'autotext': 'true',
        'subject': 'test',
        'html': 'test'
      }
    }
  }).done(function(response) {
    console.log(response);
  });
});

我究竟做错了什么?


您应该包含以下内容,而不是发出 POST 请求山魈API in a <script>标签在你的<head>:

<script type="text/javascript" src="path_to_locally_stored_copy_of_mandrill_API"></script>

然后您可以在 JS 文件中访问它:

var m = new mandrill.Mandrill('your_api_key'); // This will be public

function sendTheMail(){
    m.messages.send({
        "message": {
            "from_email": "your_email_address",
            "from_name": "your_name",
            "to":[{"email": "someone's_email_address", "name": "someone's_name"}], // Array of recipients
            "subject": "optional_subject_line",
            "text": "Text to be sent in the body" // Alternatively, use the "html" key to send HTML emails rather than plaintext
        }
    });
}

然而,请注意,这会将您的 API 公开给公众,因为可以使用开发工具从客户端访问它。这可能会让您面临网络钓鱼漏洞,并且有人可能会滥用您的密钥。

我也想看看完整山魈文档 for send.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 JavaScript 通过 Mandrill 发送电子邮件? 的相关文章

随机推荐