Php sendGrid邮件功能在yii框架中不工作

Php sendGrid邮件功能在yii框架中不工作,php,yii,sendgrid,Php,Yii,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-

我使用下面的代码发送我的项目中的邮件

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);
require_once(YII_BASE_PATH./lib/sendgrid php/sendgrid.php);
require_once(YII_BASE_PATH./lib/sendgrid php/sendgrid_loader.php);
$sendgrid=新的sendgrid('uname','pwd');
$mail=new SendGrid\mail();
$mail->addTo($mail)xxxxxxxxxx@gmail.com')->
设置自('xxxyyyy5@yahoo.co.in')->
setSubject('主题在这里')->
setText(“你好,世界!”)->
setHtml(“你好,世界!”);
$sendgrid->smtp->send($mail);
我已经下载了sendGrid包,并将其放入yii中的lib文件夹中

如果我执行上面的代码,我会得到类似于“include(Swift\u DependencyContainer.php)”的错误:无法打开流:没有这样的文件或目录“

如果我包含上面的文件,我会得到错误,就像需要包含另一个文件一样


请对此提出建议。

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

Yii::setPathOfAlias()
Yii::import()
语句将SendGrid添加到包含路径。也许:

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

我使用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'));

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

最后我让它起作用了。为供参考,我列出了以下步骤(也为我自己)

1) 我们需要从下载sendgrid php包

2) 解压文件夹并放置在项目文件夹中,如“app/mail/”

3) 在“app/mail/mail.php”这样的文件夹中创建一个用于邮件发送的mail-for-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>');
?>
只是重定向。就这样。邮件发送成功

  • 此处位于::getUrl()-用于获取baseurl

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


    • 以下是对我有效的方法:

      // 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/');
      

      echo
      YII_BASE_路径
      。我认为它的值不是您所期望的。@PLB yi_BASE_PATH返回基本路径,我给出了像defined('yi_BASE_PATH')或define('yi_BASE_PATH',dirname(FILE));在index.phpI中,不要认为问题出在YII_BASE_路径上:2个require_一旦工作(错误是关于Swift_DependencyContainer.php,而不是SendGrid.php或SendGrid_loader.php)。我删除了require_一次并使用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::setPathFalias('Libs',Yii_BASE_PATH.'/lib');Yii::setPathFalias('SendGrid',Yii_BASE_PATH.'/lib/SendGrid php/SendGrid');Yii::导入('SendGrid.*');Yii::import('Libs.sendgrid php.sendgrid',true);编辑:新问题是SendGrid命名空间绑定到“SendGrid php/SendGrid”文件夹。Yii依赖路径别名来解析名称空间的类。@Ravichandran您在PHP 5.3+上吗?@Swift是的。我使用的是PHP5.3+,不幸的是,您的答案与Yii无关,也无助于以任何方式解决原始问题。
      // 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/');