PHP’s native mail() function returns true and does not send Email

Asked

Viewed 268 times

1

Good morning!

I’ve got a rock in my shoe that I need guidance on. Through a separate file (which I am calling funcao_email.php) inside my "include" folder at the root, I created a PHP (mail-based) function that sends emails when called in PHP pages. The problem of this blessed one is that when it is called, it returns TRUE, but the email does not reach the recipient.

Well, hell, on the first day I tested excessively with Hotmail, and it wasn’t enough even by prayer. So I decided to test in GMAIL and it worked. The email arrived all working perfectly. After three days, I went to test it again because I thought that day Hotmail was on maintenance or something, and the result surprised me. The function keeps returning True but, none of the providers (or anyone) receives the email.

function sentRegister($usuario, $senha, $email) {
    $subject = "Bem-vindo(a) ao nosso site!";
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=UTF-8\r\n";
    $headers .= "From: [email protected] \r\n";

    $mensagem = '<p>EMAIL TESTE COM: '.$email.' e '.$senha.' mais o '.$usuario.'</p>';

    // START SENT MAIL FUNCTION
    mail($email, $subject, $mensagem, $headers);
}

This is the PHP function, and I call it normally using IF:

if($usuario_inserido) {sentRegister($usuario, $senha, $email);)

Obviously the $user, $password and $email variables return the data coming from $_POST. And I confirm that they are returning values, because, tested. :)

Would anyone know to tell me what I might be missing?

  • How are you testing the return on a function without return? Another thing, the return of function mail means that the email was successfully inserted into the output queue, and not necessarily that it went all the way to its destination. http://php.net/manual/en/function.mail.php. - In addition, you have a group of people who want to criticise the function mail saying that "you have to use Phpmailer unconditionally etc", but it is worth remembering that the mail works very well for simple everyday situations.

  • Related: https://answall.com/a/23616/57801

  • In systems based on sendmail the parameter -f can make significant difference. See the manual link above the option additional_parameters - Anyway, if you choose Phpmailer, already have solution in this post: https://answall.com/questions/40858/70 (I’m already commenting before you post duplicates without using the mail())

  • If you choose Phpmailer use the updated version Phpmailer 6 that was released at the end of August 2017 https://answall.com/questions/252928/filling-sender-com-o-campo-de-email-php-mailer/253022#253022

  • Technically, mail() suits me, but the question is not to get these emails to Hotmail, for example. @Bacco I understood your message, but, I did a test inside the if and Else file itself. if(mail($user, $password, $email)) {echo "true";} soon... it returned the TRUE. It made me understand that the function is sending... Right?

  • true = sent to the output queue. From queue to destination, it only depends on the operating system, which will send itself. If it’s *Nix, it’s usually sendmail. Looking at the log of Mailer may be that you find some hint (whether it was delivered or not, and the reason why it was not).

  • tried it here on my server and it works. However this line is wrong if($usuario_inserido) {sentRegister($usuario, $senha, $email);) has a parenthesis after the period and comma and I had to correct for if($usuario_inserido) {sentRegister($usuario, $senha, $email);} replaces brackets by key

  • This is an @Leocaracciolo example. Thanks for this remark. : ) Bacco, thanks again. I will check if my server has this feature. :)

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.