如何使联系表单将提交的回复发送到电子邮件?

4
我希望把联系表单设置为在用户点击“提交”按钮时提交响应。当用户点击“提交”按钮时,还应将其重定向到我已经完成的“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 文件中的类/对象,但这也没有起作用。


这应该能回答你的问题:https://dev59.com/B2Af5IYBdhLWcg3wcyar - luek baja
1
兄弟,把name="firstname"改成name="name"name="subject"改成name="message",并且移除lastname输入框,因为在php文件中没有使用。 - Emmanuel
@HussainOmer 它工作吗? - Emmanuel
不,它不支持。 - user12929063
@HussainOmer 你能打印出mail($to,$email_subject,$email_body,$headers);函数的结果吗?它返回什么? - Cuong Le Ngoc
6个回答

3

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 页面,并将响应发送到我的电子邮件。 - user12929063
action标签用于指定表单数据发送的位置,而不是提交后重定向的位置。在您的.php文件中的header('Location: contact-form-thank-you.html');将在发送邮件后重定向到contact-form-thank-you.html。 - Emmanuel
你是在本地系统上执行吗? - Emmanuel
repl.it 中,有两个选项可供选择编写代码:HTML/CSS/JSPHP。但是,我是否无法在 HTML/CSS/JS 服务器中使用 PHP,因为我的网站就在那里? - user12929063

0
如上面的答案所提到的,您应该将表单POST操作更改为您的mailTo.php文件,因为它将发送邮件。之后,您可以在邮件发送成功时通过PHP服务器重定向到thankYou.html,如果失败,则可以留在同一页上,显示相应的错误信息。因此,代码如下:
# on success
header('Location: http://your-website/contact-form-thank-you.html');
exit();

0
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>

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


0

你好,我写了下面的代码并进行了编辑。如果它没有正常工作或者你遇到了任何问题,请在评论中告诉我。希望这个答案能对你有所帮助。 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');
}

你应该解释为什么或者如何你的解决方案可以解决问题。这对于其他人理解非常有用。 - Sakibul Alam
我们的朋友们错误地编写了$_POST值,有些邮件函数内容也写错了,因此当发布的名称与发布的值不匹配时,它将无法工作。 - Abdurrahman Alsallakh

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="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:地址。 这将允许用户的电子邮件客户端创建一个预填充了表单字段的电子邮件。

0

正如您所提到的,您已经成功重定向到thankyou.html,但是电子邮件没有发送给您。 问题可能出在mail($to,$email_subject,$email_body,$headers);上。

您知道,mail并不是真正意义上的发送邮件,而是在主机中调用sendmail,然后通过本地交换队列发送消息。所以

  • PHP可能无法调用sendmail(在这种情况下,mail应返回false)
  • sendmail可能会失败(要检查,请尝试https://dev59.com/3GYr5IYBdhLWcg3wytHe#13390926
  • 在许多情况下,您的托管可能有一个虚拟的sendmail
  • 您的电子邮件可能会被交换系统阻止/丢弃(将其标记为垃圾邮件/可疑邮件,许多托管还会阻止邮件以推广自己的电子邮件产品)

最好的解决方案是切换到使用SMTP,使用类似于PHPMailerhttps://github.com/PHPMailer/PHPMailer)的东西。应该有很多教程可以告诉你如何使用它。您可以使用托管的SMTP服务器,或者甚至可以使用Gmail。

更全面的答案可以在这里找到,由@John Conde提供 https://dev59.com/B2Af5IYBdhLWcg3wcyar#24644450


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接