douchen5971 2016-10-08 05:52
浏览 179
已采纳

无法从类中加载swiftmailer。 我收到错误:找不到类“Swift_SmtpTransport”

I am implementing a mailing solution where if I call swiftmailer from the main index.php file like :

require_once '../swiftmailer/lib/swift_required.php';
...
$result = $mailer->send($message);

All is great. But when I use the same swiftmailer library in my application class to implement layout/design partition like this:

// File: controller.php

class Index {
      ...
      public function ignition() {
            ...
            spl_autoload_register(array($this, 'myClassLoaderFx'));
      }

      public function myClassLoaderFx($name) {
            $classes = array(
                  'Blah' => '../blah/Blah.php',
                  ...
                  'Swift' => '../swiftmailer/lib/swift_required.php'
            );
            if(!array_key_exists($name, $classes)){
                  die('Class not found');
            }
            require_once $classes[$name];
      }

// File: Mailer.php

class Mailer {
      ...
      public function main($recepient[], $sender, $subject, $body) {
            ...
            $transport = Swift_SmtpTransport::newInstance('xyz.google.com', 465, "ssl")->setUsername($this->username)->setPassword($this->password);
            ...
            $result = $mailer->send($message);
}

Upon execution I get : Class "Swift_SmtpTransport" not found. The library is loaded because I get all Swift class list in the netbeans context help. But, upon execution I get the error. I have done all possible spell checkings. Getting nowhere from here on. Please help.

  • 写回答

1条回答 默认 最新

  • doushao8421 2016-10-08 08:53
    关注

    Looks like your swiftmailer class Swift() is loaded but its incomplete. You can take one of the following approaces to solve this :

    1. Add "extends Swift" to your class declaration in file Mailer.php.

      class Mailer extends Swift {

    Or,

    1. Load the swiftmailer library in a construct() function of Mailer class.

      Remove the following line from myClassLoaderFx() function from controller.php : 'Swift' => '../swiftmailer/lib/swift_required.php'

      And Create a construct for Mailer() class in file Mailer.php public function __construct(){ require_once '../swiftmailer/lib/swift_required.php'; }

    Go with the first approach of using spl_autoload_ function unless you want to keep swiftmailer away from the main application into a managed class. This can come in handy in case you would like to switch to another mailer. You wont have to make changes to controller.php etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题