如何让联系我们提交按钮发送电子邮件到我的电子邮件?

时间:2017-01-25 10:41:14

标签: javascript html css twitter-bootstrap-3 contact-form

我很新,试图在我的网站上使用联系我们表单,将联系我们表格查询发送到我的电子邮箱。我使用模板,我不知道如何将这些表单查询发送到我的电子邮件。

如何使用我的静态网站而不是服务器端语言来联系我们表格,因为我想让它与html和javascript一起使用,例如人员在表单中输入姓名,电子邮件和消息,然后点击提交时按钮然后我将人员数据收到我的电子邮件帐户

这些是代码。

<div class="container">
            <div class="row">
                <div class="col-lg-12 text-xs-center">
                    <h2 class="section-heading">Contact Us</h2>
                    <h3 class="section-subheading text-muted">Use ths form to contact us.</h3>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <form name="sentMessage" id="contactForm" novalidate>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
                                    <p class="help-block text-danger"></p>
                                </div>
                                <div class="form-group">
                                    <input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email physics.">
                                    <p class="help-block text-danger"></p>
                                </div>
                                <div class="form-group">
                                    <input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
                                    <p class="help-block text-danger"></p>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
                                    <p class="help-block text-danger"></p>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="col-lg-12 text-xs-center">
                                <div id="success"></div>
                                <button type="submit" class="btn btn-xl">Send Message</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

表单的Javascript代码

   $(function() 
{ $("#contactForm input,#contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});
    $('#name').focus(function() {
    $('#success').html('');
});

1 个答案:

答案 0 :(得分:-1)

可以使用$_POST

在您的php代码中访问所有表单值
<?php
$recipient = 'your@email.com';
//construct your email text here
$message = 'Name: '.$_POST['name'].'<br/>Email: '.$_POST['email'].'Phone: '.$_POST['phone'].'<br/>Message: '.$_POST['message'];
mail($recipient, 'MAIL SUBJECT', $message);
?>

mail()执行发送邮件的任务。您可以在http://php.net/manual/en/function.mail.php了解更多相关信息。

您可以在服务器的公共文件夹的主根目录中上传php脚本。但是,这意味着您将ajax调用的网址更改为&#39; contact_me.php&#39;如果使用javascript的页面与php脚本在同一个文件夹中