-
Notifications
You must be signed in to change notification settings - Fork 25
/
Mailer.php
170 lines (156 loc) · 4.4 KB
/
Mailer.php
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
<?php
/**
* @author Bryan Jayson Tan <[email protected]>
* @link http://bryantan.info
*/
namespace bryglen\sendgrid;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\mail\BaseMailer;
use Yii;
/**
* Mailer implements a mailer based on SendGrid.
*
* To use Mailer, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => [
* ...
* 'sendGrid' => [
* 'class' => 'bryglen\sendgrid\Mailer',
* 'username' => 'your_user_name',
* 'password' => 'your password here',
* //'viewPath' => '@app/views/mail', // your view path here
* ],
* ...
* ],
* ~~~
*
* To send an email, you may use the following code:
*
* ~~~
* $sendGrid = Yii::$app->sendGrid;
* $message = $sendGrid->compose('contact/html', ['contactForm' => $form])
* $message->setFrom('[email protected]')
* ->setTo($form->email)
* ->setSubject($form->subject)
* ->send($sendGrid);
* ~~~
*
* Note: you need to pass a parameter in the send() if your component is not `mail`
*
* @see http://sendgrid.com/
* @package bryglen\sendgrid
*/
class Mailer extends BaseMailer
{
/**
* @var string message default class name.
*/
public $messageClass = 'bryglen\sendgrid\Message';
/**
* @var string the username for the sendgrid api
*/
public $username;
/**
*
* @var string the password for the sendgrid api
*/
public $password;
/**
* @var array a list of options for the sendgrid api
*/
public $options = [];
/**
* @var string a json string of the raw response from the sendgrid
*/
private $_rawResponse;
/**
* @var array a list of errors
*/
private $_errors = [];
/**
* @var string Send grid mailer instance
*/
private $_sendGridMailer;
/**
* @return \SendGrid Send grid mailer instance
*/
public function getSendGridMailer()
{
if (!is_object($this->_sendGridMailer)) {
$this->_sendGridMailer = $this->createSendGridMailer($this->username, $this->password, $this->options);
}
return $this->_sendGridMailer;
}
/**
* Create send grid mail instance
* @param string $username the username for the sendgrid api
* @param string $password the password for the sendgrid api
* @return \SendGrid
* @throws \yii\base\InvalidConfigException
*/
public function createSendGridMailer($username, $password, $options)
{
if (!$username) {
throw new InvalidConfigException("Username cannot be empty.");
}
if (!$password) {
throw new InvalidConfigException("Password cannot be empty.");
}
$sendgrid = new \SendGrid($username, $password , $options);
return $sendgrid;
}
/**
* @inheritdoc
*/
public function sendMessage($message)
{
$address = $message->getTo();
if (is_array($address)) {
$address = implode(', ', array_keys($address));
}
$this->setRawResponse($this->getSendGridMailer()->send($message->getSendGridMessage()));
$responseArray = Json::decode($this->getRawResponse());
if (!isset($responseArray['message'])) {
throw new \Exception('Invalid SendGrid response format');
} elseif ($responseArray['message'] === "success") {
// reset the error if success
$this->setErrors(array());
return true;
} elseif (isset($responseArray['errors'])) {
// reset the error if success
$this->setErrors($responseArray['errors']);
return false;
}
}
/**
* @return string get the raw response, this can be a json string or empty string
*/
public function getRawResponse()
{
return $this->_rawResponse;
}
/**
* @param string $value set a raw response, the response get from [[sendMessage()]] is an object, convert it to json
*/
public function setRawResponse($value)
{
$this->_rawResponse = Json::encode($value);
}
/**
* @return array a list of errors, the response get [[sendMessage()]]
*/
public function getErrors()
{
return $this->_errors;
}
/**
* @param array $errors a array of errors
*/
public function setErrors($errors)
{
$this->_errors = $errors;
}
}