我是否需要安装sendmail才能在联系表单中使用mail()函数?

时间:2017-09-23 19:16:41

标签: php email

这之前有用,但我不得不创建一个新的vps,现在我似乎无法使邮件功能正常工作。当我尝试发送测试电子邮件时,我什么也得不到,我甚至看不到我创建的成功或错误消息。

我的vps日志中的错误消息对我来说很神秘,它说:

2017/09/23 15:06:47 [error] 22847#22847: *96 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: error in /var/www/microurb.com/public_html/index.php on line 295" while reading upstream, client: 73.197.81.232, server: microurb.club, request: "GET /index.php?success=-1 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "microurb.club", referrer: "http://microurb.club/index.php?success=-1"

我不知道这可能意味着什么。

我的index.php文件中的表单如下所示:

<section class="section-form" id="form">
  <div class="row">
    <h2>We're happy to hear from you</h2>
  </div>
  <div class="row">
    <form class="contact-form" action="mailer.php" method="post">
      <div class="row">
        <?php
                    if ($_GET['success'] == 1) {
                            echo "<div class=\"form-messages success\">Thank you! Your message has been sent.</div>";
                    }
                    if ($_GET['error'] == -1) {
                            echo "<div class=\"form-messages error\">Oops! Something went wrong. Please try again!</div>";
                    }
        ?>
      </div>
      <div class="row">

        <div class="col span-1-of-3">
          <label for="name">Name</label>
        </div>
        <div class="col span-2-of-3">
          <input type="text" name="name" id="name" placeholder="Your name" required>
        </div>
      </div>
      <div class="row">
        <div class="col span-1-of-3">
          <label for="email">Email</label>
        </div>
        <div class="col span-2-of-3">
          <input type="email" name="email" id="email" placeholder="Your email" required>
        </div>

我的mailer.php看起来像这样:

<?php

    // Get the form fields, removes html tags and whitespace.
    $name = strip_tags(trim($_POST["name"]));
    $name = str_replace(array("\r","\n"),array(" "," "), $name);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    // Check the data
    if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
      header("Location: http://microurb.club/index.php?success=-1#form");
      exit;
    }

    // Set the recipient email address.
    $recipient = "ale@example.com";

    // set the email subject.
    $subject = "New contact from $name";

    // Build the email content.
    $email_content = "Name: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message: \n$message\n";

    // Build the email headers.
    $email_headers = "From $name <$email>";

    // Send the email
    mail($recipient, $subject, $email_content, $email_headers);

    // Redirect to the index.html page with success code
    header("Location: http://microurb.club/index.php?success=-1#form");

 ?>

我已经用几种方式重构了这段代码,现在我想知道问题是我是否必须在我的服务器上安装postfix或sendmail。

1 个答案:

答案 0 :(得分:0)

您确保在确保其存在之前尝试访问$_GET['error']

尝试if (isset($_GET['error']) && $_GET['error'] == -1)