Sendgrid email component

yii-sendgrid extension

View on GitHub

Download now

YiiSendGrid is an application component used for sending email through sendgrid.
It's a wrapper for SendGrid php library

You may configure it as below. Check the attributes of YiiSendGrid class and YiiSendGridMail class for more options.


  • PHP 5.3+


Download the file and extract to protected/extensions (or anywhere you like, but then adjust the example accordingly)

Add the component to your configuration file (usually protected/configs/main.php) as below:

return array(  
    'components' => array(  
        'sendgrid' => array(  
            'class' => 'ext.yii-sendgrid.YiiSendGrid', //path to YiiSendGrid class  
            'username'=>'myUsername', //replace with your actual username  
            'password'=>'myP4s$w0rd', //replace with your actual password  
            //alias to the layouts path. Optional  
            //'viewPath' => 'application.views.mail',  
            //wheter to log the emails sent. Optional  
            //'enableLog' => YII_DEBUG, 
            //if enabled, it won't actually send the emails, only log. Optional  
            //'dryRun' => false, 
            //ignore verification of SSL certificate  

How to use

Examples of use

$message = Yii::app()->sendgrid->createEmail();  
//shortcut to $message=new YiiSendGridMail($viewsPath);  
$message->setHtml('<p>Message content here with HTML</p>')  
    ->setSubject('My Subject')   

or just

$message = Yii::app()->sendgrid->createEmail($htmlBody,$subject,$to,$from);  

A more real life and complete example using a view and optionally a layout

/* @var $sg YiiSendGrid */
$message = $sg->createEmail();  
//set view variable $user  
    ->setView('signup') //view located in YiiSendGrid::$viewPath. Defaults to protected/views/mail  
        'user'=>$user//my User model. Pass it to the view as $user, same way controller does  
// optionally you can use a layout  
$message->setSubject('Welcome to '.Yii::app()->name.'!')  
//handle errors
    Yii::log("Failed to send email:\n".print_r($sg->lastErrors,true) ,CLogger::LEVEL_ERROR);


Total 1 comment

#20111 report it
manjunathsonju at 2017/08/04 04:58am
problem in locating file

I have set all the configuration. Despite that it is showing the warning message of not finding file HttpMethod.php.

Leave a comment

Please to leave your comment.

Create extension