1
|
|
|
<?php |
2
|
|
|
namespace Sichikawa\LaravelSendgridDriver; |
3
|
|
|
|
4
|
|
|
use Illuminate\Mail\Mailable; |
5
|
|
|
use Sichikawa\LaravelSendgridDriver\Api\Asm; |
6
|
|
|
use Sichikawa\LaravelSendgridDriver\Api\Attachment; |
7
|
|
|
use Sichikawa\LaravelSendgridDriver\Api\MailSettings; |
8
|
|
|
use Sichikawa\LaravelSendgridDriver\Api\Personalize; |
9
|
|
|
use Sichikawa\LaravelSendgridDriver\Api\TrackingSettings; |
10
|
|
|
use Swift_Message; |
11
|
|
|
|
12
|
|
|
trait SendGrid |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $sg_params = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function getSgParams() |
23
|
|
|
{ |
24
|
|
|
return $this->sg_params; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $sg_params |
29
|
|
|
*/ |
30
|
|
|
public function setSgParams($sg_params) |
31
|
|
|
{ |
32
|
|
|
$this->sg_params = $sg_params; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param null|array $params |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function sendgrid($params = null) |
40
|
|
|
{ |
41
|
|
|
$this->sg_params = $params ?: $this->sg_params; |
42
|
|
|
if ($this instanceof Mailable) { |
43
|
|
|
$this->withSwiftMessage(function (Swift_Message $message) { |
44
|
|
|
$message->embed(\Swift_Image::newInstance($this->sg_params, 'sendgrid/x-smtpapi')); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $personalizations |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setPersonalizations($personalizations) |
55
|
|
|
{ |
56
|
|
|
$this->sg_params['personalizations'] = $personalizations; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Personalize $personalize |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function addPersonalizations(Personalize $personalize) |
65
|
|
|
{ |
66
|
|
|
$this->sg_params['personalizations'][] = $personalize->toArray(); |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $attachments |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function setAttachments($attachments) |
75
|
|
|
{ |
76
|
|
|
$this->sg_params['attachments'] = $attachments; |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Attachment $attachment |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function addAttachments(Attachment $attachment) |
85
|
|
|
{ |
86
|
|
|
$this->sg_params['attachments'][] = $attachment->toArray(); |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $id |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setTemplateId($id) |
95
|
|
|
{ |
96
|
|
|
$this->sg_params['template_id'] = $id; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $section |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setSection($section) |
105
|
|
|
{ |
106
|
|
|
$this->sg_params['section'] = $section; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $headers |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setHeaders($headers) |
115
|
|
|
{ |
116
|
|
|
$this->sg_params['headers'] = $headers; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $categories |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setCategories($categories) |
125
|
|
|
{ |
126
|
|
|
$this->sg_params['categories'] = $categories; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $category |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function addCategories($category) |
135
|
|
|
{ |
136
|
|
|
$this->sg_params['categories'][] = $category; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $custom_args |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setCustomArgs($custom_args) |
145
|
|
|
{ |
146
|
|
|
$this->sg_params['custom_args'] = $custom_args; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $key |
152
|
|
|
* @param string $val |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function addCustomArgs($key, $val) |
156
|
|
|
{ |
157
|
|
|
$this->sg_params['custom_args'][$key] = $val; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param int $send_at |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function setSendAt($send_at) |
166
|
|
|
{ |
167
|
|
|
$this->sg_params['send_at'] = $send_at; |
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $batch_id |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setBatchId($batch_id) |
176
|
|
|
{ |
177
|
|
|
$this->sg_params['batch_id'] = $batch_id; |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Asm $asm |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function setAsm(Asm $asm) |
186
|
|
|
{ |
187
|
|
|
$this->sg_params['asm'] = $asm->toArray(); |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $ip_pool_name |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function setIpPoolName($ip_pool_name) |
196
|
|
|
{ |
197
|
|
|
$this->sg_params['ip_pool_name'] = $ip_pool_name; |
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param MailSettings $mailSetting |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function setMailSettings(MailSettings $mailSetting) |
207
|
|
|
{ |
208
|
|
|
$this->sg_params['mail_settings'] = $mailSetting->toArray(); |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param TrackingSettings $trackingSettings |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function setTrackingSettings(TrackingSettings $trackingSettings) |
217
|
|
|
{ |
218
|
|
|
$this->sg_params['tracking_settings'] = $trackingSettings->toArray(); |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
} |