2014-04-27 123 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

您正在使用时髦的引号:。将它们更改为单引号:'