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
|
|
ef87d54f
Mihail
add MailAttachmen...
|
12
13
|
use common\components\CustomVarDamp;
|
8f043ab6
Mihail
add classes to wo...
|
14
15
16
17
18
19
|
/**
* Class MailAttachmentsSaver
* @package common\components\mail
* сохраняет вложения в указанную папку по полученому соединению к ящику,
* а также хранит имена сохраненных файлов
*/
|
ef87d54f
Mihail
add MailAttachmen...
|
20
21
|
class MailAttachmentsSaver
{
|
8f043ab6
Mihail
add classes to wo...
|
22
23
24
25
|
/**
* @var соединение с ящиком - экземляр класса - MailReader
*/
|
8b0defd0
Mihail
add mails classes
|
26
|
protected $mail_reader;
|
8f043ab6
Mihail
add classes to wo...
|
27
28
29
30
|
/**
* @var - string, тип сообщений - например UNSEEN. Значения можно перечислять разделяя запятой.
*/
|
8b0defd0
Mihail
add mails classes
|
31
|
protected $massage_type;
|
8f043ab6
Mihail
add classes to wo...
|
32
33
34
35
36
37
38
39
40
|
/**
* @var array - после сохранения будет содержать сохраненные файлы, ключ - путь к файлу, значение - расширение
*/
protected $saved_files_arr;
/**
* @var - префикс который будет прибавлен к оригинальному имени сохраняемого файла
*/
|
c4da20f0
Mihail
temp commit - tes...
|
41
|
protected $file_name_prefix;
|
ef87d54f
Mihail
add MailAttachmen...
|
42
|
|
c4da20f0
Mihail
temp commit - tes...
|
43
44
45
|
public function __construct(MailReader $mail_reader)
|
ef87d54f
Mihail
add MailAttachmen...
|
46
|
{
|
8b0defd0
Mihail
add mails classes
|
47
|
$this->mail_reader = $mail_reader;
|
c4da20f0
Mihail
temp commit - tes...
|
48
|
|
8f043ab6
Mihail
add classes to wo...
|
49
|
$this->saved_files_arr = [];
|
8b0defd0
Mihail
add mails classes
|
50
|
|
ef87d54f
Mihail
add MailAttachmen...
|
51
|
}
|
5c2732df
Mihail
add mail parser a...
|
52
|
|
c4da20f0
Mihail
temp commit - tes...
|
53
54
55
56
57
58
59
|
/**
* @param mixed $file_name_prefix
*/
public function setFileNamePrefix($file_name_prefix)
{
$this->file_name_prefix = $file_name_prefix;
}
|
5c2732df
Mihail
add mail parser a...
|
60
|
|
c4da20f0
Mihail
temp commit - tes...
|
61
|
public function saveAttachmentsTo( $destination, $massage_type )
|
ef87d54f
Mihail
add MailAttachmen...
|
62
|
{
|
c4da20f0
Mihail
temp commit - tes...
|
63
|
$this->massage_type = $massage_type;
|
8f043ab6
Mihail
add classes to wo...
|
64
|
$emails = $this->mail_reader->getEmails($this->massage_type);
|
5c2732df
Mihail
add mail parser a...
|
65
66
|
/* if emails are returned, cycle through each... */
|
c4da20f0
Mihail
temp commit - tes...
|
67
|
$result = false;
|
ef87d54f
Mihail
add MailAttachmen...
|
68
|
if ($emails) {
|
5c2732df
Mihail
add mail parser a...
|
69
70
71
72
73
74
|
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
|
8f043ab6
Mihail
add classes to wo...
|
75
|
// CustomVarDamp::dump($emails);
|
5c2732df
Mihail
add mail parser a...
|
76
77
|
foreach ($emails as $email_number) {
|
8b0defd0
Mihail
add mails classes
|
78
|
$structure = $this->mail_reader->getCurrentEmailStructure($email_number);
|
5c2732df
Mihail
add mail parser a...
|
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
105
106
|
$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...
|
107
|
$attachments[$i]['attachment'] = $this->mail_reader->getEmailBody($email_number, $i + 1);
|
5c2732df
Mihail
add mail parser a...
|
108
109
110
111
112
113
|
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...
|
114
|
}
|
5c2732df
Mihail
add mail parser a...
|
115
|
|
ef87d54f
Mihail
add MailAttachmen...
|
116
|
if (count($attachments) != 0) {
|
5c2732df
Mihail
add mail parser a...
|
117
|
|
8f043ab6
Mihail
add classes to wo...
|
118
119
120
121
122
123
|
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
|
124
|
}
|
8f043ab6
Mihail
add classes to wo...
|
125
126
127
128
|
$ext = pathinfo($name, PATHINFO_EXTENSION);
mb_internal_encoding("UTF-8");
file_put_contents($name, $val['attachment']);
$this->setSavedFile( $name , $ext );
|
c4da20f0
Mihail
temp commit - tes...
|
129
|
$result = true;
|
ef87d54f
Mihail
add MailAttachmen...
|
130
|
}
|
ef87d54f
Mihail
add MailAttachmen...
|
131
|
|
8f043ab6
Mihail
add classes to wo...
|
132
|
}
|
5c2732df
Mihail
add mail parser a...
|
133
134
135
136
|
}
}
|
5c2732df
Mihail
add mail parser a...
|
137
|
}
|
8b0defd0
Mihail
add mails classes
|
138
|
|
5c2732df
Mihail
add mail parser a...
|
139
|
}
|
8f043ab6
Mihail
add classes to wo...
|
140
|
|
c4da20f0
Mihail
temp commit - tes...
|
141
|
return $result;
|
8f043ab6
Mihail
add classes to wo...
|
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
}
/**
* @return array
*/
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...
|
161
|
}
|