Mail function in php

3

I'm using php's mail function and would like to check if the emails were successfully sent, but I'm not sure how I can do it. How could I verify that the email was successfully sent?

    
asked by anonymous 30.12.2014 / 21:10

3 answers

3

You asked if you have any idea if it was sent, has, and I advise you to do 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!
}

link

É 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 suppress warnings, you can use:

if (!@mail(...))

3 - In the end, I advise you to use e-mail firing through the PHPMailer () class. There are a hundred tutorials on this. The only difficulty you will have is configuring the class with the ports and 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;
?>

link

    
31.12.2014 / 00:23
1

As far as I know, there is no way to verify that the email actually reached the recipient , because the email function is just a "command", who does the work on Truth is a communication 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 that it received its instruction.

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

So at runtime you can not tell if the email was to the recipient, the only response you will have is whether SMTP has received your instructions or not.

Alternative

  

Note: More details link

Generally when we send an email and a failure occurs, SMTP sends an email to the recipient's Inbox, this email contains details of the error, this log error has no default format, each SMTP service has a format, unfortunately, therefore using this data is somewhat difficult, but still you can use them (as far as I know there is nothing ready to use it ).

Read an example of this at serverfault

    
30.12.2014 / 21:27
0

To know for sure you will have to have access to the EXIM report for example or any other report of similar software, taking into account that your server is LINUX.

If you have a hosting that gives you access via VHM / CPanel I have made a class that gives you the result in a friendly way. It provides all the shipping information according to the message ID, ie whether it was sent, rejected or in the delivery queue.

link

    
31.12.2014 / 13:54