php - 在没有框架的情况下使用 Swiftmailer

标签 php swiftmailer

我想使用 Swiftmailer 作为独立库,而不使用 Symfony、Laravel、Yii、composer 等。

根据Swiftmailer website包含该库的方法是调用 Composer 的 autoload.php,但是在本例中我没有使用 Composer,并且想知道需要包含/vender/swiftmailer 中的哪些基本文件才能使用该库而不是 require_once '/path/to/vendor/autoload.php';

我查看了 Swiftmailer Google Group ,但它已经停产了,并说要到这里来。

谢谢

最佳答案

正如您所发现的,如果您将 Swiftmailer 放入一个没有任何帮助加载的网站中,您将需要为外部依赖项添加您自己的自动加载器。

Swiftmailer v6.1.0 有两个: https://github.com/egulias/EmailValidator 和另一个 https://github.com/doctrine/lexer

包括 swiftlib/swift_required.php 会自动加载所有 swift 类。您可以将其他文件的自动加载添加到同一文件中。下面是一个示例,其中两个依赖项已复制到 swiftlib 文件夹的子目录中:

spl_autoload_register(function ($class) {

    $load_deps = [ 
        'Egulias\\EmailValidator\\' => __DIR__ . '/EmailValidator/',
        'Doctrine\\Common\\Lexer\\' => __DIR__ . '/Doctrine/Common/Lexer/',
    ];

    foreach ($load_deps as $prefix => $base_dir) {
        // does the class use the namespace prefix?
        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) == 0) {

            // get the relative class name
            $relative_class = substr($class, $len);

            // replace the namespace prefix with the base directory, replace namespace
            // separators with directory separators in the relative class name, append
            // with .php
            $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

            // if the file exists, require it
            if (file_exists($file)) {
                require $file;
            }
        }
    }
});

关于php - 在没有框架的情况下使用 Swiftmailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44467212/

相关文章:

php - Yii2 邮件程序返回错误设置未知属性: yii\swiftmailer\Mailer::mailer

php - PHP 中的安全文件层次结构

php - Symony2 控制台命令删除缓存文件夹,更改权限

symfony - 从 parameters.ini 传递 "null"加密并在 config.yml 中使用它

php - Symfony2 Controller 中未捕获 Swiftmailer 异常

php - Symfony 3.3 和 Swiftmailer - 由服务器延迟的 Controller 创建和发送的邮件

php - Symfony - 扩展已注册

php - WooCommerce Blocks - 向产品网格添加简短的产品描述

php - 检索多个数据以 ListView ?

php - 使用 X-Send 模块提供 css、javascript 和图像文件