sendGrid 邮件函数在 yii-framework 中不起作用



我使用以下代码作为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 SendGridMail();
        $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"

如果我包含上述文件,

则出现错误,例如需要包含另一个文件。

请就此提供建议。

似乎 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'));

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

以下是对我有用的方法:

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

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

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

2)解压缩文件夹并放置在您的项目文件夹中,例如" app/mail/"。

3)为邮件创建一个.php文件,以便在该文件夹中发送邮件,例如" 应用程序/邮件/邮件.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 SendGridMail();
    $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() - 用于获取基本网址。

  • 我们使用邮件功能将 sendGrid 包文件夹放入 yii 项目文件夹内部并使用它。

最新更新