Looking for help with my PHPMailer code if possible please.
Overall, it works fine, what I’m struggling to capture is the specific error messages.
if (!$mail->send()) {
echo "Mail <font color=red><b>Not</font></b> Sent to $address - Error: $mail>ErrorInfo<br>";
}
else {
echo "Mail <font color=green><b>Successfully</font></b> Sent to $address<br>";
}
This works okay, but the ErrorInfo I receive for an Invalid address for example is:You must provide at least one recipient email address
Whereas if I look in the actual debug log, I can see an actual error:
Invalid address: (bcc): steven.rothera.com
Can anyone help me get it so I can echo the actual error message please?
I’ve looked at a bunch of other posts to try help me get to the bottom of this but no luck so far.
To cover off some of the code that is present:
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
$mail = new PHPMailer(true);
$mail->SMTPDebug = 2;
$mail->Debugoutput = function($str, $level) {
$fp = fopen('/var/log/phpmailer.log','a');
fwrite($fp, date('d-m-Y H:i:s') . "t" . $str);
fclose($fp);
};
I have tried to use exceptions also, but this didn’t show me any errors:
try {
$mail->send();
echo 'Message sent!';
} catch (Exception $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
3
Answers
Here’s an updated version of your code with some modifications and additional suggestions for debugging:
Make sure to do the following:
$mail->ErrorInfo
You can get the error message, try itPHPMailer can only report the errors it knows about i.e. the errors which are returned via SMTP. If your code is not reflecting what the MTA is logging, then the first step would be to look at the SMTP exchange. If the MTA is writing important stuff to the log which it is not telling the client about then your only course of action is to change the MTA – not your code.
Turn the debugging up to 4 and if this still does not answer your question show us the SMTP exchange