PHP联系表单提交但不发送邮件

我最近使用 HTML、CSS 和 JS 制作了一个网站。由于我不懂 PHP,所以我一直致力于构建网站上至关重要的联系表单。有以下 HTML 和 PHP 代码:


<div class="contact_form">

  <form action="/action_page.php">

    <input type="text" id="name" name="name" placeholder="Name*">

    <input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">

    <input type="text" id="phone" name="phone" placeholder="Phone No.">

    <input class="contact_even" type="text" id="city" name="city" placeholder="City">

    <textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>

    <input type="submit" value="Submit">

  </form>

</div>

<?php

  if (isset($_POST['submit'])) {

    $name = $_POST['name'];

    $mailFrom = $_POST['email'];

    $phone = $_POST['phone'];

    $city = $_POST['city'];

    $message = $_POST['message'];


    $mailTo = 'example@something.in';

    $headers = 'From: '.$mailFrom;

    $txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;


    $headers = "MIME-VERSION: 1.0" . "\r\n";

    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


    mail($mailTo, $headers, $txt);

    header("Location: index.html?mailsent");

  }

?>

为什么我需要底部的 MIME 和内容类型标头,就像我从另一个教程中添加的那样。


当我使用表单并尝试发送消息时,我在 URL 后面收到&ldquo;?mailsent&rdquo;,但我没有收到电子邮件,这是 GoDaddy 的专业计划。


慕运维8079593
浏览 92回答 2
2回答

白猪掌柜的

参见邮件功能文档有 3 个必需参数:电子邮件目的地(收件人)、主题和消息,另外两个选项是:标头和参数。您的代码不尊重这一点,因为您缺少添加主题作为参数。你会得到 ?mailsent 因为你使用 header("Location: index.html?mailsent") 而不进行任何测试电子邮件是否发送成功。我建议你用这个替换你的 php 代码的最后两行$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]$result = mail($mailTo, $subject , $txt,$headers);if ($result){  // mail send successfully   header("Location: index.html?mailsent");} else { // error}您可以使用 error_get_last() 函数获取错误消息。$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]$result = mail($mailTo, $subject , $txt,$headers);if ($result){  // mail send successfully   header("Location: index.html?mailsent");} else { print_r(error_get_last());}

慕森王

您缺少表单操作,因此 PHP 不知道如何处理您的变量数据。尝试添加method="post"内部<form>标签。像这样<div class="contact_form">&nbsp; <form action="/action_page.php"&nbsp; method="post">&nbsp; &nbsp; <input type="text" id="name" name="name" placeholder="Name*">&nbsp; &nbsp; <input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">&nbsp; &nbsp; <input type="text" id="phone" name="phone" placeholder="Phone No.">&nbsp; &nbsp; <input class="contact_even" type="text" id="city" name="city" placeholder="City">&nbsp; &nbsp; <textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>&nbsp; &nbsp; <input type="submit" value="Submit">&nbsp; </form></div>并且。如果您将计算机用作本地主机(使用 xampp 、 wamp 或没有托管服务的东西),则必须对配置文件进行一些更改。也尝试一下修改后的 php 代码<?php&nbsp; if (isset($_POST['submit'])) {&nbsp; &nbsp; $name = $_POST['name'];&nbsp; &nbsp; $mailFrom = $_POST['email'];&nbsp; &nbsp; $phone = $_POST['phone'];&nbsp; &nbsp; $city = $_POST['city'];&nbsp; &nbsp; $message = $_POST['message'];&nbsp; &nbsp; $title = "replace this";&nbsp; &nbsp; $mailTo = 'support@udichi.in';&nbsp; &nbsp; $txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;&nbsp; &nbsp;$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL .&nbsp; 'X-Mailer: PHP/' . phpversion();&nbsp; &nbsp; mail($mailTo,$title,$txt,$headers);&nbsp; &nbsp; header("Location: index.html?mailsent");&nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP