-1

This was working before, but I had to create a new vps and now I cannot seem to get the mail function working. When I try to send a test email, I get nothing and I do not even see the success or error messages I created.

The error message in my vps log is cryptic to me, it says:

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"

I have no clue what this could mean.

The form on my index.php file looks like this:

<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>

And my mailer.php looks like this:

<?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");

 ?>

I have refactored this code in several ways and now I am wondering if the issue is whether or not I have to install postfix or sendmail in my server.

  • You're setting `success=-1` but your checking for `$_GET['error'] == -1`. You're actually _always_ setting `success` to `-1` (regardless of the state) and you're never setting the `error` param at all in any situation. – M. Eriksson Sep 23 '17 at 19:21
  • @MagnusEriksson, ok I made a bit of progress. I changed the code to this: header("Location: http://microurb.club/index.php?success=1#form"); and now I get a thank you for successful submission but still no email. –  Sep 23 '17 at 19:27
  • That's a different subject, though. There could be a lot of reasons for that. Check the duplicate post from @Qirel to help you debug that. – M. Eriksson Sep 23 '17 at 19:30

1 Answers1

0

You're trying to access $_GET['error'] before ensuring it exists.

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

eidsonator
  • 1,319
  • 2
  • 11
  • 25
  • Eidsonator, this did not work, in fact, I did not even get a success message after hitting send. –  Sep 23 '17 at 19:49
  • @Ale, mail() calls the mail shell command which comes with sendmail or postfix, I am curious, do you have either of these installed? – Daniel Sep 24 '17 at 02:27
  • @Daniel, so you are saying I do need one of them? –  Sep 24 '17 at 02:30
  • @Ale, yeah, look in /var/mail, what directories do you have? – Daniel Sep 24 '17 at 02:31
  • @Daniel, nothing in there. –  Sep 24 '17 at 02:34
  • @Ale, yeah so, this documentation: https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail, is kind of appropriate, but it is not explicit about sendmail and the guy who mentioned sendmail alot didn't even get any upvotes. The documenation you really need is: https://stackoverflow.com/questions/5022605/why-does-php-mail-require-a-mail-program-like-sendmail-postfix-etc-for-sendin. Look it over and let me know. – Daniel Sep 24 '17 at 02:39
  • @Daniel, I installed sendmail on my server and retried to send mail on my contact form and it worked! You are my new best friend. –  Sep 24 '17 at 02:46
  • @Ale, great :) Try editing your question, because technically it is a duplicate, but after trying your code in different ways and looking at the error log you posted, I went on a hunch. My server experience paid off for you, best wishes. – Daniel Sep 24 '17 at 02:51