PHPmailer - 包括站点范围文件中的设置

I am setting up several scripts across a site with PHP mailer - all of which include common code to configure email settings, such as:

$mail = new PHPMailer;
$mail->isSMTP();                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';   // Specify main and backup server
$mail->Port = 587;                    // Set port
$mail->SMTPAuth = true;           // Enable SMTP authentication
$mail->Username = 'me@mail.com';
$mail->Password = 'PASSWORD'; 
$mail->Subject  = "My subject";
$mail->Body = 'Message here!!';

etc..

I would like to set this information once in a site-wide 'settings.php' file? Whilst I could do $mail->Port = $port; etc, I wondered if there is a better way to include all the code across the site, whilst still having the information in the settings file. The best way I can think of so far is three files:

File 1: settings.php

$host = 'smtp.gmail.com';
$port = 587;
$SMTPAuth = true;
$Username = 'me@mail.com';
$Password = 'PASSWORD';

File 2: mailer_settings.php

include('settings.php');

$mail->isSMTP();                      // Set mailer to use SMTP
$mail->Host = $host;      // Specify main and backup server
$mail->Port = $port;                      // Set port
$mail->SMTPAuth = $SMTPAuth;              // Enable SMTP authentication
$mail->Username = $Username;
$mail->Password = $Password; 

File 3: mailer_script.php

$mail = new PHPMailer;
include('mailer_settings.php');
$mail->Subject  = "My subject";
$mail->Body = 'Message here!!';

Is there an even more efficient way to do this?

You approach requires you to use the variable $mail every time you use PHPMailer.

I’d rather suggest you writer a wrapper class, that takes config options as an array, and then creates a new PHPMailer instance, sets the options it was given, and returns the instance …

Or instead of a wrapper class you could also extend the PHPMailer class, so that it calls its parent constructor and then sets the options.