Giter VIP home page Giter VIP logo

yii-sendgrid's Introduction

yii-sendgrid extension

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  
 			//'viewPath' => 'application.views.mail',  //alias to the layouts path. Optional  
 			//'enableLog' => YII_DEBUG, //wheter to log the emails sent. Optional  
 			//'dryRun' => false, //if enabled, it won't actually send the emails, only log. Optional  
			//'disableSslVerification'=>true,//ignore verification of SSL certificate  
 		),  
 		//...  
 	)  
 );  

How to use

Examples

$message = Yii::app()->sendgrid->createEmail();  
//shortcut to $message=new YiiSendGridMail($viewsPath);
$message->setHtml('<p>Message content here with HTML</p>')  
	->setSubject('My Subject')  
	->addTo(array('[email protected]'))
	->setFrom('[email protected]');  
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

$message = Yii::app()->sendgrid->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(array($user->email))
	->setFrom(Yii::app()->params['adminEmail']);  
//handle errors
if(!Yii::app()->sendgrid->send($message))
{
	Yii::log("Failed to send email:\n".print_r(Yii::app()->sendgrid->lastErrors,true),CLogger::LEVEL_ERROR);
}

Resources

yii-sendgrid's People

Contributors

gusnips avatar roberto14 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.