简体   繁体   中英

sending an email using JavaScript

I'm trying to come up a way to send email via JavaScript , and after some search I found this tutorial exampling how to do it using Mandrill . Thus, I went ahead trying the API, and I'm not having success so far.

<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’: ‘SOMEONE@EMAIL.COM’,
                ‘to’: [
                    {
                        ‘email’: ‘MY@EMAIL.COM’,
                        ‘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>

Moreover, I see firebug reporting me the following error:

SyntaxError: missing : after property id


‘key’: ‘MY_KEY€™,

When I look at the page, I see:

function validateMyForm()
{
    $.ajax({
        type: 'POST',
        url: 'https://mandrillapp.com/api/1.0/messages/send.json',
        data: {
              ‘key’: ‘MY_KEY€™,
              ‘message’: {
                ‘from_email’: ‘MY_EMAIL@EMAIL.COM€™,
                ‘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
        });
}

I have no idea why Firefox is doing that ... So there are two problems I'm asking in this question:

  • Why is Firefox returning those weird symbols?
  • How can I make the validateMyForm() function to send me an email?

You're using funky quotes: ' . Change them to single quotes: ' .

Why is Firefox returning those weird symbols?

Ensure that you are using the right encoding to read the file - It is look like you saved the file with an encoding that FireFox can't infer.

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

You're using funky quotes: '. Replace them with single quotes: ', example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM