【问题标题】:How to make the contact form submit responses to email?如何使联系表格提交对电子邮件的回复?
【发布时间】:2021-05-08 05:39:49
【问题描述】:

我想在用户点击submit 按钮时制作联系表单以提交回复。当用户点击submit 按钮时,它还应该将他们重定向到thankyou.html,我已经完成了。但是回复也应该通过电子邮件发送给我。我被困在那部分,无法在 PHP 文件中找到它。

PHP 文件

$errors = '';
$myemail = 'm.hussainomer03@gmail.com'; // Put Your email address here.
if (empty($_POST['name']) ||
    empty($_POST['email']) ||
    empty($_POST['message'])) {
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $email_address)) {
    $errors .= "\n Error: Invalid email address";
}

if (empty($errors)) {
    $to = $myemail;
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. " .
        " Here are the details:\n Name: $name \n " .
        "Email: $email_address\n Message \n $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    mail($to, $email_subject, $email_body, $headers);
    
    // Redirect to the 'thank you' page.
    header('Location: contact-form-thank-you.html');
}

其余代码

input[type=text], [type=email], select, textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid #ccc;
    margin-top: 6px;
    margin-bottom: 16px;
    resize: vertical;
}

input[type=submit] {
    background-color: rgb(62, 3, 179);
    color: white;
    padding: 12px 20px;
    border: none;
    cursor: pointer;
}

input[type=submit]:hover {
    background-color: deeppink;
}

.contactform {
    position: relative;
    border-radius: 50px;
    background-color: #f2f2f2;
    padding: 5px;
    z-index: 2;
    display: block;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: auto;
    margin-top: 1%;
    width: 100%;
    animation-name: gradient;
    animation-duration: 3s;
    animation-iteration-count: infinite;
}

.contactform:hover {
    animation-name: gradient;
    animation-duration: 15s;
    animation-iteration-count: infinite;
}


.column {
    float: center;
    width: 50%;
    margin-top: 6px;
    padding: 20px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 40%;

}

.row:after {
    content: "";
    display: table;
    clear: both;
}

@media screen and (max-width: 600px) {
    .column, input[type=submit] {
        width: auto;
        margin-top: 0;
    }
}
<section id="contact">
  <div class="container" data-aos="fade-up">
    <div class="contactform">
      <div style="text-align:center">
        <div class="section-title">
          <h2><br/>Get In Touch</h2>
        </div>
        <p>Feel Free To Reach Out To Me Through This Form! </p>
      </div>
      <div class="row">
        <div class="column">
          <form name="myform" action="contact.php" method="POST">
            <label for="firstname">First Name</label>
            <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>

            <label for="lastname">Last Name</label>
            <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>

            <label for="email">Email:</label>
            <input type="email" id="email" name="email" placeholder="Your Email.." required>

            <label for="subject">Subject</label>
            <textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
            <input type="submit" value="Submit">
          </form>
        </div>
      </div>
    </div>
  </div>
</section>

我之前曾尝试重命名 php 文件中的类/对象,但这也没有成功。

【问题讨论】:

  • 这应该回答你的问题:stackoverflow.com/questions/24644436/…
  • Bro 将name="firstname" 更改为name="name"name="subject" 更改为name="message" 并删除lastname 输入字段,因为它没有在php 文件中使用
  • @HussainOmer 有用吗?
  • 不,它没有
  • @HussainOmer 你能打印出mail($to,$email_subject,$email_body,$headers); 的结果吗?它返回什么?

标签: javascript php html jquery css


【解决方案1】:
     <section id="contact">
        <div class="container" data-aos="fade-up">
            <div class="contactform">
                <div style="text-align:center">
                    <div class="section-title">
                        <h2><br/>Get In Touch</h2>
                    </div>
                    <p>Feel Free To Reach Out To Me Through This Form! </p>
                </div>
                <div class="row">
                    <div class="column">
                        <form name="myform" action="mailto:youraddr@domain.tld" method="POST">
                        <label for="firstname">First Name</label>
                        <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>

                        <label for="lastname">Last Name</label>
                        <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>

                        <label for="email">Email:</label>
                        <input type="email" id="email" name="email" placeholder="Your Email.." required>

                        <label for="subject">Subject</label>
                        <textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
                        <input type="submit" value="Submit">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>

在 HTML 中,您可以在元素的 [action] 属性中指定 mailto: 地址。 这将允许用户的电子邮件客户端创建一个电子邮件,其中预填充了 .

中的字段

【讨论】:

    【解决方案2】:

    正如您提到的,您已经完成了重定向到thankyou.html,但电子邮件并未发送给您。 问题可能在于mail($to,$email_subject,$email_body,$headers);

    您会看到,mail 本身并没有真正发送邮件,而是在主机中调用sendmail,主机依次将要通过本地交换发送的消息排队。所以

    • PHP 可能无法调用 sendmail(在这种情况下,mail 应该返回 false)
    • sendmail 可能会失败(检查尝试https://stackoverflow.com/a/13390926/1626321
    • 在许多情况下,您的主机可能有一个虚拟的sendmail
    • 您的电子邮件可能会被交易所阻止/丢弃(将其标记为垃圾邮件/可疑邮件,许多主机也会阻止邮件以宣传他们自己的电子邮件产品)

    最好的解决方案是切换到使用 SMTP,使用类似 PHPMailer (https://github.com/PHPMailer/PHPMailer) 的东西。应该有很多关于如何使用它的教程。您可以使用主机中的 SMTP 服务器,甚至可以使用 Gmail。

    更全面的答案可以在这里找到@John Condehttps://stackoverflow.com/a/24644450/1626321

    【讨论】:

      【解决方案3】:

      您好,我编写了下面的代码并对其进行了编辑,如果它不起作用或者您遇到任何问题,请在 cmets 中告诉我,希望您觉得这个答案有帮助 html页面:

      <form name="myform" action="contact.php" method="POST">
       <label for="firstname">First Name</label>
       <input type="text" id="firstname" name="firstname" placeholder="Your name.." required>
      
       <label for="lastname">Last Name</label>
       <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>
      
       <label for="email">Email:</label>
       <input type="email" id="email" name="email" placeholder="Your Email.." required>
      
       <label for="subject">Subject</label>
       <textarea id="subject" name="subject" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
       <input type="submit" value="Submit">
      </form>
      

      php页面:

      $fname = $_POST['firstname'];
      $lname = $_POST['lastname'];
      $email_address = $_POST['email'];
      $message = $_POST['subject'];
      if(empty($fname) || 
      empty($lname) || 
      empty($email_address) || 
      empty($message)){
          $errors .= "\n Error: all fields are required";
      }else{
      //some other code 
      $to = $myemail;
      $email_subject = "Contact form submission:" . $name;
      $email_body = "You have received a new message. ".
      " Here are the details:\n Name: " . $name . "\n ".
      "Email: $email_address\n Message \n " . $message;
      $headers = "From:" . $myemail . "\n";
      $headers .= "Reply-To:" . $email_address;
      mail($to,$email_subject,$email_body,$headers);
      
      header('Location: thank-you.html');
      }
      

      【讨论】:

      • 您应该解释解决问题的原因或方式。其他人理解它会非常有用。
      • 我们的朋友写错了 $_POST 值,并且一些邮件功能内容也写错了,所以当帖子名称与发布的值不匹配时,它将无法工作
      【解决方案4】:

      action="thankyou.html" 更改为action="yourMail.php",以便将表单数据发送到包含mail() 函数的PHP 文件。

      <form name="myform" action="contact.php" method="POST">
                  <label for="name">First Name</label>
                  <input type="text" id="name" name="firstname" placeholder="Your name.." required>
      
             <!--     <label for="lastname">Last Name</label>
                  <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required> -->
      
                  <label for="email">Email:</label>
                  <input type="email" id="email" name="email" placeholder="Your Email.." required>
      
                  <label for="message">Subject</label>
                  <textarea id="message" name="message" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
                  <input type="submit" value="Submit">
                </form>
      

      也许这会对你有所帮助:)

      【讨论】:

        【解决方案5】:

        如上述答案中所述,您应该将表单 POST 操作更改为您的 mailTo.php 文件,因为它将发送邮件。之后您可以做的是在您的邮件发送成功后,您可以通过 PHP 服务器重定向到 thankYou.html,如果失败,您可以留在同一页面上,显示发现/发生的相应错误。 就这样吧,

        # on success
        header('Location: http://your-website/contact-form-thank-you.html');
        exit();
        

        【讨论】:

          【解决方案6】:

          action="thankyou.html" 更改为action="yourMail.php" 以便将表单 POST 数据发送到包含 mail() 函数的 php 文件,而不是发送到您的thankyou.html。 另请注意,您需要一个 SMTP 服务器来从您的本地主机发送邮件。我会推荐你​​使用 PHPMailer。

          HTML

          <form name="myform" action="contact.php" method="POST">
                      <label for="name">First Name</label>
                      <input type="text" id="name" name="firstname" placeholder="Your name.." required>
          
                 <!--     <label for="lastname">Last Name</label>
                      <input type="text" id="lastname" name="lastname" placeholder="Your last name.." required> -->
          
                      <label for="email">Email:</label>
                      <input type="email" id="email" name="email" placeholder="Your Email.." required>
          
                      <label for="message">Subject</label>
                      <textarea id="message" name="message" placeholder="Lets Collaborate..." style="height:170px" required></textarea>
                      <input type="submit" value="Submit">
                    </form>
          

          【讨论】:

          • 但是就像我说的,我还需要将用户重定向到thankyou.HTML以及发送到我的电子邮件的响应。
          • action 标签用于指定表单数据发送到哪里,而不是提交后重定向到哪里。 .php 文件中的header('Location: contact-form-thank-you.html'); 在发送邮件后将重定向到contact-form-thank-you.html。
          • 你是在本地系统上执行这个吗?
          • 好的,所以在repl.it 中有两个选项可以选择在其中编写代码:HTML/CSS/JSPHP 但我无法在HTML/CSS/JS 服务器中使用 PHP,因为那是我的网站在哪里
          猜你喜欢
          • 2021-09-24
          • 2013-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-06
          相关资源
          最近更新 更多