HTML表单发送电子邮件

时间:2016-01-19 12:01:48

标签: javascript php html

我不是开发人员,但我正在为我的婚礼建立一个RSVP网站。所以我有一个HTML表单。我希望每次有人填写rsvp表单时都能收到电子邮件。我知道它可以通过PHP或js完成,但不知道如何做到这一点。这是我的代码

<div id="rsvp" class="text-center" data-scroll-reveal>
            <div class="heading">
                <h2>RSVP</h2>
                <p><span></span><i class="fa fa-heart"></i><span></span></p>
            </div>
            <form role="form">
                <div class="row">
                    <div class="form-group col-xs-12">
                        <label for="input-name">Name</label>
                        <input type="text" class="form-control" id="input-name" placeholder="John Doe">
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-xs-12">
                        <label for="input-email">Email</label>
                        <input type="email" class="form-control" id="input-email" placeholder="name@domain.com">
                    </div>
                </div>
                <div class="row">
                    <div class="form-group col-xs-6">
                        <label for="select-guests">Guests</label>
                        <select class="form-control" id="select-guests">
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                        </select>
                    </div>
                    <div class="form-group col-xs-6">
                        <label for="select-attending">I am attending...</label>
                        <select class="form-control" id="select-attending">
                            <option>All Events</option>
                            <option>Ceremony</option>
                            <option>Reception</option>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <button type="submit" class="btn btn-lg">I'm attending</button>
                </div>
            </form>
        </div>

3 个答案:

答案 0 :(得分:0)

您可以找到电子邮件发件人php脚本,但您也可以考虑以下内容:http://www.123contactform.com/

答案 1 :(得分:0)

您可以使用<form role="form" action="MAILTO:someone@example.com" method="post" enctype="text/plain">代替<form role="form">,而不是here

但是只打开默认邮件程序并插入表单内容。用户必须自己发送。

如果这不是您想要的,则必须使用创建电子邮件的服务器端脚本。

答案 2 :(得分:0)

您可以使用MAILTO:,但它有很多缺点。 如果服务器支持,我更喜欢使用PHP mail函数。 而不是<button type="submit" class="btn btn-lg">I'm attending</button>使用<input type="submit" value="I'm attending" name="submit" class="btn btn-lg">

以下代码正常(我多次测试过)。

<!DOCTYPE html>
<html>
<body>
<div id="rsvp" class="text-center" data-scroll-reveal>
        <div class="heading">
            <h2>RSVP</h2>
            <p><span></span><i class="fa fa-heart"></i><span></span></p>
        </div>
        <form role="form" method="POST" action="">
            <div class="row">
                <div class="form-group col-xs-12">
                    <label for="input-name">Name</label>
                    <input type="text" name="name" class="form-control" id="input-name" placeholder="John Doe" required>
                </div>
            </div>
            <div class="row">
                <div class="form-group col-xs-12">
                    <label for="input-email">Email</label>
                    <input type="email" name="email" class="form-control" id="input-email" placeholder="name@domain.com" required>
                </div>
            </div>
            <div class="row">
                <div class="form-group col-xs-6">
                    <label for="select-guests">Guests</label>
                    <select class="form-control" name="check" id="select-guests">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                    </select>
                </div>
                <div class="form-group col-xs-6">
                    <label for="select-attending">I am attending...</label>
                    <select class="form-control" name="check2" id="select-attending">
                        <option>All Events</option>
                        <option>Ceremony</option>
                        <option>Reception</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <input type="submit" value="I'm attending" name="submit" class="btn btn-lg">
            </div>
        </form>
    </div>
    </body>
    </html>
<?php 
if(isset($_POST['submit'])){
    $to= "user@example.com"; 
    $from = $_POST['email']; 
    $name = $_POST['name']; 
    $subject = "Invitation"; //Write whatever you want here
    $message = $name . " is with " . $_POST['check'] . " person(s) and attends " . $_POST['check2'];
    $headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('location: thank-you.html'); //redirects the user to another page that you made if the mail was send succesfully
} 
?>