|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: execut |
|
5
|
|
|
* Date: 9/27/16 |
|
6
|
|
|
* Time: 2:26 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace execut\import\components\source\adapter\email; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use execut\import\components\source\File; |
|
13
|
|
|
use roopz\imap\Imap; |
|
14
|
|
|
use roopz\imap\IncomingMail; |
|
15
|
|
|
use yii\base\Component; |
|
16
|
|
|
|
|
17
|
|
|
class Receiver extends Component |
|
18
|
|
|
{ |
|
19
|
|
|
public $filter = null; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Imap |
|
22
|
|
|
*/ |
|
23
|
|
|
public $imap = null; |
|
24
|
|
|
public $now = null; |
|
25
|
|
|
protected $searchCriteria = null; |
|
26
|
|
|
protected $mails; |
|
27
|
|
|
|
|
28
|
|
|
public function setSearchCriteria($criteria) { |
|
29
|
|
|
$this->searchCriteria = $criteria; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function getSearchCriteria() { |
|
33
|
1 |
|
if ($this->searchCriteria === null) { |
|
34
|
1 |
|
return 'UNSEEN SINCE "' . $this->getNow() . '"'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this->searchCriteria; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
protected function getNow() { |
|
41
|
1 |
|
if ($this->now !== null) { |
|
42
|
1 |
|
return $this->now; |
|
43
|
|
|
} |
|
44
|
|
|
return date('d F Y', time() - 3600 * 24 * 10); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
protected function _getMails() { |
|
48
|
1 |
|
if ($this->mails !== null) { |
|
49
|
1 |
|
return $this->mails; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$mailsIds = $this->imap->searchMailBox($this->getSearchCriteria()); |
|
53
|
1 |
|
$mails = []; |
|
54
|
1 |
|
foreach ($mailsIds as $id) { |
|
55
|
1 |
|
$mails[] = self::createMailFromImap($this->imap->getMail($id, false)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
return $this->mails = $mails; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Mail[]; |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function getMails() { |
|
65
|
3 |
|
$mails = $this->_getMails(); |
|
66
|
3 |
|
if ($this->filter) { |
|
67
|
1 |
|
$mails = $this->filter->filtrate($mails); |
|
68
|
1 |
|
if ($this->imap !== null) { |
|
69
|
1 |
|
foreach ($mails as $mail) { |
|
70
|
1 |
|
$this->imap->markMailAsRead($mail->id); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return $mails; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
public static function createMailFromImap(IncomingMail $imapMail) { |
|
79
|
2 |
|
$attachments = []; |
|
80
|
2 |
|
foreach ($imapMail->getAttachments() as $imapAttachment) { |
|
81
|
1 |
|
$attachment = new File([ |
|
82
|
1 |
|
'fileName' => $imapAttachment->name, |
|
83
|
1 |
|
'filePath' => $imapAttachment->filePath, |
|
84
|
|
|
]); |
|
85
|
1 |
|
$attachments[] = $attachment; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return new Mail([ |
|
89
|
2 |
|
'id' => $imapMail->id, |
|
90
|
2 |
|
'sender' => $imapMail->fromAddress, |
|
91
|
2 |
|
'subject' => $imapMail->subject, |
|
92
|
2 |
|
'attachments' => $attachments, |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
} |