如何使用mandrill通过电子邮件发送表单信息

时间:2014-10-02 06:06:15

标签: javascript html email mandrill

我创建了一个网站,其中包含一个包含多个字段的表单,并且我试图让按钮实际向我发送输入的信息。我一直在尝试学习代码学院的Mandrill API,但我可能会对其他完成相同任务的方法持开放态度。这是我到目前为止所拥有的:

<div id="contactUs">
    <div class="container">

    <div class="row form-group">

        <div class="col-sm-6">
            <input class="form-control" id="email" placeholder="Your Email Address">
        </div><!-- end col-sm-6 -->

        <div class="col-sm-6">
            <input class="form-control" id="organization" placeholder="Organization or Company Name">
        </div><!-- end col-sm-6 -->

    </div><!-- end form-group -->

    <div class="row form-group">

        <div class="col-sm-6">
            <input class="form-control" id="name" placeholder="Your Name">
        </div><!-- end col-sm-6 -->

        <div class="col-sm-6">
            <input class="form-control" id="phone" placeholder="Phone Number">
        </div><!-- end col-sm-6 -->

    </div><!-- end form-group -->

    <div class="row form-group">

        <div class="col-sm-12">
            <button onclick="sendTheMail(); return false;" class="btn btn-default pull-right" id="alertMe">Contact Us</button>
        </div><!-- end col-sm-6 -->

    </div><!-- end form-group -->
</div><!-- end container -->
</div><!-- end contactUs -->

和JS ..

// create a variable for the API call parameters
var params = {
    "message": {
        "from_email":"staticallyanyone@anywhere.net",
        "to":[{"email":"myemail@hotmail.com"}],
        "subject": "An Inquiry from your website",
        "text": "Static text.. but how do i link the information entered in the fields to show up here?"
    }
};

function sendTheMail() {
// Send the email!

    m.messages.send(params);
}

必须有一种方法可以将字段中的信息链接到JS中吗?我还是一个新手......任何提高js的技巧..请告诉我。

1 个答案:

答案 0 :(得分:1)

您可以按ID获取元素并从元素中获取值。下面的代码应该有效。我相信Mandrill允许html消息,所以如果你想要你可以在emailBody变量中修改html,以制作更具吸引力的电子邮件。

function sendTheMail() {
var email = document.getElementById('email').value;
var name = document.getElementById('name').value;
var phone = document.getElementById('phone').value;
var organization = document.getElementById('organization').value;
var emailBody = "<b>Email: </b>" + email + "<br><b>Name: </b>" + name + "<br><b>Phone: </b>" + phone + "<br><b>Organization: </b>" + organization;
var params = {
"message": {
    "from_email":"staticallyanyone@anywhere.net",
    "to":[{"email":"myemail@hotmail.com"}],
    "subject": "An Inquiry from your website",
    "text": emailBody
    }
};
m.messages.send(params);

}