-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.messagesimapbounce.php
178 lines (159 loc) · 5.01 KB
/
class.messagesimapbounce.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
require_once 'class.connectionimapbounce.php';
/**
* ImapBounce - PHP parse bounced emails.
* PHP Version 5
* @package ImapBounce
* @link https://github.com/archieDeveloper/ImapBounce/ The ImapBounce GitHub project
* @author Arkady Kozhedub (archie) <[email protected]>
* @copyright 2015, Arkady Kozhedub
* @license GPL licensed
*/
class MessagesImapBounce
{
public $config = [
'subject' => 'Delivery Status Notification (Failure)',
'from' => 'Mail Delivery Subsystem <[email protected]>'
];
/**
* Connection mailbox
* @var object
*/
public $connection;
/**
* Headers all messages
* @var array
*/
private $_headersMessages = null;
/**
* Bounce messages
* @var array
*/
private $_bounceMessages = null;
/**
* Count messages in one task
* @var integer
*/
private $_countMessagesInTask = 500;
/**
* Count iteration for cycle
* @var integer
*/
private $_countIteration = null;
/**
* Count messages for last iteration
* @var integer
*/
private $_lastIteration = null;
/**
* SMTP RFC standard line ending.
*/
const CRLF = "\r\n";
function __construct()
{
$this->connection = new ConnectionImapBounce();
}
/**
* Get count all messages
* @access public
* @return integer|boolean
*/
public function getCount()
{
$a = $this->connection->getImapCheck();
if($a) {
return $a->Nmsgs;
}
if($this->connection->isAuth()){
return imap_num_msg($this->connection->getResource());
}
return false;
}
/**
* Get all headers messages
* @access public
* @return array|boolean
*/
public function getHeaders()
{
if ($this->_headersMessages != null) {
return $this->_headersMessages;
}
$this->_calculateTasks();
$result = [];
for($i = 0; $i <= $this->_countIteration; $i++) {
$openCursor = $i*$this->_countMessagesInTask+1;
$closeCursor = $openCursor-1;
if ($i != $this->_countIteration) {
$closeCursor += $this->_countMessagesInTask;
} else {
$closeCursor += $this->_lastIteration;
}
echo 'Task '.($i+1).': '.$openCursor.' - '.$closeCursor." [".round(($i/$this->_countIteration)*100, 2)."%]", self::CRLF;
if($this->connection->isAuth()){
$result = array_merge($result, imap_fetch_overview($this->connection->getResource(),"".($openCursor).":".$closeCursor."",0));
continue;
}
return false;
}
$this->_headersMessages = $result;
return $result;
}
/**
* Get all bounce messages
* @access public
* @return array|boolean
*/
public function getBounces()
{
if ($this->_bounceMessages != null) {
return $this->_bounceMessages;
}
$allHeaders = $this->getHeaders();
if($allHeaders) {
$bounceMessages = [];
foreach($allHeaders as $messageHeader) {
if(
(isset($messageHeader->subject))
&& (isset($messageHeader->from))
&& ($messageHeader->subject == $this->config['subject'])
&& ($messageHeader->from == $this->config['from'])
){
$bodyMessage = imap_body($this->connection->getResource(),$messageHeader->msgno);
$email = $this->_findField($bodyMessage, "Delivery to the following recipient failed permanently:","Technical details of permanent failure:");
$message = $this->_findField($bodyMessage, "Technical details of permanent failure:", "----- Original message -----");
array_push($bounceMessages, [
'email' => $email,
'message' => $message
]);
}
}
return $bounceMessages;
}
}
/**
* Calculate count iteration and count messages in the last
* @access private
* @return void
*/
private function _calculateTasks()
{
$this->_countIteration = round($this->getCount()/$this->_countMessagesInTask,0, PHP_ROUND_HALF_UP);
$this->_lastIteration = $this->getCount()-($this->_countIteration*$this->_countMessagesInTask);
}
/**
* Find the field in the message body
* @param string $message Message
* @param string $startString Start string for crop
* @param string $endString End string for crop
* @access private
* @return string
*/
private function _findField($message, $startString, $endString)
{
$beginCrop = substr($message,strpos($message, $startString)+strlen($startString));
$field = substr($beginCrop,0,strpos($beginCrop, $endString));
$trimField = trim($field);
return $trimField;
}
}