-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.php
141 lines (132 loc) · 4.56 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
<?php
/**
* Mail.php
*
* PHP version 5.6+
*
* @author Rahul K <abcdemo87@gmail.com>
* @copyright 2010-2017 Rahul K
* @license http://www.devtuts.in/license license
* @version XXX
* @link http://www.devtuts.in
* @package rahul\sendgrid
*/
namespace rahul\sendgrid;
use Exception;
use SendGrid\Mail\Mail;
use SendGrid;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\mail\BaseMailer;
/**
* This component allow user to send an email
*
* @author Rahul K <abcdemo87@gmail.com>
* @copyright 2010-2017 Rahul K
* @license http://www.devtuts.in/license license
* @version XXX
* @link http://www.devtuts.in
* @package rahul\sendgrid
* @since XXX
* @todo implement batch messages using API
*/
class Mailer extends BaseMailer
{
/**
* @var string Sendgrid API Key
*/
public $token;
/**
* @var string Sendgrid login
*/
public $user;
/**
* @var string Sendgrid password
*/
public $password;
/**
* @var array options as defined in https://github.com/sendgrid/sendgrid-php#usage
*/
public $options;
/**
* @inheritdoc
*/
public $messageClass = 'rahul\sendgrid\Message';
/**
* @param Message $message
* @since XXX
* @throws InvalidConfigException
*/
public function sendMessage($message)
{
try {
if (($this->token === null)) {
throw new InvalidConfigException('Token or login/password are missing');
}
$client = null;
if ($this->token !== null) {
$client = new SendGrid($this->token, $this->options);
} elseif(($this->user !== null) && ($this->password !== null)) {
$client = new SendGrid($this->user, $this->password, $this->options);
}
if ($client === null) {
throw new InvalidParamException('Email transport must be configured');
}
$sendGridMail = new Mail();
$replyTo = $message->getReplyTo();
if ($replyTo !== null) {
$sendGridMail->setReplyTo($replyTo);
}
$sendGridMail->setFrom($message->getFrom());
if ($message->getFromName() !== null) {
$sendGridMail->setFrom($message->getFrom(),$message->getFromName());
}
foreach($message->getTo() as $email => $name) {
$sendGridMail->addTo($email, $name);
}
foreach($message->getCc() as $email => $name) {
$sendGridMail->addCc($email, $name);
}
foreach($message->getBcc() as $email => $name) {
$sendGridMail->addBcc($email, $name);
}
$sendGridMail->setSubject($message->getSubject());
foreach($message->getHeaders() as $header) {
list($key, $value) = each($header);
$sendGridMail->addHeader($key, $value);
}
foreach($message->getAttachments() as $attachment) {
$cid = isset($attachment['ContentID']) ? $attachment['ContentID'] : null;
$sendGridMail->addAttachment($attachment['File'], $attachment['Name'], $cid);
}
$templateId = $message->getTemplateId();
if ($templateId === null) {
$data = $message->getHtmlBody();
if ($data !== null) {
// $sendGridMail->setHtml($data);
$sendGridMail->addContent('text/html',$data);
}
$data = $message->getTextBody();
if ($data !== null) {
// $sendGridMail->setText($data);
$sendGridMail->addContent('text/plain',$data);
}
} else {
$sendGridMail->setTemplateId($templateId);
// trigger html template
$sendGridMail->addContent('text/html');
// trigger text template
$sendGridMail->addContent('text/plain');
$templateModel = $message->getTemplateModel();
if (empty($templateModel) === false) {
$sendGridMail->setSubstitutions($message->getTemplateModel());
}
}
$result = $client->send($sendGridMail);
/* @var \SendGrid\Response $result */
return $result->statusCode() == 202;
} catch (Exception $e) {
throw $e;
}
}
}