我已遵循本指南,了解如何通过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': '[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请求,不如在您的标签中包含Mandrill API:<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公开,因为可以使用开发工具从客户端进行访问。这可能使您容易遭受网上诱骗漏洞的攻击,并且有人可能会滥用您的密钥。
我也想看看在全山魈文档的send
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句