Skip to content

Commit 4a0549e

Browse files
author
Obura Tongoi
authored
Merge pull request #14 from chocoholics/oburatongoi-attachment
Fixes #7
2 parents 9b007ad + b6ef851 commit 4a0549e

File tree

1 file changed

+169
-43
lines changed

1 file changed

+169
-43
lines changed

src/ElasticTransport.php

Lines changed: 169 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
use GuzzleHttp\ClientInterface;
66
use Illuminate\Mail\Transport\Transport;
77
use Swift_Mime_SimpleMessage;
8+
use Illuminate\Support\Facades\Log;
9+
use Illuminate\Support\Facades\Storage;
10+
use Exception;
811

912
class ElasticTransport extends Transport
1013
{
1114

12-
/**
15+
/**
1316
* Guzzle client instance.
1417
*
1518
* @var \GuzzleHttp\ClientInterface
@@ -43,14 +46,17 @@ class ElasticTransport extends Transport
4346
* @param \GuzzleHttp\ClientInterface $client
4447
* @param string $key
4548
* @param string $username
46-
*
49+
*
4750
* @return void
4851
*/
49-
public function __construct(ClientInterface $client, $key, $account)
52+
public function __construct(ClientInterface $client, $key, $account, $model, $rate, $transactional)
5053
{
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;
5460
}
5561

5662
/**
@@ -68,19 +74,22 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
6874
'msgBcc' => $this->getEmailAddresses($message, 'getBcc'),
6975
'msgFrom' => $this->getFromAddress($message)['email'],
7076
'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),
7480
'subject' => $message->getSubject(),
7581
'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())
7785
];
7886

79-
$result = $this->client->post($this->url, [
80-
'form_params' => $data
81-
]);
87+
$a = $data;
88+
unset($a['body_html']);
8289

83-
return $result;
90+
$model = new $this->model();
91+
$model->data= json_encode($data);
92+
return $model->save();
8493
}
8594

8695
/**
@@ -93,38 +102,155 @@ protected function getText(Swift_Mime_SimpleMessage $message)
93102
{
94103
$text = null;
95104

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+
}
103114

104115
return $text;
105116
}
106117

107-
/**
108-
* @param \Swift_Mime_SimpleMessage $message
109-
*
110-
* @return array
111-
*/
118+
/**
119+
* @param \Swift_Mime_SimpleMessage $message
120+
*
121+
* @return array
122+
*/
112123
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+
}
130256
}

0 commit comments

Comments
 (0)