PHP mail()返回错误

时间:2018-07-19 19:13:54

标签: php html

我正在尝试使用简单的php联系表来运行一个网站。我正在从一个随机的虚拟主机上运行该网站。我也不是最了解PHP或类似的东西,所以我们将不胜感激。

我的问题是,当我在网站上填写表格并单击发送时,告诉我“出了点问题。请重试。”

这是我正在使用的sendEmail.php脚本。 (我也知道它说myemail@gmail.com)

<?php


$siteOwnersEmail = 'myemail@gmail.com';


if($_POST) {

   $fname = trim(stripslashes($_POST['contactFname']));
   $lname = trim(stripslashes($_POST['contactLname']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check First Name
    if (strlen($fname) < 2) {
        $error['fname'] = "Please enter your first name.";
    }
    // Check Last Name
    if (strlen($lname) < 2) {
        $error['lname'] = "Please enter your last name.";
    }
    // Check Email
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address.";
    }
    // Check Message
    if (strlen($contact_message) < 15) {
        $error['message'] = "Please enter your message. It should have at least 15 characters.";
    }
   // Subject
    if ($subject == '') { $subject = "Contact Form Submission"; }

    // Set Name
    $name = $fname . " " . $lname;

   // Set Message
   $message .= "Email from: " . $name . "<br />";
    $message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

   // Set From: header
   $from =  $name . " <" . $email . ">";

   // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


   if (!$error) {

      ini_set("sendmail_from", $siteOwnersEmail); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);

        if ($mail) { echo "OK"; }
      else { echo "Something went wrong. Please try again."; }

    } # end if - no validation error

    else {

        $response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
        $response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

        echo $response;

    } # end if - there was a validation error

}

?>

0 个答案:

没有答案