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 ;
11
8
12
9
class ElasticTransport extends Transport
13
10
{
14
11
15
- /**
12
+ /**
16
13
* Guzzle client instance.
17
14
*
18
15
* @var \GuzzleHttp\ClientInterface
@@ -46,17 +43,14 @@ class ElasticTransport extends Transport
46
43
* @param \GuzzleHttp\ClientInterface $client
47
44
* @param string $key
48
45
* @param string $username
49
- *
46
+ *
50
47
* @return void
51
48
*/
52
- public function __construct (ClientInterface $ client , $ key , $ account, $ model , $ rate , $ transactional )
49
+ public function __construct (ClientInterface $ client , $ key , $ account )
53
50
{
54
- $ this ->client = $ client ;
55
- $ this ->key = $ key ;
56
- $ this ->account = $ account ;
57
- $ this ->rate = $ rate ;
58
- $ this ->model = $ model ;
59
- $ this ->transactional = $ transactional ;
51
+ $ this ->client = $ client ;
52
+ $ this ->key = $ key ;
53
+ $ this ->account = $ account ;
60
54
}
61
55
62
56
/**
@@ -74,22 +68,19 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
74
68
'msgBcc ' => $ this ->getEmailAddresses ($ message , 'getBcc ' ),
75
69
'msgFrom ' => $ this ->getFromAddress ($ message )['email ' ],
76
70
'msgFromName ' => $ this ->getFromAddress ($ message )['name ' ],
77
- 'from ' => $ this ->getFromAddress ($ message )['email ' ],
78
- 'fromName ' => $ this ->getFromAddress ($ message )['name ' ],
79
- 'to ' => $ this ->getEmailAddresses ($ message ),
71
+ 'from ' => $ this ->getFromAddress ($ message )['email ' ],
72
+ 'fromName ' => $ this ->getFromAddress ($ message )['name ' ],
73
+ 'to ' => $ this ->getEmailAddresses ($ message ),
80
74
'subject ' => $ message ->getSubject (),
81
75
'body_html ' => $ message ->getBody (),
82
- 'body_text ' => $ this ->getText ($ message ),
83
- 'isTransactional ' => $ this ->transactional ,
84
- 'files ' => $ this ->files ($ message ->getChildren ())
76
+ 'body_text ' => $ this ->getText ($ message )
85
77
];
86
78
87
- $ a = $ data ;
88
- unset($ a ['body_html ' ]);
79
+ $ result = $ this ->client ->post ($ this ->url , [
80
+ 'form_params ' => $ data
81
+ ]);
89
82
90
- $ model = new $ this ->model ();
91
- $ model ->data = json_encode ($ data );
92
- return $ model ->save ();
83
+ return $ result ;
93
84
}
94
85
95
86
/**
@@ -102,155 +93,38 @@ protected function getText(Swift_Mime_SimpleMessage $message)
102
93
{
103
94
$ text = null ;
104
95
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
- }
96
+ foreach ($ message ->getChildren () as $ child )
97
+ {
98
+ if ($ child ->getContentType () == 'text/plain ' )
99
+ {
100
+ $ text = $ child ->getBody ();
101
+ }
102
+ }
114
103
115
104
return $ text ;
116
105
}
117
106
118
- /**
119
- * @param \Swift_Mime_SimpleMessage $message
120
- *
121
- * @return array
122
- */
107
+ /**
108
+ * @param \Swift_Mime_SimpleMessage $message
109
+ *
110
+ * @return array
111
+ */
123
112
protected function getFromAddress (Swift_Mime_SimpleMessage $ message )
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
- }
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
+ }
256
130
}
0 commit comments