【问题标题】:how to send contact form to mail from website [duplicate]如何将联系表从网站发送到邮件[重复]
【发布时间】:2015-07-06 09:13:22
【问题描述】:

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

这是我的表格:

<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.";
}
?>

【问题讨论】:

    标签: php forms email


    【解决方案1】:

    PHPSimpleMail

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

    您还需要根据需要使用输入类型。 就像如果你想获得电子邮件然后使用

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

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

    【讨论】:

      【解决方案2】:

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

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

      另外你还没有定义$message。将$mess 更改为$message

      【讨论】:

      • 但是我在 wamp 中使用的邮件没有到达
      • 您可以尝试将其发送到 gmail 地址吗?
      • 是的,我正在发送到 gmail,但仍然再次显示空白页。
      • 在您的表单中尝试&lt;button name='submit'&gt;
      • 警告:mail():SMTP 服务器响应:550 地址无效。
      【解决方案3】:

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

      【讨论】:

      • 你确定他需要那个吗?启发自己here
      • 哦,我真的很抱歉错误的答案......@a3ey
      • 世界不会被那些作恶的人毁灭,而是被那些旁观而无所作为的人毁灭。 ;)
      【解决方案4】:

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

      <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
                  }
              }
          }
      ?>
      

      【讨论】:

      • 你检查答案了吗??
      猜你喜欢
      • 2022-12-04
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 2012-05-02
      • 2016-02-10
      • 1970-01-01
      • 2021-10-25
      • 2021-06-19
      相关资源
      最近更新 更多