我没有收到邮件php

时间:2015-07-15 10:48:04

标签: php forms post html-email

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

我正在使用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年前),我也测试了它,但它没有工作 - 。 -

是的,请有人帮帮我。

2 个答案:

答案 0 :(得分:0)

您必须为表单中的所有输入/ textarea标记添加name属性,以便在$ _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);

答案 1 :(得分:0)

尝试用此

替换
$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);