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.
Requirements ¶
- PHP 5.3+
Instalattion ¶
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
//'disableSslVerification'=>true,
),
//...
)
);
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')
->addTo('johnDoe@domain.com')
->setFrom('myemail@mydomain.com');
Yii::app()->sendgrid->send($message);
or just
$message = Yii::app()->sendgrid->createEmail($htmlBody,$subject,$to,$from);
Yii::app()->sendgrid->send($message);
A more real life and complete example using a view and optionally a layout
/* @var $sg YiiSendGrid */
$sg=Yii::app()->sendgrid;
$message = $sg->createEmail();
//set view variable $user
$message
->setView('signup') //view located in YiiSendGrid::$viewPath. Defaults to protected/views/mail
->setHtml(array(
'user'=>$user//my User model. Pass it to the view as $user, same way controller does
));
// optionally you can use a layout
//$message->layout='application.views.layouts.email';
$message->setSubject('Welcome to '.Yii::app()->name.'!')
->addTo($user->email)
->setFrom(Yii::app()->params['adminEmail']);
//handle errors
if(!$sg->send($message))
{
Yii::log("Failed to send email:\n".print_r($sg->lastErrors,true) ,CLogger::LEVEL_ERROR);
}
User Contributed Notes 1
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
If you have any questions, please ask in the forum instead.
Join the conversation to share a note.