So, you feel the badness of saving the password in the database, which is very good. Don't want to do that.
The first question that could be asked is whether you are placing your config files in the document root (which means they will be accessible directly from the web). A server error (malfunction or intentional) could cause those files to be displayed as text, revealing your database credentials to everyone viewing it at the time. If you are doing something like bootstrapping, this isn't so much of a concern.
So, once you know where to place it, put it in an .ini
file, for which PHP has very good support out of the box. Save it in a file called config.ini
and place it outside of the document root, so it is not accessible from the web. Also make certain that this config.ini
is not under source control! Don't suck it into your repo! What follows should show you how to abstract away your connection to your mailer and secure credentials in the file outside of your application logic. The following example is for a DB connection but you will see the pattern.
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('../config.ini');
// Try and connect to the database
$connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
// If connection was not successful, handle the error
if($connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
}