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

Ral*_*thy 6 javascript email jquery mandrill

我已经按照指南关于如何使用Mandrill使用JavaScript发送电子邮件,但我在控制台中收到此错误: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': 'test@hotmail.com',
        'to': [{
          'email': 'test@gmail.com',
          'name': 'RECIPIENT NAME (OPTIONAL)',
          'type': 'to'
        }],
        'autotext': 'true',
        'subject': 'test',
        'html': 'test'
      }
    }
  }).done(function(response) {
    console.log(response);
  });
});
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Ast*_*oCB 5

您应该在以下标签中包含Mandrill API,而不是发出POST请求:<script><head>

<script type="text/javascript" src="path_to_locally_stored_copy_of_mandrill_API"></script>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在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
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

请注意,这会将您的API暴露给公众,因为可以使用开发工具从客户端访问它.这可以打开您的网络钓鱼漏洞,有人可能会滥用您的密钥.

我也想看看在全山魈文档send.