sendGrid mail function not working in yii-framework

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I used below code for sendGrid codes for sending mails from my project.

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

I already downloaded the sendGrid package and put it into lib folder in yii.

if I execute the above code i got error like "include(Swift_DependencyContainer.php): failed to open stream: No such file or directory"

if I included the above file i got error like another file need to be include.

Kindly advice on this.

回答1:

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.



回答2:

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


回答3:

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.



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