2015-05-01 40 views
5

ho seguito this guida su come inviare un'email utilizzando JavaScript con Mandrillo, ma sto ricevendo questo errore nel mio console: 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.Come inviare un messaggio di posta elettronica con Mandrill utilizzando JavaScript?

Ecco il mio codice:

$('#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); 
    }); 
}); 

Che cosa sto facendo di sbagliato?

+0

Tenete a mente che la vostra chiave API è visibile a chiunque, in modo che qualsiasi utente malintenzionato potrebbe utilizzare la chiave per l'invio di messaggi di posta elettronica in grado di mangiare il vostro quota/spam tante persone che il tuo account verrà bloccato – baao

risposta

4

Piuttosto che fare una richiesta POST, è necessario includere il Mandrill API in un tag <script> nel <head>:

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

È quindi possibile accedere nel file 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 
     } 
    }); 
} 

Tuttavia, nota che questo esporrà la tua API al pubblico, in quanto sarà accessibile dal lato client utilizzando gli strumenti di sviluppo. Questo può aprirti alle vulnerabilità di phishing e qualcuno potrebbe abusare della tua chiave.

Vorrei anche dare un'occhiata allo full Mandrill docs per send.

0

Non è possibile accedere all'API Mandrill da un browser, in base alla progettazione, per motivi di sicurezza. Guarda come la tua chiave API sarà esposta a chiunque visiti il ​​tuo sito web?

Dovrai effettuare una richiesta AJAX sul tuo server e quindi chiamare l'API Mandrill dal codice dell'applicazione back-end.

+2

purtroppo può accedervi e molte persone lo usano senza alcuna conseguenza. – GorillaApe

4

Fantastico, ecco una soluzione che utilizza Jquery per inviare il modulo. :)

Stavo tentando di creare un modulo di contatto sul mio sito web usando jquery + mandrill. Non ho trovato utile la risposta di cui sopra (nessun reato) Quindi spero che la mia risposta possa chiarirlo. Ho ricevuto aiuto dal mio buon amico & Sviluppatore Thomas Lane @ d00by.

Si prega di vedere sotto il mio modulo. E sotto il mio modulo il javascript.

  • creare la forma
  • Usare Ajax per presentare il modulo
  • return false funzione di chiamata
  • su Invia.

Per utilizzare mandrill è necessaria una chiave 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; 
} 
+0

È disponibile per ora? – Hiroto

+0

Non capisco la tua domanda ... –