-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.br-autohelpdesk.php
156 lines (116 loc) · 3.89 KB
/
test.br-autohelpdesk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
use rdx\imap\IMAPMailbox;
require 'env.php';
require 'autoload.php';
header('Content-type: text/plain');
$mbox = new IMAPMailbox(IMAP_HELPDESK_HOST, IMAP_HELPDESK_USER, IMAP_HELPDESK_PASS, 'INBOX', ['novalidate-cert']);
print_r($mbox->headers());
$messages = $mbox->messages();
var_dump(count($messages));
// print_r($messages);
$woop = null;
foreach ($messages as $message) {
echo "\n\n\n\n";
echo $message->subject() . "\n";
echo implode("\n", $message->simpleStructure()) . "\n";
if (strpos($message->subject(), '"woop"')) {
$woop = $message;
}
}
echo "\n\n\n\n\n\n\n";
print_r($woop);
$imgs = $woop->subtypeParts(['JPEG', 'PNG', 'GIF'], true);
var_dump(count($imgs));
foreach ($imgs as $img) {
$filename = $img->safeFilename();
// echo substr($img->content(), 0, 10) . "\n\n";
// echo substr($img->decodedContent(), 0, 10) . "\n\n";
echo "$filename\n";
var_dump($img->saveAttachment(__DIR__ . '/attachments'));
}
exit;
$headers = imap_headers($mbox);
print_r($headers);
foreach ( $headers AS $hd ) {
if ( preg_match('/(\d+)\)/', $hd, $match) ) {
$msgNum = $match[1];
$hd = imap_headerinfo($mbox, $msgNum);
$new = !!trim($hd->Unseen);
if ( $new ) {
// subject -- should contain #code#
$title = get_plain_text_subject($mbox, $msgNum, $hd);
$code = preg_match('/#(\d+)#$/', $title, $match) ? (int)$match[1] : 0;
echo $title . "\n";
// body -- get only last part (no conversation history)
$attachments = array();
$full_body = get_plain_text_body($mbox, $msgNum, $attachments);
$body = get_last_body_part($full_body);
if ( $attachments ) {
$body .= "\n\n== Attachments:\n* " . implode("\n* ", $attachments);
}
#echo $full_body . "\n===================================================\n";
echo $body . "\n\n===================================================\n===================================================\n===================================================\n";
flush();
}
}
}
echo "\n";
imap_close($mbox, CL_EXPUNGE);
function get_last_body_part( $body ) {
$lines = preg_split('/(\r\n|\r|\n)/', $body);
$body = '';
foreach ( $lines AS $line ) {
if ( '>' == substr($line, 0, 1) || '-----' == substr($line, 0, 5) || '_____' == substr($line, 0, 5) ) {
break;
}
$body .= $line . "\r\n";
}
return trim($body);
}
function get_plain_text_subject( $mbox, $msgNum, $headers = null ) {
$headers or $headers = imap_headerinfo($mbox, $msgNum);
$subjectParts = imap_mime_header_decode($headers->Subject);
$subject = '';
foreach ( $subjectParts AS $p ) {
$subject .= $p->text;
}
return trim($subject);
}
function get_plain_text_body( $mbox, $msgNum, &$attachments = array() ) {
$structure = imap_fetchstructure($mbox, $msgNum);
// only plain text
if ( 'PLAIN' == $structure->subtype ) {
return trim(imap_qprint(imap_body($mbox, $msgNum)));
}
if ( isset($structure->parts) ) {
// get attachments
foreach ( $structure->parts AS $partNum => $part ) {
if ( in_array($part->subtype, array('JPEG', 'PNG', 'GIF')) ) {
// oeh an image
$name = 'image-from-email-'.$msgNum.'.' . strtolower($part->subtype);
foreach ( $part->parameters AS $param ) {
if ( 'NAME' == $param->attribute ) {
$name = $param->value;
}
}
$data = imap_fetchbody($mbox, $msgNum, (string)($partNum+1));
file_put_contents($attachments[] = 'attachments/'.time().'--'.$name, base64_decode($data));
}
}
// multipart (probably) -- look for plain text part
foreach ( $structure->parts AS $partNum => $part ) {
if ( 'PLAIN' == $part->subtype ) {
$body = imap_fetchbody($mbox, $msgNum, (string)($partNum+1));
return trim($body);
}
else if ( 'ALTERNATIVE' == $part->subtype && isset($part->parts) ) {
foreach ( $part->parts AS $subPartNum => $subPart ) {
if ( 'PLAIN' == $subPart->subtype ) {
$body = imap_fetchbody($mbox, $msgNum, ($partNum+1).'.'.($subPartNum+1));
return trim($body);
}
}
}
}
}
}