PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How to customize PHPMailer error messages

 July 07, 2022     php, phpmailer     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,213,060

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing