1

I have problem with sending mail in php..Under this text is my code..Echo for script working and else and if working too,but only thing which is no working is mail()

<?php
        if (isset($_POST['submit'])) {
           $datumod = $_POST['dateod'];
           $datumdo = $_POST['datedo'];
           $broj = $_POST['phone'];
           $email = $_POST['email'];
            if (empty($datumod) || empty($datumdo) || empty($broj) || filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            echo "<script>
        alert('Morate popuniti sva polja tacno kako bi znali da li ima rezervisanih soba u tom vremenskom intervalu!');
        </script>";

        }
        else {
        $poruka = "Jedna osoba je htela da proveri da li ima slobodnih soba od ".$datumod." do ".$datumdo."Broj telefona:".$broj."Email od tog korisnika:".$email."";
        $headers = array("From: vakijevsvet@yahoo.com",
            "Reply-To: vakijevsvet@yahoo.com",
            "X-Mailer: PHP/" . PHP_VERSION
        );     
        mail('vakijevsvet@yahoo.com','Prenociste',$poruka,$headers);
        echo "<script> 
        alert('Uspesno ste poslali Vasu rezervaciju,u roku od 24h cete dobiti odgovor o kapacitetu prenocista u tom vremenskom intervalu.');
        </script>";
        }
        }
        ?> 
  • This is highly dependent on whether your MTA is configured properly and PHP is configured to make use of it properly. You need to describe what 'not working' means and perhaps review error logs for both PHP and MTA. – pvg Dec 17 '16 at 09:12
  • @pvg, it may be that way. But in this case there is a mistake in the code. See my answer. – Ionut Necula Dec 17 '16 at 09:58
  • @lonut ah yeah. Leave it to PHP to have a bad API for almost everything! – pvg Dec 17 '16 at 10:07
  • @pvg, what do you mean by that? – Ionut Necula Dec 17 '16 at 10:14
  • @lonut that taking an array of strings there and constructing the properly separated headers would have actually been a sensible API. PHPMailer takes this a step further by accepting name-value pairs. – pvg Dec 17 '16 at 10:18
  • Yes. I agree. Thus why the suggestion/recommendation from my part of using it. But my answer, in this case resolves user's problem, except if he doesn't have some server issues. PHPMailer it was just a recomendation. I don't think it was a bad thing to suggest. I hope it helps the OP. – Ionut Necula Dec 17 '16 at 10:23

1 Answers1

1

Headers should be string and NOT array, so change this:

$headers = array("From: vakijevsvet@yahoo.com",
            "Reply-To: vakijevsvet@yahoo.com",
            "X-Mailer: PHP/" . PHP_VERSION
        );  

to this:

$headers = "From: vakijevsvet@yahoo.com\r\n";
$headers .= "Reply-To: vakijevsvet@yahoo.com\r\n";
$headers .= "X-Mailer: PHP/" . PHP_VERSION . "\r\n";

and it should work just fine.

Also, for avoiding spam and whatnot I suggest you using a library like PHPMailer. This will not 100% guarantee it will prevent the emails from going into spam. It's just a recommendation, an improvement of PHP's mail() function. There is thing to take in account like your server configuration. But in this case as I said is just a recommendation. You can use both mail() or PHPMailer, it's your choice.

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
  • I think you're missing the required \r\n at the end of each header – pvg Dec 17 '16 at 10:06
  • How does using a library avoid spam filters? – Barmar Dec 17 '16 at 10:07
  • @pvg, missed them. Added them. Barmar, I don't think there is an absolutely 100% chance that using PHPMailer will prevent mails from going to spam, but it contains everything you need to try and prevent them, being a library created to help in this purpose too, setting the headers and all, it depends also on the server configuration etc. – Ionut Necula Dec 17 '16 at 10:13
  • 1
    @Barmar using PHPMailer would have also avoided the problem you had to begin with. – pvg Dec 17 '16 at 10:19
  • What problem did I have to begin with? I didn't write the question. – Barmar Dec 17 '16 at 10:34
  • @Barmar, I think he was speaking metaphorically. Anyway, please let's not turn this into a chat. As we're here only to help others and to be helped. – Ionut Necula Dec 17 '16 at 10:35
  • 1
    @Bramar oops. The key bit is that the `mail` call has a poor, easily misused api. Using a higher-level, more foolproof API like PHPMailer eliminates a class of errors similar to the one the poster encountered. So the recommendation to use it or something like it is a useful addition to the answer. – pvg Dec 17 '16 at 11:02