forked from decent/imap2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap2csv.php
executable file
·164 lines (124 loc) · 4.11 KB
/
imap2csv.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
#!/usr/bin/env php
<?php
// Configuration file
// To create a config file, copy "config.ini.sample" to "config.ini" and
// make your changes.
$config_file = 'config/config.ini';
function main()
{
global $config_file;
$orders = []; // An array that keeps one row per parsed mail
if(($config = @get_config($config_file)) == false)
{
die("Could not read configuration file.\n");
}
// Open connection to IMAP server
$inbox = @imap_open($config['hostname'].$config['folder'],
$config['username'], $config['password'])
or die("Couldn't connect to mail server.\n");
if(archive_folder_exists($inbox, $config['hostname'], $config['archive']) == false)
{
if((imap_createmailbox($inbox, $config['hostname'].$config['archive'])) == false)
{
imap_close($inbox);
die("Could not create archive folder");
}
}
// Get the emails that interest us
$emails = imap_search($inbox, "FROM " . $config['sender']);
// Only continue if we have any emails to process
if($emails) {
$orders = parse_mail($inbox, $emails, $config['fields'], $config['archive']);
output($orders, $config['output'], $config['logfile'], $config['delimiter']);
// Remove moved emails
imap_expunge($inbox);
} else {
echo "No new mails.\n";
}
// Close imap handle
imap_close($inbox);
}
// Encodes text to UTF-8, turns CRLF to LF and ; to :
function clean_message($text)
{
// decode
$text = imap_qprint($text);
// Mail seems to arrive as Latin1
$text = utf8_encode($text);
// http://stackoverflow.com/questions/7836632/how-to-replace-different-newline-styles-in-php-the-smartest-way
// \R matches newline characters in unicode by default.
// With --enable-bsr-anycrlf it only matches CR, LR or CRLF
$text = preg_replace('~(*BSR_ANYCRLF)\R~', "\n", $text);
// Replace ; with :
$text = str_replace(';', ':', $text);
return $text;
}
function get_fields($message, $fields)
{
$data = [];
foreach($fields as $field)
{
// /m modifier should let us use ^ and $ as start and end
// per line instead of whole string.
preg_match("/^$field: (.*)$/m", $message, $result);
$data[] = $result[1];
}
return $data;
}
function get_config($config_file)
{
// Read config file
// We don't parse section names in our config file so instead of
// $config['mail']['hostname'] we only get $config['hostname']
// Older versions return empty array on failure while PHP 5.2.7 and
// newer returns FALSE
$config = parse_ini_file($config_file, false);
if($config != false && !empty($config))
{
// Splitting the fields string into an array
$config['fields'] = explode(',', $config['fields']);
} else {
$config = false;
}
return $config;
}
function output($orders, $output, $log, $delimiter)
{
$fp_output = fopen($output, 'w');
$fp_log = fopen($log, 'a');
// Outputs array to CSV for us
foreach($orders as $order)
{
fputcsv($fp_output, $order, $delimiter);
fputcsv($fp_log, $order, $delimiter);
}
fclose($fp_output);
fclose($fp_log);
}
function parse_mail($inbox, $emails, $fields, $archive_folder)
{
$orders = [];
echo "Processing mail";
foreach($emails as $email_number) {
echo ".";
$overview = imap_fetch_overview($inbox, $email_number, 0);
$message = clean_message(imap_fetchbody($inbox, $email_number, 1));
// array_unshift adds to front of array
$order = get_fields($message, $fields);
array_unshift($order, $overview[0]->udate);
$orders[] = $order;
// Mark current mail for move
imap_mail_move($inbox, $email_number, $archive_folder);
}
echo "\n";
return $orders;
}
// Tests if the archive folder we requested exists
function archive_folder_exists($mail_handle, $hostname, $folder)
{
$folder_list = imap_list($mail_handle, $hostname, "*");
return array_search($hostname.$folder, $folder_list);
}
// Start program
main();
?>