doy57007 2016-06-05 16:39
浏览 69

PHP - swiftmailer自动加载器无法正常工作

I'm trying to use swiftmailer to send transactional emails but I always get the following error in Email.php, when I try to create swiftmailer's objects:

Warning: require_once(Swift/SmtpTransport.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Tutorials\E-commerce\inc\autoload.php on line 13

Fatal error: require_once(): Failed opening required 'Swift/SmtpTransport.php' (include_path='C:\xampp\htdocs\Tutorials\E-commerce\classes;C:\xampp\htdocs\Tutorials\E-commerce\pages;C:\xampp\htdocs\Tutorials\E-commerce\mod;C:\xampp\htdocs\Tutorials\E-commerce\inc;C:\xampp\htdocs\Tutorials\E-commerce\template;C:\xampp\php\PEAR') in C:\xampp\htdocs\Tutorials\E-commerce\inc\autoload.php on line 13

The thing is, I included composer's and swiftmailer's autoloaders (separately), so it should find the classes. I have a custom autoloader but, if I include swiftmailer's autoloader directly, even if my autoloader doesn't find the classes, the include statement should be triggered and find them right? I can get it to work if I include each class individually but that's not very practical.

This is my dirs structure:

index.php
inc
    config.php
    autoload.php
classes
    SwiftMailer
         vendor
         composer.json
         composer.lock
    Url.php
    Core.php
    Email.php
    Login.php
    etc
pages
    login.php
    basket.php
    etc
etc

Email.php:

<?php

// I tried including them separately (also, php has no problem in finding them):
require_once 'SwiftMailer/vendor/autoload.php';
require_once 'SwiftMailer/vendor/swiftmailer/swiftmailer/lib/swift_required.php';

class Email {

    public function __construct() {
        $this->objTransport = Swift_SmtpTransport::newInstance();
        etc

autoload.php:

<?php
require_once('config.php');

function generic_autoloader($class_name) {
    $class = explode("_", $class_name); 
    $path = implode('/', $class) . '.php';
    require_once($path);

}

spl_autoload_register('generic_autoloader');

config.php:

It's a file where I define several constants (eg: classes directory, pages directory, root path..) and at the end I have this:

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(ROOT_PATH.DS.CLASSES_DIR),
    realpath(ROOT_PATH.DS.PAGES_DIR),
    realpath(ROOT_PATH.DS.MOD_DIR),
    realpath(ROOT_PATH.DS.INC_DIR),
    realpath(ROOT_PATH.DS.TEMPLATE_DIR),
    get_include_path()
)));

I'm not sure if this has anything to do with the problem or not (I'm following a tutorial and I haven't quite understood this part of the code yet), so I'll leave it here:

index.php:

<?php
require_once('inc/autoload.php');

$core = new Core();
$core->run();

Core.php:

<?php

class Core {
    public function run() {
        ob_start();
        require_once(Url::getPage()); // Url::getPage just returns the path of the current page ($_GET['page'])
        ob_get_flush();
    }
}

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongyongju9560 2016-06-13 19:38
    关注

    If there is indeed an SmtpTransport.php file in the right folder, it seems that the error is precisely what it says it is : a wrong file name.

    You should replace

    $this->objTransport = Swift_SmtpTransport::newInstance();
    

    by

    $this->objTransport = SwiftMailer_SmtpTransport::newInstance();
    

    You should also include composer's autoload file (the autoload file inside vendor)

    评论

报告相同问题?

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率