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

Posted

技术标签:

【中文标题】如何使用 JavaScript 使用 Mandrill 发送电子邮件?【英文标题】:How to send an email with Mandrill using JavaScript? 【发布时间】:2015-07-11 18:39: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': '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);
  );
);

我做错了什么?

【问题讨论】:

请记住,您的 API 密钥对任何人都是可见的,因此任何恶意用户都可能使用您的密钥发送电子邮件,这些电子邮件可能会占用您的配额/垃圾邮件,以至于您的帐户将被阻止 【参考方案1】:

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

<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 公开,因为可以使用开发工具从客户端访问它。这可能会使您面临网络钓鱼漏洞,并且有人可能会滥用您的密钥。

我还想看看full Mandrill docs 的send

【讨论】:

这是一个巨大的安全问题。你永远不应该公开你的 API 密钥。【参考方案2】:

酷,这是使用 Jquery 发送表单的解决方案。 :)

我试图使用 jquery + mandrill 在我的网站上制作联系表格。我没有发现上述答案有帮助(没有冒犯兄弟)所以我希望我的答案可以解决这个问题。我从我的好朋友兼开发人员 Thomas Lane @d00by 那里得到了一些帮助。

请看下面我的表格。在我的表单下方是 javascript。

创建表单 使用ajax提交表单 返回假 提交时调用函数。

为了使用 mandrill,您需要一个 API 密钥。

<form id="contact-form" method="POST" action="" onsubmit="return   submitContactForm();" class="margin-top" role="form">

                <div class="row">
                    <div class="form-group">
                        <i class="fa fa-check-circle fa-2 check" aria-hidden="true"></i>
                        <input id="form_name" type="text" name="name" class="form-control" placeholder="Full Name" required="required" data-error="Firstname is required.">
                    </div>
                </div>

                <div class="row">
                    <div class="form-group">
                        <i class="fa fa-check-circle fa-2 check" aria-hidden="true"></i>
                        <input id="form_email" type="text" name="name" class="form-control" placeholder="Email" required="required" data-error="E-mail is required.">
                    </div>
                </div>

                <div class="row">
                    <div class="form-group">
                        <i class="fa fa-check-circle fa-2 check" aria-hidden="true"></i>
                        <input id="form_phone" type="text" name="name" class="form-control" placeholder="Phone" required="required" data-error="Phone Number is required.">
                    </div>
                </div>

                <div class="row">
                    <div class="form-group">
                        <i class="fa fa-check-circle fa-2 check" aria-hidden="true"></i>
                        <textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="2" required="required" data-error="Please,leave us a message."></textarea>
                    </div>
                </div>
                     <button class="btn btn-primary text-center submit" type="submit">Send</button>
            </form>



     function submitContactForm() 
         /*var name = $('#form_name').val();
  var email = $('#form_email').val();
  var phone = $('#form_phone').val();
  var message =$('#form_message').val();*/

//this is the html template. You can also do it as used above. But is much simpler done as below

 var htmlMessage = 'Contact form<br/>' +
    'Name: '+$('#form_name').val()+'<br/>'+
    'EMail: '+$('#form_email').val()+'<br/>'+
    'Message<br/>'+
    $('#form_message').val();

//submit the form using ajax
  $.ajax(
  type: "POST",
  url: "https://mandrillapp.com/api/1.0/messages/send.json",
  data: 
    "key": 'Your API key here',
    "message": 
      "from_email": 'your email',
      "to": [
        
          "email": 'form email',
          "name": 'name',
          "type": 'to'
        
      ],
      "subject": 'Subject',
      "html": htmlMessage
    
  
);

  return false;

【讨论】:

现在可以使用吗? 这还不暴露你的api密钥吗?【参考方案3】:

您无法从浏览器访问 Mandrill API - 出于安全原因,这是设计使然。了解您的 API 密钥将如何向访问您网站的任何人公开?

您需要向服务器发出 AJAX 请求,然后从后端应用程序代码调用 Mandrill API。

【讨论】:

不幸的是,您可以访问它并且许多人使用它而没有任何后果。

以上是关于如何使用 JavaScript 使用 Mandrill 发送电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用本地存储使用javascript登录

如何使用 JavaScript 创建会话?

javascript 如何使用JavaScript连接REST API

如何使用命令行美化 JavaScript 代码?

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

如何使用按钮使用 JavaScript 对数组进行计数?