试图将表单和php文件链接在一起。没有收到电子邮件。我究竟做错了什么?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图将表单和php文件链接在一起。没有收到电子邮件。我究竟做错了什么?相关的知识,希望对你有一定的参考价值。
<div class="col-lg-12 col-md-12 offset-lg-1">
<div class="impl_con_form">
<div class="col-lg-12 col-md-12">
<h1>get in touch</h1>
</div>
<form action="sendmail.php" method="POST" class="contact-form" novalidate="novalidate">
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="first_name" class="form-control require" placeholder="YOUR NAME">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="email" class="form-control require" placeholder="YOUR EMAIL" data-valid="email" data-error="Email should be valid.">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="SUBJECT">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<textarea name="message" class="form-control" placeholder="YOUR MESSAGE"></textarea>
</div>
</div>
<div class="response"></div>
<div class="col-lg-12 col-md-12">
<input type="hidden" name="form_type" value="contact">
<button type="submit" data-type="submit" name="submit" value="submit">Submit</button>
</div>
</form>
</div>
</div>
上面的代码是我正在进行的一个实践项目的联系表。我试图让它与我的sendmail.php文件链接,并将其发送到我当前的电子邮件地址,但我没有运气。
<?php
if( isset($_POST['submit']))
$to = "jamie@sharpsdigital.co.uk";
$sub = "Contact Form Enquiry";
$headers = "From: Star Tyres <jamie@sharpsdigital.co.uk>"."
";
$headers .= "Reply-To: ".$email."
";
$headers .= "X-Mailer: PHP/". phpversion()."
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$first_name.'</p>
<p>Email: '.$email.'</p>
<p>Subject: '.$subject.'</p>
<p>Message: '.$message.'</p><br>
';
$mail = mail($to, $sub, $message, $headers, 'jamie@sharpsdigital.co.uk');
if ( $mail)
{
header('Location: contact.html?var=sent');
}
else
{
/* error_reporting(-1);
ini_set('display_errors', 'On');
echo 'error';
print_r(error_get_last()); */
header('Location: contact.html?var=error');
}
这个是我的php,我已经使用action =“sendmail.php”并使用方法post链接了两者。然而,当我点击按钮并发送它似乎只是去* .com / sendmail.php而我没有收到电子邮件。我究竟做错了什么?
更新==============================
经过几点评论后,我试图调整一些内容。我现在在点击提交按钮后返回echo命令,但没有别的。
<?php
extract($_POST);
echo "Made it to sendmail.php";
if( isset($_POST['submit']))
{
$mailTo = "jamie@sharpsdigital.co.uk";
$sub = "Contact Form Enquiry";
$headers = "From: Star Tyres <jamie@sharpsdigital.co.uk>"."
";
$headers .= "Reply-To: ".$email."
";
$headers .= "X-Mailer: PHP/". phpversion()."
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$first_name.'</p>
<p>Email: '.$email.'</p>
<p>Subject: '.$sub.'</p>
<p>Message: '.$message.'</p><br>
';
$mail = mail($to, $sub, $message, $headers, 'jamie@sharpsdigital.co.uk');
}
?>
答案
更改按钮以将表单提交至:
<input type="button" name="submit" value="Submit">
input
是一个形式的元素,一个按钮没有。这个名字是你在php($_POST['submit']
)中寻找的名字
value属性是按钮内显示的内容
另一答案
HTH
<?php
extract($_POST);
if( isset($_POST['submit']))
{
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$to = "jamie@sharpsdigital.co.uk";
$sub = "Contact Form Enquiry";
$headers = "From: Star Tyres <jamie@sharpsdigital.co.uk>"."
";
$headers .= "Reply-To: ".$email."
";
$headers .= "X-Mailer: PHP/". phpversion()."
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$first_name.'</p>
<p>Email: '.$email.'</p>
<p>Subject: '.$sub.'</p>
<p>Message: '.$message.'</p><br>
';
$mail = mail($to, $sub, $message, $headers, 'jamie@sharpsdigital.co.uk');
}
?>
或者,更简单地说:
<?php
$to = 'jamie@sharpsdigital.co.uk';
$subject = 'Contact Form Enquiry';
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$_POST['first_name'].'</p>
<p>Email: '.$_POST['email'].'</p>
<p>Subject: '.$subject .'</p>
<p>Message: '.$_POST['message'].'</p><br>
';
$headers = 'From: webmaster@example.com' . "
" .
'Reply-To: ".$_POST['email']' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
测试目的:
创建新文件testemail.php
并输入以下代码:
<?php
$to = 'jamie@sharpsdigital.co.uk';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: no-reply@domain.com' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
然后浏览到页面,看看你是否收到了一封电子邮件......
以上是关于试图将表单和php文件链接在一起。没有收到电子邮件。我究竟做错了什么?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 PHP 表单不起作用?我确实收到了电子邮件,但里面没有内容