What are some best practices for handling SMTP connection details securely in PHP scripts?

When handling SMTP connection details in PHP scripts, it is crucial to ensure that sensitive information such as usernames and passwords are securely stored and not exposed in the code. One best practice is to store these details in environment variables or a configuration file outside of the web root, and then access them securely within the script.

// Load SMTP connection details from a separate configuration file
$config = parse_ini_file('/path/to/smtp_config.ini');

// Set up SMTP connection using the retrieved details
$transport = (new Swift_SmtpTransport($config['host'], $config['port']))
  ->setUsername($config['username'])
  ->setPassword($config['password']);

// Create the Mailer using the SMTP transport
$mailer = new Swift_Mailer($transport);

// Use the $mailer object to send emails securely