【问题标题】:I am not receiving mail php我没有收到邮件php
【发布时间】:2015-07-15 10:48:04
【问题描述】:

我在接收邮件时遇到问题。我尝试了不同的邮件提供商(微软、雅虎、gmail),但仍然没有收到邮件。我正在为我的朋友开发网站,我购买了一个带有 php 联系人的引导模板。

我正在使用 xampp,我尝试过以这种方式发送邮件,但我已将其上传到其他朋友网站的子文件夹中,但仍未收到。

代码如下: HTML index.php

<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
                        <div id="msgInfo"></div>
                        <input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name">
                        <input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email">
                        <textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message"></textarea>
                        <button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
    </form>

这里是contact.php代码:

<?php
// PHP script for sending email
//
// Configuration
//

$toEmail = "";                                          // replace with your email where you would like to send email
$subject = 'Contact form from my website';              // replace with subject you want to receive
$body    = 'You have received email from website:';     // replace with text that you want to receive in email
$from    = '';                                          // replace with email that will look like sender

//
// ----- do not edit after this line if you don't understand what you are doing -----
//

if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
    echo "Invalid input";
    return false;
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$body .= "\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message\n";

$headers = "From: $email\n";
$headers .= "Reply-To: $toEmail";

$res = mail($toEmail, $subject, $body, $headers);
echo "OK";
return true;
?>

这不是我第一次使用 php,过去我有一个工作 php(2-3 年前),我也测试过它,但它不起作用 -.-

谁能帮帮我。

【问题讨论】:

  • 你可以尝试var_dump($_POST);吗?你是否尝试用静态值替换所有$_POST(例如$_POST['name']等)?

标签: php forms post html-email


【解决方案1】:

您必须为表单中的所有输入/文本区域标签添加名称属性,以便您可以在 $_POST 数组中找到它们

<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
                    <div id="msgInfo"></div>
                    <input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name" name="name">
                    <input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email" name="email">
                    <textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message" name="message"></textarea>
                    <button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>

并在您的 php 代码中更改此行($ToEmail 变量始终为空)

$res = mail($toEmail, $subject, $body, $headers);

有了这个

$res = mail($email, $subject, $body, $headers);

【讨论】:

  • 我故意把 $toEmail 留空,因为你把你的电子邮件放在你想接收它的地方。
【解决方案2】:

尝试用这个替换

$body .= "\n";
$body .= "Name: ".$name."\n";
$body .= "Email: ".$email."\n";
$body .= "Messagee: ".$message."\n";

并更改标题:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers = 'From: '.$name."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/' . phpversion();


更新

if($res){
echo "OK";
}
else{
echo "failed."

}

通过这个你可以知道你的邮件功能是否在工作。
如果未启用,则启用 php 中的错误:

ini_set('display_errors',1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多