-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathemail_parser.php
More file actions
137 lines (111 loc) · 4.35 KB
/
email_parser.php
File metadata and controls
137 lines (111 loc) · 4.35 KB
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
<?php
class Email_Parser
{
private $allowed_mime_types = [];
private $disallowed_mime_types = [];
private $charset = 'UTF-8';
private $debug = false;
public $raw = null;
public $decoded = null;
public $from = null;
public $from_email = null;
public $to = null;
public $cc = null;
public $reply_to = null;
public $subject = null;
public $date = null;
public $body = "";
public $html = "";
public $attachments = [];
public function __construct($raw = null)
{
if ( !empty($raw) ) {
$this->parse($raw);
}
}
public function parse($raw = null)
{
if ( $raw !== null ) {
$this->raw = $raw;
}
if ( $this->raw !== null ) {
$parser = new PhpMimeMailParser\Parser;
$parser->setText($this->raw);
$this->from = $parser->getHeader('from');
$this->to = $parser->getHeader('to');
$this->cc = $parser->getHeader('cc');
$this->reply_to = $parser->getHeader('reply-to');
$this->subject = $parser->getHeader('subject');
$this->date = $parser->getHeader('date');
if ( $this->from ) {
$this->from_email = preg_replace('/.*<(.*)>.*/', "$1", $this->from);
}
$this->body = $parser->getMessageBody('text');
$this->html = $parser->getMessageBody('html');
$attach_dir = sys_get_temp_dir() . '/';
$attachment_paths = $parser->saveAttachments($attach_dir);
$attachments = $parser->getAttachments(true);
if ( !empty($attachments) ) {
foreach ( $attachments as $i => $attachment ) {
if ( array_key_exists($i, $attachment_paths) && $attachment_path = $attachment_paths[$i] ) {
if ( $mime_type = $attachment->getContentType() ) {
if ( $this->is_valid_attachment($mime_type) ) {
$this->attachments[] = [
'name' => $attachment->getFilename(),
'path' => $attachment_path,
'size' => $this->format_bytes(filesize($attachment_path)),
'mime' => $mime_type
];
}
}
}
}
}
}
return $this;
}
private function is_valid_attachment($mime_type)
{
if ( empty($this->allowed_mime_types) || in_array($mime_type, $this->allowed_mime_types) ) {
if ( empty($this->disallowed_mime_types) || !in_array($mime_type, $this->disallowed_mime_types) ) {
return true;
}
}
return false;
}
public function plain()
{
if ( !empty($this->body) ) {
$text = $this->body;
} elseif ( !empty($this->html) ) {
$text = $this->html;
}
if ( !empty($text) ) {
$plain = strip_tags($text, '<style>'); // remove all tags but style tags
$substr = substr($plain, strpos($plain, "<style"), strpos($plain, "</style>")); // take care of style tags manually to remove inline css
$plain = str_replace($substr, "", $plain); // remove all css
$plain = str_replace([ "\t" ], "", $plain); // remove tabs
$plain = preg_replace("/\n\n+/", "\n\n", $plain); // remove excessive line-breaks that come from stripping tags
return trim($plain); // trim extra white-space
}
}
private function format_bytes($bytes, $precision = 2)
{
$units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
private static function get_file_extension($filename)
{
if ( substr($filename, 0, 1) == '.' ) {
return substr($filename, 1);
}
$pieces = explode('.', $filename);
if ( count($pieces) > 1 ) {
return strtolower(array_pop($pieces));
}
}
}