我正在尝试使用PHP通过一个页面提交从表单发送邮件,但我似乎丢失了一些东西,因为在提交时没有发送邮件也不会引发错误(这相当奇怪)。下面是同一文件中的PHP和HTML表单代码 PHP脚本
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: risingventures.com.ng';
$to = 'inquiry@risingventures.com.ng';
$subject = 'New message from contact form';
$body = "From: $name\n E-mail: $email\n Phone: $phone\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: from@example.com' . "\r\n" .
'Reply-To: reply@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $body, $headers)) {
$result = '<div class="alert alert-success">Thank You! We will be in touch with you soon</div>';
} else {
$result = '<div class="alert alert-danger">Sorry! There was an error submitting your message. Please try again</div>';
}
}
?>
HTML代码
<form action="contact.php" method="POST" role="form">
<legend>Contact Us</legend>
<div class="form-group">
<div class="col-lg-12" style="margin-bottom: 20px">
<label>Name *</label>
<input type="text" class="form-control" name="name" placeholder="Please type your Firstname followed by your Lastname">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-6">
<label>Email *</label>
<input type="email" class="form-control" name="email" placeholder="Please type in your email address *">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-6" style="margin-bottom: 20px">
<label>Phone</label>
<input type="tel" class="form-control" name="phone" placeholder="Please type in your phone number">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-12" style="margin-bottom: 20px">
<label>Message *</label>
<textarea class="form-control" name="message" rows="6"></textarea>
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-12" style="margin-bottom: 20px">
<input type="submit" class="btn btn-primary" value="Send Message">
</div>
</div>
<div class="form-group" style="margin-bottom: 20px">
<div class="col-lg-12" style="margin-bottom: 10px">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</form>
答案 0 :(得分:0)
在检查了html的表单标签后,我观察了以下内容
enctype属性指定在将表单数据提交到服务器时应如何编码表单数据,如果method =“post”则需要enctype属性。这可以是multipart / form-data,text / plain或application / x-www-form-urlencoded
如果这样做无法解决,请删除role = form并重试