Skip to content

Commit 7568f0e

Browse files
committed
Revert "Merge pull request #14 from chocoholics/oburatongoi-attachment"
This reverts commit 4a0549e, reversing changes made to 9b007ad.
1 parent 4a0549e commit 7568f0e

File tree

1 file changed

+43
-169
lines changed

1 file changed

+43
-169
lines changed

src/ElasticTransport.php

Lines changed: 43 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
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;
118

129
class ElasticTransport extends Transport
1310
{
1411

15-
/**
12+
/**
1613
* Guzzle client instance.
1714
*
1815
* @var \GuzzleHttp\ClientInterface
@@ -46,17 +43,14 @@ class ElasticTransport extends Transport
4643
* @param \GuzzleHttp\ClientInterface $client
4744
* @param string $key
4845
* @param string $username
49-
*
46+
*
5047
* @return void
5148
*/
52-
public function __construct(ClientInterface $client, $key, $account, $model, $rate, $transactional)
49+
public function __construct(ClientInterface $client, $key, $account)
5350
{
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;
6054
}
6155

6256
/**
@@ -74,22 +68,19 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
7468
'msgBcc' => $this->getEmailAddresses($message, 'getBcc'),
7569
'msgFrom' => $this->getFromAddress($message)['email'],
7670
'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),
8074
'subject' => $message->getSubject(),
8175
'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)
8577
];
8678

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

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

9586
/**
@@ -102,155 +93,38 @@ protected function getText(Swift_Mime_SimpleMessage $message)
10293
{
10394
$text = null;
10495

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

115104
return $text;
116105
}
117106

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

0 commit comments

Comments
 (0)