Funcao mail in php

Asked

Viewed 814 times

3

I’m using the function mail php and would like to check if emails were sent successfully, but I don’t know how I can do it. How could I check if the email was successfully sent?

  • To know for sure you will have to have access to the EXIM report for example or any other report of a similar software, taking into account that your server is LINUX. If you have a hosting that gives you access via VHM/Cpanel I made a class that gives you the result in a friendly way. It provides all the shipping information according to the ID of the message, ie if it was sent, rejected or is in the delivery queue. https://github.com/mjpsolucoes/VHMAuth

2 answers

4


You asked if you have any way of knowing if it was sent, has, and, I advise you to perform the third option:

1 - You can use error_get_last(), while mail() return false.

With print_r(error_get_last()) you get something like this:

[type] => 2 [message] => mail(): Failed to connect to mailserver at "x.x.x.x" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() [file] => C:\www\X\X.php [line] => 2

2 - You could do

if (!mail(...)) {
   // lol!
}

http://php.net/manual/en/function.mail.php

É importante notar que só porque o email foi true, não significa que o e-mail vai realmente chegar ao destino pretendido.

If you need to delete warnings, you can use:

if (!@mail(...))

3 - At the end of the day, I advise you to use email triggering through the Phpmailer() class. There are a hundred tutorials on this. The only difficulty you will have will be to configure the class with the ports and the SMTP server of your hosting. Example of a function:

    <?php

    require_once("smtp/class.phpmailer.php"); //link do arquivo abaixo

       define('GUSER', '[email protected]');   // <-- Insira aqui o seu GMail
       define('GPWD', 'minhasenha');      // <-- Insira aqui a senha do seu GMail


       function smtpmailer($para, $de, $de_nome, $assunto, $corpo) { 
       global $error;
       $mail = new PHPMailer();
       $mail->IsSMTP();     // Ativar SMTP
       $mail->SMTPDebug = 0;      // Debugar: 1 = erros e mensagens, 2 = mensagens apenas
       $mail->SMTPAuth = true;    // Autenticação ativada
       $mail->SMTPSecure = 'TLS'; // SSL REQUERIDO pelo GMail
       $mail->Host = 'smtp.do.meuservidor.net';  // SMTP utilizado
       $mail->Port = 587;      // A porta 587 deverá estar aberta em seu servidor
       $mail->Username = GUSER;
       $mail->Password = GPWD;
       $mail->SetFrom($de, $de_nome);
       $mail->Subject = $assunto;
       $mail->Body = $corpo;
       $mail->AddAddress($para);
       $mail->IsHTML(true);
       if(!$mail->Send()) {  //olha aqui o que você queria
          $error = 'Mail error: '.$mail->ErrorInfo; 
          return false;
       } else {

          $error = 'Mensagem enviada!';      
          return true;
       }
    }
          //Insira abaixo o email que irá receber a mensagem, o email que irá enviar (o mesmo da variável GUSER), 
          //Email que recebe a mensagem, email que envia a mensagem, o Assunto da mensagem e por último a variável com o corpo do email.

          if (smtpmailer($emaildestinatario, $emailremetente , $nomeremetente, $assunto , $corpodamensagem)) {


    }
          if (!empty($error)) echo $error;
?>

https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php

2

It’s not because the function mail() returned true and the email came out of your server and got to the recipient’s server which means it will be delivered. Until I understand there is no way to verify if the email really arrived successfully to recipient, because the email function is just a "command", who does the job is actually a communication service (SMTP service) that exists on your server.

When you use mail(...);, it sends the command to the "service" and this service only returns the response to the PHP it received its instruction.

The email is in a queue and this can take a long time to send it (varying according to the server), because there may be several emails in the queue of the same server and for this reason there is no way to know if the SMTP communication with the recipient was done, if not your PHP page could get stuck for several minutes.

Therefore in running time there is no way to know if the email was to the recipient, the only answer you will get is whether the SMTP received your instructions or not.

Alternative

Note: More details http://en.wikipedia.org/wiki/Bounce_message#Bouncing_vs. _rejecting

Usually when we send an email and a failure occurs SMTP sends a email for the inbox (inbox) of the recipient itself, in this email contains details of the error, this error log has no standard format, each SMTP service has a format, unfortunately, so using this data is something difficult, but still you can use them (that I know there is nothing ready to use this).

Read an example of this situation on serverfault

  • Good afternoon @Testerapo Do you have any questions? If not, please choose the answer you like and mark as "correct". Grateful.

Browser other questions tagged

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