5c2732df
Mihail
add mail parser a...
|
1
2
3
4
5
6
7
8
|
<?php
/**
* Created by PhpStorm.
* User: Cibermag
* Date: 01.09.2015
* Time: 10:53
*/
|
8b0defd0
Mihail
add mails classes
|
9
|
namespace common\components\mail;
|
5c2732df
Mihail
add mail parser a...
|
10
11
|
|
8f043ab6
Mihail
add classes to wo...
|
12
13
14
15
16
17
|
/**
* Class MailAttachmentsSaver
* @package common\components\mail
* сохраняет вложения в указанную папку по полученому соединению к ящику,
* а также хранит имена сохраненных файлов
*/
|
ef87d54f
Mihail
add MailAttachmen...
|
18
19
|
class MailAttachmentsSaver
{
|
8f043ab6
Mihail
add classes to wo...
|
20
21
22
23
|
/**
* @var соединение с ящиком - экземляр класса - MailReader
*/
|
8b0defd0
Mihail
add mails classes
|
24
|
protected $mail_reader;
|
8f043ab6
Mihail
add classes to wo...
|
25
26
27
28
|
/**
* @var - string, тип сообщений - например UNSEEN. Значения можно перечислять разделяя запятой.
*/
|
8b0defd0
Mihail
add mails classes
|
29
|
protected $massage_type;
|
8f043ab6
Mihail
add classes to wo...
|
30
31
32
33
|
/**
* @var array - после сохранения будет содержать сохраненные файлы, ключ - путь к файлу, значение - расширение
*/
|
d73ee737
Mihail
finish with mail ...
|
34
|
protected $saved_files_arr = [];
|
8f043ab6
Mihail
add classes to wo...
|
35
36
37
38
|
/**
* @var - префикс который будет прибавлен к оригинальному имени сохраняемого файла
*/
|
c4da20f0
Mihail
temp commit - tes...
|
39
|
protected $file_name_prefix;
|
ef87d54f
Mihail
add MailAttachmen...
|
40
|
|
c4da20f0
Mihail
temp commit - tes...
|
41
42
43
|
public function __construct(MailReader $mail_reader)
|
ef87d54f
Mihail
add MailAttachmen...
|
44
|
{
|
8b0defd0
Mihail
add mails classes
|
45
|
$this->mail_reader = $mail_reader;
|
c4da20f0
Mihail
temp commit - tes...
|
46
|
|
8f043ab6
Mihail
add classes to wo...
|
47
|
$this->saved_files_arr = [];
|
8b0defd0
Mihail
add mails classes
|
48
|
|
ef87d54f
Mihail
add MailAttachmen...
|
49
|
}
|
5c2732df
Mihail
add mail parser a...
|
50
|
|
c4da20f0
Mihail
temp commit - tes...
|
51
52
53
54
55
56
57
|
/**
* @param mixed $file_name_prefix
*/
public function setFileNamePrefix($file_name_prefix)
{
$this->file_name_prefix = $file_name_prefix;
}
|
5c2732df
Mihail
add mail parser a...
|
58
|
|
c4da20f0
Mihail
temp commit - tes...
|
59
|
public function saveAttachmentsTo( $destination, $massage_type )
|
ef87d54f
Mihail
add MailAttachmen...
|
60
|
{
|
c4da20f0
Mihail
temp commit - tes...
|
61
|
$this->massage_type = $massage_type;
|
8f043ab6
Mihail
add classes to wo...
|
62
|
$emails = $this->mail_reader->getEmails($this->massage_type);
|
5c2732df
Mihail
add mail parser a...
|
63
64
|
/* if emails are returned, cycle through each... */
|
c4da20f0
Mihail
temp commit - tes...
|
65
|
$result = false;
|
ef87d54f
Mihail
add MailAttachmen...
|
66
|
if ($emails) {
|
5c2732df
Mihail
add mail parser a...
|
67
68
69
70
71
72
|
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
|
8f043ab6
Mihail
add classes to wo...
|
73
|
// CustomVarDamp::dump($emails);
|
5c2732df
Mihail
add mail parser a...
|
74
75
|
foreach ($emails as $email_number) {
|
8b0defd0
Mihail
add mails classes
|
76
|
$structure = $this->mail_reader->getCurrentEmailStructure($email_number);
|
5c2732df
Mihail
add mail parser a...
|
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => '');
if ($structure->parts[$i]->ifdparameters) {
foreach ($structure->parts[$i]->dparameters as $object) {
if (strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if ($structure->parts[$i]->ifparameters) {
foreach ($structure->parts[$i]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if ($attachments[$i]['is_attachment']) {
|
8f043ab6
Mihail
add classes to wo...
|
105
|
$attachments[$i]['attachment'] = $this->mail_reader->getEmailBody($email_number, $i + 1);
|
5c2732df
Mihail
add mail parser a...
|
106
107
108
109
110
111
|
if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
} elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
|
ef87d54f
Mihail
add MailAttachmen...
|
112
|
}
|
5c2732df
Mihail
add mail parser a...
|
113
|
|
ef87d54f
Mihail
add MailAttachmen...
|
114
|
if (count($attachments) != 0) {
|
5c2732df
Mihail
add mail parser a...
|
115
|
|
8f043ab6
Mihail
add classes to wo...
|
116
117
118
119
120
121
|
foreach ($attachments as $key => &$val) {
if ($val['is_attachment'] == 1) {
if (isset($this->file_name_prefix)) {
$name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']);
} else {
$name = $destination . '/' . mb_decode_mimeheader($val['name']);
|
8b0defd0
Mihail
add mails classes
|
122
|
}
|
8f043ab6
Mihail
add classes to wo...
|
123
124
125
|
$ext = pathinfo($name, PATHINFO_EXTENSION);
mb_internal_encoding("UTF-8");
file_put_contents($name, $val['attachment']);
|
d73ee737
Mihail
finish with mail ...
|
126
|
// все извлеченные файлы сохраняем в $this->saved_files_arr
|
8f043ab6
Mihail
add classes to wo...
|
127
|
$this->setSavedFile( $name , $ext );
|
c4da20f0
Mihail
temp commit - tes...
|
128
|
$result = true;
|
ef87d54f
Mihail
add MailAttachmen...
|
129
|
}
|
ef87d54f
Mihail
add MailAttachmen...
|
130
|
|
8f043ab6
Mihail
add classes to wo...
|
131
|
}
|
5c2732df
Mihail
add mail parser a...
|
132
133
134
135
|
}
}
|
5c2732df
Mihail
add mail parser a...
|
136
|
}
|
8b0defd0
Mihail
add mails classes
|
137
|
|
5c2732df
Mihail
add mail parser a...
|
138
|
}
|
8f043ab6
Mihail
add classes to wo...
|
139
|
|
c4da20f0
Mihail
temp commit - tes...
|
140
|
return $result;
|
8f043ab6
Mihail
add classes to wo...
|
141
142
143
|
}
/**
|
d73ee737
Mihail
finish with mail ...
|
144
|
* @return array - массив извлеченных файлов
|
8f043ab6
Mihail
add classes to wo...
|
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
*/
public function getSavedFilesArr()
{
return $this->saved_files_arr;
}
/**
* @param array $saved_files_arr
*/
public function setSavedFile($saved_file, $saved_file_ext)
{
$this->saved_files_arr[$saved_file] = $saved_file_ext;
}
|
5c2732df
Mihail
add mail parser a...
|
160
|
}
|