Issue
I am using PHPMailer in my website to send emails to users when they create an account, and I want it to echo error messages back to the user. Is there a way I can customize the error messages it returns (on error)? (basically what shows up as the value of $mail->ErrorInfo
)
Solution
You can to some extent by creating your own language file, then asking PHPMailer to use it:
$mail->setLanguage('xx', '/path/to/languagefiles/');
These params are then used to construct a path to the file according to a fixed pattern:
$lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php';
So you would need to create your translation file in:
/path/to/languagefiles/phpmailer.lang-xx.php
Note that the en
language code is treated specially as the English language strings are built-in to the PHPMailer class, and not kept in an external file, so to create an arbitrary custom language, use a placeholder language code as I've done here with xx
.
See the files in the language
folder for the format and expected values of the language files. It's along these lines:
$PHPMAILER_LANG['authenticate'] = 'Erreur SMTP : échec de l\'authentification.';
$PHPMAILER_LANG['connect_host'] = 'Erreur SMTP : impossible de se connecter au serveur SMTP.';
$PHPMAILER_LANG['data_not_accepted'] = 'Erreur SMTP : données incorrectes.';
You only need to define the strings that you want to override, as they are overlaid on top of the default English translations.
You can also inject a debug handler and use that to process error messages for you – see the Debugoutput
property in the source code for examples.
Answered By - Synchro Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.