使用静态方法为phpmailer创建一个全局方法

时间:2015-07-24 14:08:38

标签: php class phpmailer static-methods

当我实例化静态一个发送给定对象的电子邮件的方法时,我收到此错误。我不确定我的做法是否正确。我在自动加载器中使用了纤薄的框架,我喜欢在我想要的地方使用PDO,我希望在没有设置发送电子邮件的情况下使用phpmailer。

Fatal error: Using $this when not in object context

<?php
    namespace lib;
    use lib\Config;
    use PDO;
    class Core {

        public $dbh;
        public $phpmail;

        private static $instance;

        private function __construct() {

            $dsn = 'mysql:host=' . Config::read('db.host') .
                   ';dbname='    . Config::read('db.basename') .
                   ';port='      . Config::read('db.port') .
                   ';connect_timeout=15';
            $user = Config::read('db.user');
            $password = Config::read('db.password');
            $this->dbh = new PDO($dsn, $user, $password);

            $this->phpmail = new PHPMailer;

        }

        public static function getInstance() {
            if (!isset(self::$instance))
            {
                $object = __CLASS__;
                self::$instance = new $object;
            }
            return self::$instance;
        }

        public static function SendEmail ($fromAddress, $fromName, $toAddress, $isHTML = true, $emailContent) {

            $this->phpmail->From = $fromAddress;
            $this->phpmail->FromName = $fromName;
            $this->phpmail->addAddress($toAddress);

            $this->phpmail->isHTML($isHtml);
            $this->phpmail->Subject = 'Test';
            $this->phpmail->Body = $emailContent;

            if(!$this->phpmail->send())
                return $mail->ErrorInfo;
            else
                return true;
        }
    }

这就是我在我的路由器或模型中调用它的方式

<?php

$mail = \lib\Core::SendEmail('nor-reply@domain.com', 'Webiste', 'user@domain.com', true, $email_content);
?>

1 个答案:

答案 0 :(得分:1)

静态上下文中不提供任何实例字段或方法(通过$this访问)。静态方法只是一个过程(给它一个输入,它给你一个输出)。实例化类时,会调用其构造函数并填充实例变量(因此可以访问)。

由于PHPMailer对象中包含的详细信息,您似乎需要一个实例,因此您需要从static方法中删除SendMail

然后,您需要实例化Core类:

$core = new Core();
$core->SendMail($fromAddress, $fromName, $toAddress, $isHTML = true, $emailContent);

修改

保持SendMail静态的建议方法:

<?php
namespace lib;
use lib\Config;
use PDO;
class Core {

    public $dbh;

    private static $instance;

    private function __construct() {

        $dsn = 'mysql:host=' . Config::read('db.host') .
               ';dbname='    . Config::read('db.basename') .
               ';port='      . Config::read('db.port') .
               ';connect_timeout=15';
        $user = Config::read('db.user');
        $password = Config::read('db.password');
        $this->dbh = new PDO($dsn, $user, $password);

    }

    public static function getInstance() {
        if (!isset(self::$instance))
        {
            $object = __CLASS__;
            self::$instance = new $object;
        }
        return self::$instance;
    }

    public static function SendEmail ($fromAddress, $fromName, $toAddress, $isHTML = true, $emailContent) {

        $phpmail = new PHPMailer();
        $phpmail->From = $fromAddress;
        $phpmail->FromName = $fromName;
        $phpmail->addAddress($toAddress);

        $phpmail->isHTML($isHtml);
        $phpmail->Subject = 'Test';
        $phpmail->Body = $emailContent;

        if(!$phpmail->send())
            return $phpmail->ErrorInfo;
        else
            return true;
    }
}

然后使用:

调用它
Core::SendMail('nor-reply@domain.com', 'Webiste', 
    'user@domain.com', true, $email_content)