如何使联系人表单提交对电子邮件的回复?



我想让联系人表单在用户点击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_addressn Message n $message";
$headers = "From: $myemailn";
$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文件中的类/对象,但这也没有做到这一点。

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>

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

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

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>

也许这对你有帮助:)

你好,我写了下面的代码并编辑了它,如果它不起作用或者你遇到任何问题,请在评论中告诉我,我希望你发现这个答案对你有帮助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_addressn Message n " . $message;
$headers = "From:" . $myemail . "n";
$headers .= "Reply-To:" . $email_address;
mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');
}

正如你提到的,你已经完成了重定向到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 Conde https://stackoverflow.com/a/24644450/1626321

<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: address。这将允许用户的电子邮件客户端创建一个预先填充了。

字段的电子邮件。

最新更新