5
5
use GuzzleHttp \ClientInterface ;
6
6
use Illuminate \Mail \Transport \Transport ;
7
7
use Swift_Mime_SimpleMessage ;
8
+ use Illuminate \Support \Facades \Log ;
9
+ use Illuminate \Support \Facades \Storage ;
10
+ use Exception ;
8
11
9
12
class ElasticTransport extends Transport
10
13
{
11
14
12
- /**
15
+ /**
13
16
* Guzzle client instance.
14
17
*
15
18
* @var \GuzzleHttp\ClientInterface
@@ -43,14 +46,17 @@ class ElasticTransport extends Transport
43
46
* @param \GuzzleHttp\ClientInterface $client
44
47
* @param string $key
45
48
* @param string $username
46
- *
49
+ *
47
50
* @return void
48
51
*/
49
- public function __construct (ClientInterface $ client , $ key , $ account )
52
+ public function __construct (ClientInterface $ client , $ key , $ account, $ model , $ rate , $ transactional )
50
53
{
51
- $ this ->client = $ client ;
52
- $ this ->key = $ key ;
53
- $ this ->account = $ account ;
54
+ $ this ->client = $ client ;
55
+ $ this ->key = $ key ;
56
+ $ this ->account = $ account ;
57
+ $ this ->rate = $ rate ;
58
+ $ this ->model = $ model ;
59
+ $ this ->transactional = $ transactional ;
54
60
}
55
61
56
62
/**
@@ -68,19 +74,22 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
68
74
'msgBcc ' => $ this ->getEmailAddresses ($ message , 'getBcc ' ),
69
75
'msgFrom ' => $ this ->getFromAddress ($ message )['email ' ],
70
76
'msgFromName ' => $ this ->getFromAddress ($ message )['name ' ],
71
- 'from ' => $ this ->getFromAddress ($ message )['email ' ],
72
- 'fromName ' => $ this ->getFromAddress ($ message )['name ' ],
73
- 'to ' => $ this ->getEmailAddresses ($ message ),
77
+ 'from ' => $ this ->getFromAddress ($ message )['email ' ],
78
+ 'fromName ' => $ this ->getFromAddress ($ message )['name ' ],
79
+ 'to ' => $ this ->getEmailAddresses ($ message ),
74
80
'subject ' => $ message ->getSubject (),
75
81
'body_html ' => $ message ->getBody (),
76
- 'body_text ' => $ this ->getText ($ message )
82
+ 'body_text ' => $ this ->getText ($ message ),
83
+ 'isTransactional ' => $ this ->transactional ,
84
+ 'files ' => $ this ->files ($ message ->getChildren ())
77
85
];
78
86
79
- $ result = $ this ->client ->post ($ this ->url , [
80
- 'form_params ' => $ data
81
- ]);
87
+ $ a = $ data ;
88
+ unset($ a ['body_html ' ]);
82
89
83
- return $ result ;
90
+ $ model = new $ this ->model ();
91
+ $ model ->data = json_encode ($ data );
92
+ return $ model ->save ();
84
93
}
85
94
86
95
/**
@@ -93,38 +102,155 @@ protected function getText(Swift_Mime_SimpleMessage $message)
93
102
{
94
103
$ text = null ;
95
104
96
- foreach ($ message ->getChildren () as $ child )
97
- {
98
- if ($ child ->getContentType () == 'text/plain ' )
99
- {
100
- $ text = $ child ->getBody ();
101
- }
102
- }
105
+ foreach ($ message ->getChildren () as $ child ) {
106
+ if ($ child ->getContentType () == 'text/plain ' ) {
107
+ $ text = $ child ->getBody ();
108
+ }
109
+ }
110
+
111
+ if ($ text == null ) {
112
+ $ text = strip_tags ($ message ->getBody ());
113
+ }
103
114
104
115
return $ text ;
105
116
}
106
117
107
- /**
108
- * @param \Swift_Mime_SimpleMessage $message
109
- *
110
- * @return array
111
- */
118
+ /**
119
+ * @param \Swift_Mime_SimpleMessage $message
120
+ *
121
+ * @return array
122
+ */
112
123
protected function getFromAddress (Swift_Mime_SimpleMessage $ message )
113
- {
114
- return [
115
- 'email ' => array_keys ($ message ->getFrom ())[0 ],
116
- 'name ' => array_values ($ message ->getFrom ())[0 ],
117
- ];
118
- }
119
-
120
- protected function getEmailAddresses (Swift_Mime_SimpleMessage $ message , $ method = 'getTo ' )
121
- {
122
- $ data = call_user_func ([$ message , $ method ]);
123
-
124
- if (is_array ($ data ))
125
- {
126
- return implode (', ' , array_keys ($ data ));
127
- }
128
- return '' ;
129
- }
124
+ {
125
+ return [
126
+ 'email ' => array_keys ($ message ->getFrom ())[0 ],
127
+ 'name ' => array_values ($ message ->getFrom ())[0 ],
128
+ ];
129
+ }
130
+
131
+ protected function getEmailAddresses (Swift_Mime_SimpleMessage $ message , $ method = 'getTo ' )
132
+ {
133
+ $ data = call_user_func ([$ message , $ method ]);
134
+
135
+ if (is_array ($ data )) {
136
+ return implode (', ' , array_keys ($ data ));
137
+ }
138
+ return '' ;
139
+ }
140
+
141
+ /**
142
+ * Check Swift_Attachment count
143
+ * @param $attachments
144
+ * @return bool
145
+ */
146
+ public function files ($ attachments )
147
+ {
148
+ //solo attachement
149
+ $ files = array_filter ($ attachments , function ($ e ) {
150
+ return $ e instanceof \Swift_Attachment && $ e ->getDisposition () == 'attachment ' ;
151
+ });
152
+
153
+ if (empty ($ files )) {
154
+ return null ;
155
+ }
156
+
157
+ $ data = [];
158
+ $ i = 1 ;
159
+ foreach ($ files as $ attachment ) {
160
+ $ attachedFile = $ attachment ->getBody ();
161
+ $ fileName = $ attachment ->getFilename ();
162
+ $ ext = pathinfo ($ fileName , PATHINFO_EXTENSION );
163
+ $ tempName = uniqid () . '. ' . $ ext ;
164
+ Storage::put ($ tempName , $ attachedFile );
165
+ $ type = $ attachment ->getContentType ();
166
+ $ attachedFilePath = storage_path ('app/ ' . $ tempName );
167
+ $ data [] = [
168
+ 'name ' => "file_ {$ i }" ,
169
+ 'contents ' => $ attachedFilePath ,
170
+ 'filename ' => $ fileName ,
171
+ ];
172
+ $ i ++;
173
+ }
174
+
175
+ return $ data ;
176
+ }
177
+
178
+ public function attachmentParam (array $ data )
179
+ {
180
+ $ p = array_map (function ($ i ) {
181
+ $ i ['contents ' ] = fopen ($ i ['contents ' ], 'r ' );
182
+ return $ i ;
183
+ }, $ data ['files ' ]);
184
+
185
+ unset($ data ['files ' ]);
186
+
187
+ foreach ($ data as $ key => $ value ) {
188
+ $ p [] = [
189
+ 'name ' => $ key ,
190
+ 'contents ' => $ value ,
191
+ ];
192
+ }
193
+
194
+ return [
195
+ 'multipart ' => $ p
196
+ ];
197
+ }
198
+
199
+ public function withoutAttachment (array $ data )
200
+ {
201
+ unset($ data ['files ' ]);
202
+ return [
203
+ 'form_params ' => $ data
204
+ ];
205
+ }
206
+
207
+ public function sendMail (array $ data , $ resend = true )
208
+ {
209
+ $ params = $ data ['files ' ] ?
210
+ $ this ->attachmentParam ($ data ) :
211
+ $ this ->withoutAttachment ($ data );
212
+
213
+ $ result = $ this ->client ->post ($ this ->url , $ params );
214
+ $ body = $ result ->getBody ();
215
+ $ obj = json_decode ($ body ->getContents ());
216
+ if (empty ($ obj ->success )) {
217
+ Log::warning ($ obj ->error );
218
+ //intenta reenviar sin adjunto
219
+ if ($ data ['files ' ] && $ resend ) {
220
+ $ data ['files ' ] = null ;
221
+ $ this ->sendMail ($ data , false );
222
+ }
223
+ } else {
224
+ return true ;
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Process the queue
230
+ * @return [type] [description]
231
+ */
232
+ public function sendQueue ()
233
+ {
234
+ $ model = $ this ->model ;
235
+ $ emails = $ model ::whereNull ('send_at ' )
236
+ ->orderBy ('created_at ' , 'asc ' )
237
+ ->take ($ this ->rate )
238
+ ->get ();
239
+
240
+ //delete old
241
+ $ model ::where ('send_at ' , '< ' , date ("Y-m-d H:i:s " , strtotime ("-1 day " )))->delete ();
242
+
243
+ foreach ($ emails as $ e ) {
244
+ try {
245
+ $ data = $ e ->data ;
246
+ if ($ this ->sendMail ($ data )) {
247
+ $ e ->send_at = date ("Y-m-d H:i:s " );
248
+ $ e ->save ();
249
+ };
250
+ } catch (Exception $ e ) {
251
+ Log::error ($ e );
252
+ break ;
253
+ }
254
+ }
255
+ }
130
256
}
0 commit comments