sendGrid mail function not working in yii-framework

放肆的年华 提交于 2019-12-06 21:53:26

It seems that SendGrid relies on include path to load its dependencies. So you must use one or several

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

statements to add SendGrid to the include path. Maybe :

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

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

I use Zend_Mail instead of SendGrid, but I had the same kind of include path problem. I've solved it by using these statements :

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

I think the solution to your problem is similar.

Here is what works for me:

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

finally i make it works. for your reference i list down the steps (for myself also),

1) we need to download the sendgrid-php pack from https://github.com/sendgrid/sendgrid-php/downloads

2) Unzip the folder and placed in your project folder like " app/mail/".

3) the create one .php file for mail for mail sending in that folder like " app/mail/mail.php ".

4)in that file,

    <?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) i need to send mail when i redirect to mailsend page. so i write code in controller file in Actionmailsend(),

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

just redirection. that's it. mail send successfully.

  • here AT::getUrl() - used for get baseurl.

  • it's not integrated to yii. we used mail functionality by to place the sendGrid package folder into inside the yii project folder and used it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!