-
Notifications
You must be signed in to change notification settings - Fork 13
/
MailgunYii.php
192 lines (176 loc) · 7.2 KB
/
MailgunYii.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/** @author Andrei Baibaratsky */
/**
* Class MailgunYii
*
* @method string sendMessage(MailgunMessageYii $message)
* @method MailgunList[] getMailingLists(int $limit = 100, int $skip = 0)
* @method MailgunList getMailingList(string $listAddress)
* @method MailgunList createMailingList(MailgunList $mailingList)
* @method MailgunList updateMailingList(string $listAddress, MailgunList $mailingList)
* @method bool deleteMailingList(string $listAddress)
* @method MailgunListMember[] getMailingListMembers(string $listAddress, int $limit = 100, int $skip = 0)
* @method MailgunListMember getMailingListMember(string $listAddress, string $memberAddress)
* @method MailgunListMember addMemberToMailingList(string $listAddress, MailgunListMember $member, bool $upsert = false)
* @method MailgunList addMultipleMembersToMailingList(string $listAddress, array $members)
* @method MailgunListMember updateMailingListMember(string $listAddress, string $memberAddress, MailgunListMember $member)
* @method bool deleteMailingListMember(string $listAddress, string $memberAddress)
* @method array getMailingListStats(string $listAddress)
* @method MailgunUnsubscribe[] getUnsubscribes(int $limit = 100, int $skip = 0)
* @method MailgunUnsubscribe[] getUserUnsubscribes(string $userAddress)
* @method bool createUnsubscribe(MailgunUnsubscribe $unsubscribe)
* @method bool deleteUnsubscribe(string $id)
* @method bool deleteUserUnsubscribes(string $userAddress)
* @method MailgunComplaint[] getComplaints(int $limit = 100, int $skip = 0)
* @method MailgunComplaint getComplaint(string $userAddress)
* @method bool createComplaint(MailgunComplaint $complaint)
* @method bool deleteComplaint(string $userAddress)
* @method MailgunBounce[] getBounces(int $limit = 100, int $skip = 0)
* @method MailgunBounce getBounce(string $userAddress)
* @method bool createBounce(MailgunBounce $bounce)
* @method bool deleteBounce(string $userAddress)
* @method MailgunRoute[] getRoutes(int $limit = 100, int $skip = 0)
* @method MailgunRoute getRoute(string $id)
* @method MailgunRoute createRoute(MailgunRoute $route)
* @method MailgunRoute updateRoute(string $id, MailgunRoute $route)
* @method bool deleteRoute(string $id)
* @method bool validateHook(array $data)
* @method void setFrom(string $address, string $name = null)
* @method string getFrom()
* @method void addTag(string $tag)
* @method string[] getTags()
* @method void setCampaignId(string $campaignId)
* @method string getCampaignId()
* @method void enableDkim()
* @method void disableDkim()
* @method bool getIsDkimEnabled()
* @method void enableTestMode()
* @method void disableTestMode()
* @method bool getIsTestModeEnabled()
* @method void enableTracking()
* @method void disableTracking()
* @method bool getIsTrackingEnabled()
* @method void enableClicksTracking(bool $htmlOnly = false)
* @method void disableClicksTracking()
* @method void setClicksTrackingMode(int $clicksTrackingMode)
* @method int getClicksTrackingMode()
* @method void enableOpensTracking()
* @method void disableOpensTracking()
* @method bool getIsOpensTrackingEnabled()
*/
class MailgunYii extends CApplicationComponent
{
public $domain;
public $key;
public $viewPath = 'application.views.mail';
public $fromAddress;
public $fromName;
public $tags = array();
public $campaignId;
public $enableDkim;
public $enableTestMode;
public $enableTracking;
public $clicksTrackingMode;
public $enableOpensTracking;
public $enableDryRun;
protected $_api;
public function __call($name, $parameters)
{
if (method_exists($this->_api, $name)) {
return call_user_func_array(array($this->_api, $name), $parameters);
}
return parent::__call($name, $parameters);
}
public function init()
{
$this->_api = new MailgunApi($this->domain, $this->key);
if (!empty($this->fromAddress)) {
$this->_api->setFrom($this->fromAddress, $this->fromName);
}
foreach ($this->tags as $tag) {
$this->_api->addTag($tag);
}
if (!empty($this->campaignId)) {
$this->_api->setCampaignId($this->campaignId);
}
if (isset($this->enableDkim)) {
$this->enableDkim ? $this->_api->enableDkim() : $this->_api->disableDkim();
}
if (isset($this->enableTestMode)) {
$this->enableTestMode ? $this->_api->enableTestMode() : $this->_api->disableTestMode();
}
if (isset($this->enableTracking)) {
$this->enableTracking ? $this->_api->enableTracking() : $this->_api->disableTracking();
}
if (isset($this->clicksTrackingMode)) {
$this->_api->setClicksTrackingMode($this->clicksTrackingMode);
}
if (isset($this->enableOpensTracking)) {
$this->enableOpensTracking ? $this->_api->enableOpensTracking() : $this->_api->disableOpensTracking();
}
if (isset($this->enableDryRun)) {
$this->enableDryRun ? $this->_api->enableDryRun() : $this->_api->disableDryRun();
}
}
/**
* @return MailgunMessageYii
*/
public function newMessage()
{
$message = new MailgunMessageYii($this->_api);
$message->setViewPath($this->viewPath);
return $message;
}
}
class MailgunMessageYii extends MailgunMessage
{
private $_viewPath;
/**
* @param string $viewPath
*/
public function setViewPath($viewPath)
{
$this->_viewPath = $viewPath;
}
/**
* @return string
*/
public function getViewPath()
{
return $this->_viewPath;
}
/**
* Set the message text from view
* @param $view
* @param array $params
*/
public function renderText($view, $params = array())
{
$this->setText($this->_render($view, $params));
}
/**
* Set the message HTML from view
* @param $view
* @param array $params
*/
public function renderHtml($view, $params = array())
{
$this->setHtml($this->_render($view, $params));
}
/**
* Render message view
* @param $view
* @param array $params
* @return string
*/
protected function _render($view, $params = array())
{
if (isset(Yii::app()->controller)) {
$controller = Yii::app()->controller;
} else {
$controller = new CController(get_class());
}
$viewPath = Yii::app()->findLocalizedFile(Yii::getPathOfAlias($this->_viewPath . '.' . $view) . '.php');
return $controller->renderInternal($viewPath, $params, true);
}
}