SendGrid function not working in yii-framework

I used below code for sendGrid codes to send letters 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 in the lib folder in yii.

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

If I included the above file, I got an error because another file must be included.

I ask for advice on this.

+1
source share
3 answers

Finally, I make it work. for your link, I list the steps (for myself as well)

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

2) Unzip the folder and place it in the project folder, for example, "app / mail /".

3) create one .php file for mail to send mail in this folder, for example, "app / mail / mail.php".

4) in this 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 the mailsend page. so I write the code in the controller file in Actionmailsend (),

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

just a redirect. this is. mail is sent successfully.

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

  • it is not integrated into yii. we used the mail function to put the sendGrid package folder in the yii project folder and use it.

0
source

SendGrid seems to rely on an include path to load its dependencies. Therefore you must use one or more

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

to add SendGrid to the include path. May be:

 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 problem with the inclusion path. I solved this 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.

+1
source

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

Source: https://habr.com/ru/post/1443788/


All Articles