【问题标题】:sendGrid mail function not working in yii-frameworksendGrid 邮件功能在 yii 框架中不起作用
【发布时间】:2012-08-08 10:27:06
【问题描述】:

我使用下面的代码作为 sendGrid 代码,用于从我的项目发送邮件。

require_once(YII_BASE_PATH . "/lib/sendgrid-php/SendGrid.php");
require_once(YII_BASE_PATH . "/lib/sendgrid-php/SendGrid_loader.php");   
$sendgrid = new SendGrid('uname', 'pwd');
        $mail = new SendGrid\Mail();
        $mail->addTo('xxxxxxxxxx@gmail.com')->
               setFrom('xxxyyyy5@yahoo.co.in')->
               setSubject('Subject goes here')->
               setText('Hello World!')->
               setHtml('<strong>Hello World!</strong>');
       $sendgrid->smtp->send($mail);

我已经下载了sendGrid包,放到yii的lib文件夹中了。

如果我执行上面的代码,我会得到类似"include(Swift_DependencyContainer.php): failed to open stream: No such file or directory"的错误

如果我包含了上述文件,我会遇到错误,例如需要包含另一个文件。

请就此提出建议。

【问题讨论】:

  • 回显YII_BASE_PATH。我认为它的价值不是你所期望的。
  • @PLB YII_BASE_PATH 返回基本路径,我给出了定义('YII_BASE_PATH')或定义('YII_BASE_PATH',dirname(FILE));在 index.php 中
  • 我认为问题不在于 YII_BASE_PATH :2 require_once 工作(错误是关于 Swift_DependencyContainer.php,而不是 SendGrid.php 或 SendGrid_loader.php)。

标签: php yii sendgrid


【解决方案1】:

SendGrid 似乎依赖包含路径来加载其依赖项。所以你必须使用一个或几个

Yii::setPathOfAlias()
Yii::import()

将 SendGrid 添加到包含路径的语句。也许:

Yii::setPathOfAlias('SendGrid', YII_BASE_PATH'.'/lib/sendgrid-php');
Yii::import('SendGrid.*');

见:http://www.yiiframework.com/doc/api/1.1/YiiBase#import-detail

我使用 Zend_Mail 而不是 SendGrid,但我遇到了同样的包含路径问题。 我已经通过使用这些语句解决了它:

Yii::setPathOfAlias('zf', '/path/to/zend/library/folder');
Yii::import('zf.*');
Yii::import('zf.Zend.Loader.Autoloader', true);
Yii::registerAutoloader(array('Zend_Loader_Autoloader', 'autoload'));

我认为你的问题的解决方案是相似的。

【讨论】:

  • 我删除了 require_once 并使用了 Yii::import。但仍然面临同样的问题。类似“include(/var/www/html/test/project/lib/sendgrid-php/Mail.php)的错误:无法打开流:没有这样的文件或目录”
  • 是的,你是对的。位于“ lib\sendgrid-php\SendGrid\Mail.php ”路径中的文件。由于“ $mail = new SendGrid\Mail(); ”这一行而发生错误。我如何在 SendGrid 的子目录中调用这个 Mail.php?
  • 快速而肮脏的修复(未测试):Yii::setPathOfAlias('Libs', YII_BASE_PATH.'/lib'); Yii::setPathOfAlias('SendGrid', YII_BASE_PATH.'/lib/sendgrid-php/SendGrid'); Yii::import('SendGrid.*'); Yii::import('Libs.sendgrid-php.SendGrid', true);编辑:新问题是 SendGrid 命名空间与“sendgrid-php/SendGrid”文件夹相关联。 Yii 依赖路径别名来解析命名空间类。
  • @Ravichandran 你在 PHP 5.3+ 上吗?
  • @Swift 是的。我正在使用 PHP 5.3+
【解决方案2】:

这对我有用:

// Define constant which SendGrid uses for referencing the path
define('ROOT_DIR', Yii::app()->basePath . '/lib/sendgrid-php/');
// Prevent swift_required from executing
define('SWIFT_REQUIRED_LOADED', true);

// Import SendGrid and Swift libraries
Yii::import('application.lib.sendgrid-php.SendGrid');
Yii::import('application.lib.sendgrid-php.lib.swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift', 'autoload'));
Yii::import('application.lib.sendgrid-php.lib.swift.swift_init', true);

// Register namespace
Yii::setPathOfAlias('SendGrid', Yii::app()->basePath . '/lib/sendgrid-php/SendGrid/');

【讨论】:

    【解决方案3】:

    最后我让它工作了。供您参考,我列出了步骤(也为我自己),

    1)我们需要从https://github.com/sendgrid/sendgrid-php/downloads下载sendgrid-php包

    2) 解压文件夹并放入你的项目文件夹,如“app/mail/”。

    3) 为邮件创建一个 .php 文件,以便在该文件夹中发送邮件,例如“app/mail/mail.php”。

    4) 在那个文件中,

        <?php
            session_start();
            define("ROOT_DIR", __dir__ . DIRECTORY_SEPARATOR);
    
            function sendGrid_loader($string) {
                if (preg_match("/SendGrid/", $string)) {
                    $file = str_replace('\\', '/', "$string.php");
                    require_once ROOT_DIR . $file;
                }
            }
    
            spl_autoload_register("sendGrid_loader");
    
            $sendgrid = new SendGrid('sendgrid_username', 'sendgrid_password');
            $mail = new SendGrid\Mail();
        $mail->addTo('foo@bar.com')->
               setFrom('me@bar.com')->
               setSubject('Subject goes here')->
               setText('Hello World!')->
               setHtml('<strong>Hello World!</strong>');
    ?>
    

    5) 当我重定向到邮件发送页面时,我需要发送邮件。所以我在 Actionmailsend() 的控制器文件中编写代码,

    " header("Location:".AT::getUrl()."/mail/mail.php"); ".
    

    只是重定向。就是这样。邮件发送成功。

    • 此处为 AT::getUrl() - 用于获取 baseurl。

    • 它没有集成到 yii。我们使用邮件功能将 sendGrid 包文件夹放入 yii 项目文件夹中并使用它。

    【讨论】:

    • 很遗憾你的回答与 Yii 无关,也无助于解决原来的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2014-05-19
    相关资源
    最近更新 更多