【问题标题】:PHPMailer 5.2.x - Centrally set PHPMailer SMTPAuth credentialsPHPMailer 5.2.x - 集中设置 PHPMailer SMTPAuth 凭据
【发布时间】:2015-07-29 18:21:37
【问题描述】:

我们继承了一个较旧的 PHP 应用程序。这是一个以怪异风格编码的在线商店应用程序。每个邮件程序都已即时编码到响应式 PHP 文件中。有很多文件可以保存发送邮件的代码。 问题是,新的网络服务器使用一个要求 SMTPAuth 接受邮件发送的邮件服务器。该代码不提供任何 SMTPAuth 凭据,因为以前的主机没有使用它。

脚本使用 PHPMailer。 我的问题是,是否有任何方法可以集中设置 PHPMailer 在发送邮件时始终自动使用的 SMTPAuth 凭据(主机、用户名、密码)?

我不想接触外部代码并更改每个脚本,因此它使用 SMTPAuth,例如:

$mail->IsSMTP = true;
$mail->Host = "blah";
$mail->Username = "xxx";

等等。 我希望我的意图很明确。有问题请追问。

【问题讨论】:

    标签: phpmailer smtp-auth


    【解决方案1】:

    我已经解决了。

    只需在公共变量的 class.phpmailer.php 中设置值:

    $Mailer // to "smtp"
    $Host // to the mail host
    $SMTPAuth // to true
    $Username // to the mail accounts username
    $Password // to the mail accounts password
    

    就是这样。

    【讨论】:

    • 不要这样做;您永远不需要编辑库的内容。
    【解决方案2】:

    不要编辑原始文件 - 否则当您更新 PHPMailer 时,您的代码会中断 - 改为子类,如下所示:

    class myMailer extends PHPMailer {
        public function __construct($debug = false) {
            parent::__construct($debug);
            $this->Username = 'username';
            $this->Password = 'password';
            $this->isSMTP();
            $this->SMTPAuth = true;
        }
    }
    

    然后在需要实例时使用它,所以不要说

    $mail = new PHPMailer;
    

    $mail = new myMailer;
    

    这种方法是您应该始终使用库的方式;它不是特定于 PHPMailer 的。

    【讨论】:

    • 谢谢同步。这样做!现在我必须编辑每个发送邮件的 PHP 模板。但你的方式要好得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多