如何从网站发送联系表格到邮件

时间:2015-07-06 09:13:22

标签: php forms email

我在我的网站上有联系表格,当用户填写表格时,它应该将带有用户内容的表格发送到我的邮箱。我试过这个显示空白页。

这是我的表格:

<form id="contact_form" action="mail.php" method="post" class="form-horizontal">
    <div class="form-group">
        <label class="col-md-3 control-label">Name:</label>
        <div class="col-md-5">
            <input type="email" class="form-control" name="name" />
        </div>
     </div>
     <div class="form-group">
         <label class="col-md-3 control-label">E-mail:</label>
         <div class="col-md-5">
             <input type="email" class="form-control" name="email" />
         </div>
     </div>
     <div class="form-group">
         <label class="col-md-3 control-label">Mobile Number:</label>
         <div class="col-md-5">
             <input type="email" class="form-control" name="mob" />
         </div>
     </div>
     <div class="form-group">
         <label class="col-md-3 control-label">Requirement</label>
         <div class="col-md-5">
             <textarea class="form-control" name="mess" cols="30" rows="10" placeholder="Your Message"></textarea>
         </div>
     </div>
     <div class="form-group">
         <div class="col-md-5 col-md-offset-3">
             <button type="submit" class="btn btn-default">GET A QUOTE</button>
          </div>
    </div>
</form>

和PHP邮件程序:我试过这个,执行时显示空白

<?php 
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mob = $_POST['mob'];
    $mess = $_POST['mess'];
    $subject = 'Message from website';
    $to = 'mailus@pebibytetech.in';
    $headers="From: {$email}\r\nReply-To: {$email}";
    mail($to,$subject,$message,$headers);
    $success = "Thank you! You're email has been sent.";
}
?>

4 个答案:

答案 0 :(得分:1)

PHPSimpleMail

简单的PHP邮件程序比内置的PHP函数更好。 查看this link了解更多信息。

您还需要根据需要使用输入类型。 如果您想获得电子邮件,请使用

For Email: <input type="email" name="email">
For Phone: <input type="tel" name="phonenumber">
For name: <input type="text" name="FirstName">

请勿在所有输入字段中使用电子邮件类型。

答案 1 :(得分:0)

mail()成功时,您没有显示任何内容。

   if(mail($to,$subject,$message,$headers))
         echo $success = "Thank you! You're email has been sent.";
    else
         echo "Failed!!";

您还没有定义$message。将$mess更改为$message

答案 2 :(得分:0)

我认为您缺少enctype="multipart/form-data"表单属性。研究以下链接。这非常有用。 Contact Form Data Email Link

答案 3 :(得分:0)

在您的表单中(将其替换为您的代码)

<form action="#" method="post" class="form-horizontal">
    <div class="form-group">
        <label class="col-md-3 control-label">Name:</label>
        <div class="col-md-5">
            <input type="text" class="form-control" name="name" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">E-mail:</label>
        <div class="col-md-5">
            <input type="email" class="form-control" name="email" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">Mobile Number:</label>
        <div class="col-md-5">
            <input type="number" class="form-control" name="mob" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-3 control-label">Requirement</label>
        <div class="col-md-5">
            <textarea class="form-control" name="mess" cols="30" rows="10" placeholder="Your Message"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-5 col-md-offset-3">
            <input type="submit" name="submit" class="btn btn-default" value="GET A QUOTE">
        </div>
    </div>
</form>

然后在表单下方,添加此php代码

<?php
    if(isset($_POST['submit']))
    {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $mob = $_POST['mob'];
        $mess = $_POST['mess'];

        if(empty($name)||empty($email)||empty($mob))
        {
            ?>
            <script>
                alert('Name, Email, Mobile is empty');
            </script>
        <?php
        }
        else
        {
            // Method 01 (Normal method)
            $to = 'mailus@pebibytetech.in';
            $subject = 'Message from website';

            $headers="From:".$email;

            $message = 'Customer Name :'.$name;
            $message .= '<br>Customer Mobile :'.$mob;
            $message .= '<br>Customer Email :'.$email;
            $message .= '<br>Customer Message :'.$mess;

            $mail = mail($to,$subject,$message,$headers);
            if(isset($mail))
            {
                ?>
                <script>
                    alert('Mail Sent');
                </script>
            <?php
            }
            else
            {

                ?>
                <script>
                    alert('Mail Sending Failed');
                </script>
            <?php
            }

            //method 02, Using table

            $to = 'mailus@pebibytetech.in';
            $subject = 'Message from website';

            $headers="From:".$email;

            $message = '<html><body>';
            $message .= '<table rules="all" style="border: 1px solid #eee; width: 850px" cellpadding="10">';
            $message .= "<tr><td width='25%'><strong>Name</strong> </td><td width='75%'>".strip_tags($_POST['name'])."</td></tr>";
            $message .= "<tr><td><strong>Email</strong> </td><td>" .strip_tags($_POST['email']) . "</td></tr>";
            $message .= "<tr><td><strong>Mobile</strong> </td><td>" .strip_tags($_POST['mob']) . "</td></tr>";
            $message .= "<tr><td><strong>Message</strong> </td><td>" . strip_tags($_POST['mess']). "</td></tr>";
            $message .= "</table>";
            $message .= "</body></html>";

            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            $headers .="From:".$email;//can add  $headers="Cc:".$email;

            $mail = mail($to,$subject,$message,$headers);

            if(isset($mail))
            {
                ?>
                <script>
                    alert('Mail Sent');
                </script>
            <?php
            }
            else
            {

                ?>
                <script>
                    alert('Mail Sending Failed');
                </script>
            <?php
            }
        }
    }
?>