簡體   English   中英

PHPmailer-在站點范圍的文件中包含設置

[英]PHPmailer - include settings in site-wide file

我正在使用PHP mailer在整個站點上設置多個腳本-所有這些腳本都包含用於配置電子郵件設置的通用代碼,例如:

$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!!';

等等..

我想一次在站點范圍的“ settings.php”文件中設置此信息? 雖然我可以做$mail->Port = $port; 等等,我想知道是否有更好的方法可以將整個站點中的所有代碼都包含在內,同時仍將信息保留在設置文件中。 到目前為止,我能想到的最好方法是三個文件:

文件1:settings.php

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

文件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; 

文件3:mailer_script.php

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

有沒有更有效的方法可以做到這一點?

您的方法要求您每次使用PHPMailer時都使用變量$mail

我寧願建議您編寫一個包裝器類,該包裝器類將config選項作為一個數組,然后創建一個新的PHPMailer實例,設置給定的選項,然后返回該實例……

或代替包裝器類,您還可以擴展 PHPMailer類,以便它調用其父構造函數,然后設置選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM