-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathYiiSendGrid.php
More file actions
executable file
·266 lines (243 loc) · 6.02 KB
/
YiiSendGrid.php
File metadata and controls
executable file
·266 lines (243 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* YiiSendGrid class file.
*
* @author Gustavo Salomé Silva <gustavonips@gmail.com>
*/
/**
* YiiSendGrid is an application component used for sending email through sendgrid.
*
* You may configure it as below. Check the public attributes and setter
* methods of this class for more options.
* <pre>
* return array(
* 'components' => array(
* 'sendgrid' => array(
* 'class' => 'ext.yii-sendgrid.YiiSendGrid',
* 'username'=>'sendgridUsername',//replace with your actual username
* 'password'=>'myP4s$w0rd',//replace with your actual password
* 'viewPath' => 'application.views.mail',
* 'enableLog' => YII_DEBUG,//wheter to log the emails sent
* 'dryRun' => false,//if enabled, it won't actually send the emails, only log
* 'disableSslVerification'=>true,// {@link https://github.com/sendgrid/sendgrid-php#ignoring-ssl-certificate-verification}
* ),
* ...
* )
* );
* </pre>
*
* Example usage:
* <pre>
* $email = Yii::app()->sendgrid->createEmail();
* $email->setHtml('<p>Message content here with HTML</p>')
* ->setSubject('My Subject')
* ->addTo('johnDoe@domain.com')
* ->setFrom(Yii::app()->params['adminEmail']);
* Yii::app()->mail->send($email);
* </pre>
* or just
* <pre>
* $email = Yii::app()->sendgrid->createEmail($htmlBody,$subject,$to,$from);
* Yii::app()->mail->sendgrid->send($email);
* </pre>
*
* @property SendGrid $mailer
* @property string $username
* @property string $password
*/
class YiiSendGrid extends CApplicationComponent
{
/**
* whether to trace emails sent using {@link Yii::log()}. Uses CLogger::LEVEL_TRACE
* @var bool Defaults to YII_DEBUG.
*/
public $enableLog = YII_DEBUG;
/**
* whether to disable sending mail. If set to true, it will only log the email
* @var bool Defaults to false.
*/
public $dryRun = false;
/**
* the path or alias to the location where mail views are stored.
* @var string Defaults to 'application.views.mail'.
*/
public $viewPath='application.views.mail';
/**
* wheter to disable ssl verification
* @boolean defaults to true
*/
public $disableSslVerification=true;
/**
* @var SendGrid SendGrid instance
*/
private $_mailer;
/**
* sendgrid username
* @var string
*/
private $_username;
/**
* sendgrid password
* @var string
*/
private $_password;
/**
* YiiSendGridMail instance
* @var YiiSendGridMail
*/
private $_mail;
/**
* wheter the librarys are registered, so we are able to create many instances and register only once
* @var boolean
*/
private static $_registered=false;
/**
* list of last error messsages
* @var string[]
*/
public $lastErrors=array();
/**
* Calls the {@link registerScripts()} method.
*/
public function init()
{
self::registerLibs();
return parent::init();
}
/**
* Gets the SendGrid adapter instance
* @return SendGrid
*/
public function getMailer()
{
if ($this->_mailer===null)
{
$this->_mailer=new SendGrid($this->getUsername(), $this->getPassword(), array(
"turn_off_ssl_verification" => $this->disableSslVerification
));
}
return $this->_mailer;
}
/**
* get sendgrid username
* @throws Exception if username is not defined
* @return string
*/
public function getUsername()
{
if($this->_username===null)
throw new Exception('Username must be defined');
return $this->_username;
}
/**
* sendgrid username
* @param string $value
*/
public function setUsername($value)
{
$this->_username=$value;
return $this;
}
/**
* get sendgrid password
* @throws Exception if password is not defined
* @return string
*/
public function getPassword()
{
if($this->_password===null)
throw new Exception('Password must be defined');
return $this->_password;
}
/**
* sendgrid password
* @param string $value
*/
public function setPassword($value)
{
$this->_password=$value;
return $this;
}
/**
* /**
* helper to create a new email
* @param string $html html email body
* @param string $subject email subject
* @param string $to to
* @param string $from from
* @return YiiSendGridMail
*/
public function createEmail($html=null,$subject=null,$to=null,$from=null)
{
$mail=new YiiSendGridMail($this->viewPath);
if($html!==null)
$mail->setHtml($html);
if($subject!==null)
$mail->setSubject($subject);
if($to!==null)
$mail->addTo($to);
if($from!==null)
$mail->setFrom($from);
return $mail;
}
/**
* sends the mail and return the result
* @return boolean wheter it was successful
* if not, you can check the errors in {@link $lastErrors}
*/
public function send(YiiSendGridMail $mail)
{
$this->lastErrors=array();
//logs if needed
if($this->enableLog)
$this->log($mail);
//dry run
if($this->dryRun)
return true;
$result=$this->getMailer()->send($mail);
//not a json response, something is wrong. Use it as error
if(!$result instanceof stdClass)
{
$this->lastError=array($result);
return false;
}
if(isset($result->message) && $result->message==='success')
return true;
elseif(isset($result->message) && $result->message==='error')
{
$this->lastErrors=$result->errors;
return false;
}
//what happened?
return true;
}
/**
* register swiftMail and SendGrid librarys autoloaders
*/
protected static function registerLibs()
{
if(!self::$_registered)
{
self::$_registered=true;
//register sendgrid autoloader
require __DIR__.'/vendors/unirest-php/lib/Unirest.php';
require __DIR__.'/vendors/sendgrid-php/lib/SendGrid.php';
require __DIR__.'/vendors/smtpapi-php/lib/Smtpapi.php';
SendGrid::register_autoloader();
Smtpapi::register_autoloader();
//preload extension mail class
require_once __DIR__.'/YiiSendGridMail.php';
}
}
/**
* Logs a YiiSendGridMail using Yii::log.
* @return string log message
*/
protected function log(YiiSendGridMail $mail)
{
$tos=implode(', ', $mail->smtpapi->to);
$headers=implode('', $mail->getHeaders());
$content=$mail->getHtml();
Yii::log("Sending email to {$tos}\n{$headers}\n{$content}", CLogger::LEVEL_TRACE, 'ext.sendgrid.YiiSendGrid');
}
}