SMTP Debug with Phpmailer

Asked

Viewed 2,436 times

0

would like to identify a problem in the following code:

try {

    $mail = new PHPMailer(true);

    $this->setLanguage("br");
    $this->isSMTP();
    $this->SMTPDebug = 4;
    $this->SMTPAuth = true;
    $this->SMTPSecure = 'ssl';
    $this->Host = 'hostname';
    $this->Port = 465;
    $this->CharSet = 'utf-8';

    $mail->setFrom('[email protected]', 'John Doe');
    $mail->Subject = "Um e-mail qualquer";
    $mail->addAddress('[email protected]');
    $mail->isHTML(true);

    $mail->body = "teste";

    $mail->send();

    $_SESSION['email_enviado'] = true;

} catch (Exception $e) {
    $_SESSION['email_enviado'] = false;
    $_SESSION['descricao_erro'] = $mail->ErrorInfo;
}

Well, this email is not being sent, and I can’t see the problem because my SMTP even being at level 4 does not return me anything.

I would like to view only the problem according to SMTP debug.

PS: I omitted for security reasons the username/password of the host, from the account the email will be sent..

  • put $this->Smtpdebug = 1;

  • 1

    I did not reproduce your problem, I can see the Debug data, in my scenario I used your code by changing only $this->Smtpsecure to tls and $this->Port to 587. However I noticed that $mail->body should be $mail->Body, otherwise the email will not be sent due to lack of data in the body of the email.

  • That was exactly the problem. Wow.. And I received no error!!

2 answers

2

Coloque $this->SMTPDebug = 2;
Após o send() coloque, die() ou exit() para interromper o fluxo e assim 
analisar a saída;

Quanto ao erro, usar $this->Body = "mensagem" ou $this->msgHTML("mensagem"), 
seu código faltou o conteúdo da mensagem.

2

To debug in Phpmailer (version 5 and 6), you need to set the $Smtpdebug attribute of the Phpmailer instance after instantiation like this:

$mail = new PHPMailer(true);
...
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Ativar a saída do debug
...

It is necessary to assign the $Smtpdebug attribute with one of the following constants (or integer values between brackets):

SMTP::DEBUG_OFF (0): Desativar o debug (você pode deixar sem preencher este valor, uma vez que esta opção é o padrão).
SMTP::DEBUG_CLIENT (1): Imprimir mensagens enviadas pelo cliente.
SMTP::DEBUG_SERVER (2): similar ao 1, mais respostas recebidas dadas pelo servidor (esta é a opção mais usual).
SMTP::DEBUG_CONNECTION (3): similar ao 2, mais informações sobre a conexão inicial - este nível pode auxiliar na ajuda com falhas STARTTLS.
SMTP::DEBUG_LOWLEVEL (4): similar ao 3, mais informações de baixo nível, muito prolixo, não use para debugar SMTP, apenas em problemas de baixo nível.

Observing: although the code error was found by @oliveira ("$mail->body must be $mail->Body"), I believe the answer to the question should have been completed. So I posted an addendum, once searching for this information, I fell into this question first.

  • 1

    I think it’s cool you complete the answer (the idea of the site is this, a question, several answers), I gave +1 because it complements with extra knowledge. It is worth noting that the answer of Leandro already makes Smtpdebug = SMTP::DEBUG_SERVER (the number 2 is exactly that) but it is really better to use the definitions of easy understanding than that Magic Numbers

  • @Bacco I agree. He answered correctly with the value 2. But, I decided to put this complement with the constants and their meanings, because that’s what I was looking for and I fell here. So if others have the same doubt as me, there’s one more reference. Even more that in the official documentation of Phpmailer I did not find the reference for these constants. Look: https://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#property_SMTPDebug

  • 1

    It is worth suggesting to the people of PHP Mailer to document (or push, if the Docs are in a Repo as well). For now, I could add a pro source link in the reply, where it has the definitions: https://github.com/PHPMailer/PHPMailer/blob/master/src/SMTP.php

Browser other questions tagged

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