2014-04-27 122 views
-1

我想通過JavaScript想出一種方式發送電子郵件,經過一番搜索後,我發現這tutorial exampling如何使用Mandrill做到這一點。 因此,我繼續嘗試API,到目前爲止我還沒有取得成功。使用JavaScript發送電子郵件

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript"> 
function validateMyForm() 
{ 
    $.ajax({ 
     type: 'POST', 
     url: 'https://mandrillapp.com/api/1.0/messages/send.json', 
     data: { 
       ‘key’: ‘MY KEY’, 
       ‘message’: { 
       ‘from_email’: ‘[email protected]’, 
       ‘to’: [ 
        { 
         ‘email’: ‘[email protected]’, 
         ‘name’: ‘MYSELF’, 
         ‘type’: ‘to’ 
        } 
       ], 
       ‘autotext’: ‘true’, 
       ‘subject’: ‘Hello World’, 
       ‘html’: ‘YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!’ 
      } 
     } 
     }).done(function(response) { 
      console.log(response); // if you're into that sorta thing 
     }); 
} 
</script> 
</head> 
<body> 
<form method="post" onsubmit="return validateMyForm();"> 
    Email: <input name="email" type="text" /><br /> 
    Subject: <input name="subject" type="text" /><br /> 
    Message:<br /> 
    <textarea name="comment" rows="15" cols="40"></textarea><br /> 
    <input id="sendmail" type="submit" value="Submit"/> 
    </form> 
</body> 
</html> 

而且,我看到firebug報告我下面的錯誤:

SyntaxError: missing : after property id 


‘key’: ‘MY_KEY€™, 

當我看網頁,我看到:

function validateMyForm() 
{ 
    $.ajax({ 
     type: 'POST', 
     url: 'https://mandrillapp.com/api/1.0/messages/send.json', 
     data: { 
       ‘key’: ‘MY_KEY€™, 
       ‘message’: { 
       ‘from_email’: ‘[email protected]€™, 
       ‘to’: [ 
        { 
         ‘email’: ‘SOMEONE'S EMAIL’, 
         ‘name’: ‘MY NAME’, 
         ‘type’: ‘to’ 
        } 
       ], 
       ‘autotext’: ‘true’, 
       ‘subject’: ‘Hello World’, 
       ‘html’: ‘YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!’ 
      } 
     } 
     }).done(function(response) { 
      console.log(response); // if you're into that sorta thing 
     }); 
} 

我不知道爲什麼Firefox做那......所以我在這個問題中有兩個問題:

  • 爲什麼Firefox會返回那些奇怪的符號?
  • 如何讓validateMyForm()函數向我發送電子郵件?
+0

我會考慮編碼問題。 –

回答

1

Why is Firefox returning those weird symbols?

確保您使用的是正確的編碼讀取文件 - 它看起來像你用火狐不能推斷編碼保存的文件。

How can I make the validateMyForm() function to send me an email?

您正在使用時髦的引號:'。替換爲單引號:',例如:

data: { 
     'key': 'MY KEY', 
     'message': { 
... 
+0

加利福尼亞州8AM ...謝謝 – cybertextron

+2

@philippe對不起? –

5

您正在使用時髦的引號:。將它們更改爲單引號:'