Troubleshooting
How to turn on PHPMailer debug mode and diagnose SMTP errors
PHPMailer debug mode shows the SMTP conversation between the application and the mail server. It is the most direct way to tell a connection, TLS, authentication or message rejection failure apart.
Turn it on only while you reproduce the problem, with a test recipient and test content. The transcript may include senders, recipients, headers and the message body. Levels 0 to 3 hide the SMTP username and password, but SMTP::DEBUG_LOWLEVEL may show them. Don't publish the full output.
Turn on the recommended diagnostic level
In PHPMailer 6 and 7, import the classes and use the SMTP::DEBUG_SERVER constant, equivalent to level 2 in older versions:
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; $mail = new PHPMailer(true); $mail->isSMTP(); $mail->SMTPDebug = SMTP::DEBUG_SERVER;
Add these options to the PHPMailer instance your application already sends with, after loading its classes and before calling send(). The snippet isn't a complete sending program: keep your server, authentication and test recipient settings.
This level logs both the commands sent by the client and the server's responses. Level 3 adds information about the initial connection and can help when there is a timeout. Level 4 shows low-level traffic, can reveal the SMTP credentials and shouldn't be used except to analyse a protocol problem in a controlled environment.
If you don't want the transcript shown in the HTML response and PHP's error_log already points to a private log outside public_html, you can send it there:
$mail->Debugoutput = 'error_log';
Don't use this output without first checking where PHP writes: it could end up in a shared log or one that can be downloaded from the web.
When you finish, turn off diagnostics and delete the log once you no longer need it:
$mail->SMTPDebug = SMTP::DEBUG_OFF;
If you send with a Hostsuar mailbox
Use the details shown in cPanel > Email Accounts > Connect Devices next to the address: its SMTP server, the full address as the username and the mailbox password. The server name must match its certificate; do not replace it with mail. and your domain if that is not the one shown there.
$mail->Host = 'smtp-server-shown-in-cpanel'; $mail->SMTPAuth = true; $mail->Username = 'tu@tudominio.com'; $mail->Password = 'mailbox-password'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465;
If your account offers port 587, change the last two lines to PHPMailer::ENCRYPTION_STARTTLS and 587. Port and encryption go together: 465 with ENCRYPTION_SMTPS and 587 with ENCRYPTION_STARTTLS. You will find the rest of the values in email connection details.
How to read the output
Look for the server's last response before the failure:
- No
220response appears: check the server name, DNS, port and possible network blocks. STARTTLSor certificate validation fails: check that TLS and port match, that the OpenSSL extension is enabled and that the system has up-to-date CA certificates. Don't turn off TLS verification to hide the error.535or similar response: the server rejected authentication. Check the full username, the password and the method the provider supports.550or5.7.xresponse: the server received the request but rejected the sender, the recipient or the message. Keep the full text, as it usually states the policy applied.250response at the end ofDATA: the server accepted the message for processing. An earlier250, for example afterEHLOorMAIL FROM, doesn't confirm the complete send. Acceptance doesn't guarantee delivery to the inbox either. If it then shows up in spam, continue with the deliverability guide.
To see how another server receives the message, with SPF, DKIM, DMARC and a score from 0 to 10, send a test to the address our email test gives you.
Common messages
| What you see | What it means | What to do |
|---|---|---|
SMTP ERROR: Failed to connect to server: Connection timed out (110) |
The connection does not reach the server. | Check the server name and port. If they are correct, something is blocking outgoing traffic on that port on the network you are sending from. |
SMTP ERROR: Failed to connect to server: Connection refused (111) |
The server answers, but nothing is listening on that port. | Use 465 or 587, with the encryption that goes with it. |
php_network_getaddresses: getaddrinfo followed by failed |
The server name does not exist in DNS. | Fix Host: it is almost always a typo. |
Peer certificate CN=… did not match expected CN=… (at level 3) |
The certificate is not for the name you are connecting to. | Use the server shown in Connect Devices, not another name that points to the same machine. Do not turn off verification. |
535 Incorrect authentication data followed by SMTP Error: Could not authenticate. |
Wrong username or password. | The username is the full address and the password is the mailbox's, which may differ from your cPanel one. |
SMTP Error: Could not connect to SMTP host. |
It is PHPMailer's final warning, not the cause. | Look for the SMTP ERROR line before it or repeat at level 3. |
The official PHPMailer troubleshooting guide recommends using current examples, reading the full SMTP transcript and checking the network before blaming the library.
Useful details for reproducing the failure
Keep the PHPMailer version, the PHP version, the SMTP host and port, the encryption type and the full error message. Replace any private address, token or credential first. A screenshot that only shows SMTP connect() failed usually hides the response that lets you find the cause.
You may also find useful
- .eml files blocked by mail servers Why the server rejects an email with an .eml attachment and how to forward it so that it arrives: no...
- Authentication error when sending email from Outlook or Thunderbird If Outlook or Thunderbird will not send, keep asking for the password or return error 535, enable ou...
- Check whether Avast is blocking SMTP sending in Outlook or Thunderbird Isolate an SMTP timeout by checking the connection first and running a temporary test of Avast's Mai...
- Check whether an email account is sending authenticated spam Tell bounces caused by a forged sender apart from authenticated sending, and contain a compromised a...
Something wrong or out of date? Tell us and we will review it.