Search code examples
phpexceptionphpmailer

PHPMailer on exception save as .eml


I did a code that send email but the parameters of SMTP connection needs a user setup, so I do tests for check exceptions in code.

My doubt is there a way to get the message created and save it on .EML format using PHPMailer to try again later ?

        try
    {
        if (!$mail->send())
        {
            $this->setErrorDescription($mail->ErrorInfo);
            toLogAdmin(__CLASS__ . '.' . __FUNCTION__ . ' => Erro ao enviar a mensagem de teste de conexão usando ' . $server_name . ' na porta ' . $server_port . ' e usuario ' . $username . ', erro : ' . $mail->ErrorInfo, true);
            return false;
        }
    }
    catch(Exception $exception)
    {
        $this->setErrorDescription($mail->ErrorInfo);
        toLogAdmin(__CLASS__ . '.' . __FUNCTION__ . ' => Excecao ao enviar a mensagem de teste de conexao de email ' . $server_name . ' na porta ' . $server_port . ' e usuario ' . $username . ', erro : ' . $mail->ErrorInfo, true);
        return false;
    }

Solution

  • You can get a complete RFC822 message by calling getSentMIMEMessage(), however I recommend you don’t do that. Either save all your email params so you can create the message again, or better still, install a local mail server and deliver via that, which will take care of retrying, and everything else that mail servers are for. There’s no point in writing a mail server in PHP, which is almost what you’re proposing.