0

I am trying to send mail from a PHP page. I don't get any errors, but it doesn't echo the validation error/success messages I included in the code. I am trying to send from Cloud9, which may be impossible, but shouldn't it at least display my success/error message?

[UPDATE] Now it does display my error messages, but every time I submit it, even though I input the needed data. And no information weather the sending failed or not.

PHP:

<?php 
  include 'includes/mail.php';
?>
<!DOCTYPE html>

...

<form action="?" method="post">
          <input name="contact-name" type="text" placeholder="Ime" class="text-input">
          <input name="contact-mail" type="email" placeholder="Email" class="text-input">
          <textarea name="contact-text" placeholder="Poruka" class="text-input"></textarea>
          <p id="submit-btn">
            <button type=submit>
              <img src="img/lmbtn.png" alt="submit button">
              <span class="noselect">POŠALJI</span>
            </button>
          </p>
          <?php
              if (empty($errors) === true) {
                  if(isset($contact_text, $contact_name, $contact_mail)) {
                    $subject = "Mail from " . $contact_name ." via www.mysite.com";
                    $headers = "From: " . $contact_mail;
                    $sent = mail($to, $subject, $contact_text, $headers);
                    if ($sent) {
                      echo "<p>Poruka je poslata!</p>";
                    } else {
                      echo "<p>I dalje baguje.</p>";
                    }
                  }
                } else {
                  ?>
                    <ul>
                      <?php
                        foreach ($errors as $error) {
                          echo "<li>", $error, "</li>";
                        }
                       ?>
                    </ul>
                  <?php 
                }
          ?>
        </form>
...

And the included mail.php:

<?php 
    $to = "mymail@gmail.com";

    if (isset($_POST['contact-name'], $_POST['contact-mail'], $_POST['contact-text'])) {
        if (empty($_POST['contact-name'])) {
            $errors[] = "Molimo unesite Vaše ime";
        } else {
            $contact_name = htmlentities($_POST['contact-name']);
        }

        if (empty($_POST['contact-mail'])) {
            $errors[] = "Molimo unesite Vašu email adresu.";
        } else if (strlen($_POST['contact-mail']) > 60) {
            $errors[] = "Vaša email adresa je predugačka.";
        } else if (filter_var($_POST['contact-mail'], FILTER_VALIDATE_EMAIL) === false ) {
            $errors[] = "Unesite validnu email adresu.";
        } else {
            $contact_mail = "<" . htmlentities($_POST['contact-mail']) . ">";
        }

        if (empty($_POST['contact-text'])) {
            $errors[] = "Molimo unesite poruku.";
        } else {
            $contact_text = htmlentities($_POST['contact-text']);
        }
    }
?>

I did not include css or javascript, I guess they are not relevant. JQuery just resets the form after submitting it. Any help, I tried both stackoverflow and google?

  • 1
    Why "onsubmit=return false" ? – Mathews Mathai Mar 10 '16 at 14:26
  • 2
    you're just assuming mail() worked. it returns boolean false on failure, which you're completely ignoring. check that return value. if it comes back as boolean true, then it's not a php problem, and you need to look at your mail server logs. – Marc B Mar 10 '16 at 14:30
  • @MathewsMathai because I don't want it to reload the page on submit. – Nikola Markovic Mar 10 '16 at 14:45
  • Are you sure `onsubmit=false` works as you said? – Mathews Mathai Mar 10 '16 at 14:49
  • It does stop the page from reloading, but causes the problems, also. I removed it, so I will update the question now. – Nikola Markovic Mar 10 '16 at 14:54
  • According to me, `onsubmit= return false` cancels the submission routine and so your form is not getting submitted in the first place. I am not sure about it. Try removing it. – Mathews Mathai Mar 10 '16 at 14:57
  • I did. You can look at the question now, if you want. Thank you for your help thus far. – Nikola Markovic Mar 10 '16 at 14:58
  • Check if the mail function is even trying to send: `if(mail($to, $subject, $contact_text, $headers)){ echo true; } else { echo false; }` – Henry A. Mar 10 '16 at 14:59
  • @Rock2040 Wheather I try it your way, or like I did before (`$sent = mail($to, $subject, $contact_text, $headers)` and then test for true/false), it does nothing. Like it never got to that part of logic. – Nikola Markovic Mar 10 '16 at 15:09

3 Answers3

0

Mail is to be sent through server so you need to post the form and server will mail and reply the result.

Same php script is being used to generate the mail and send the mail.

So first when you will open the page . server will generate the form as the form is not yes posted. And $_POST variable is not generated.

But when you will try to submit the form it won't get submitted as you have set "return false;" on submit.

To submit the form without page load you need to send the data asynchronously (using Ajax, search Google for more info)

More over even if you remove the "Return false" code wont work as the mail() function returns boolean true and false but never echoes it. So you can var_dump it.

Ashutosh Raj
  • 1,045
  • 1
  • 11
  • 21
  • I removed the `onsubmit = "return false"` and updated the question a while ago. Thanks for info on ajax, I suspected as much, but you confirmed it. – Nikola Markovic Mar 10 '16 at 15:22
  • Mail function cannot give information of mail sent or not. But it will return true if the mail was submitted and false if not. – Ashutosh Raj Mar 10 '16 at 15:26
0

Try a simplified version maby?

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

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

// Create the email and send the message
$to = 'example@example.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "New Email:  $name";
$email_body = "You have a new message.\n\n"."Details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourwebsite.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

You are sure your server supports the PhP mail function, right?

Stefan
  • 1
  • 1
0

Check your php.ini by the way, see http://php.net/manual/en/mail.configuration.php

In this file you can see where your log file is, and check if everything is configured correctly.

Stefan
  • 1
  • 1