0
|
1 #!/usr/bin/env php
|
|
2 <?php
|
|
3
|
|
4 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
|
|
5 ini_set('memory_limit', -1);
|
|
6
|
|
7 require_once INSTALL_PATH.'program/include/clisetup.php';
|
|
8
|
|
9 function print_usage()
|
|
10 {
|
|
11 print "Usage: msgexport -h imap-host -u user-name -m mailbox name\n";
|
|
12 print "--host IMAP host\n";
|
|
13 print "--user IMAP user name\n";
|
|
14 print "--mbox Folder name, set to '*' for all\n";
|
|
15 print "--file Output file\n";
|
|
16 }
|
|
17
|
|
18 function vputs($str)
|
|
19 {
|
|
20 $out = $GLOBALS['args']['file'] ? STDOUT : STDERR;
|
|
21 fwrite($out, $str);
|
|
22 }
|
|
23
|
|
24 function progress_update($pos, $max)
|
|
25 {
|
|
26 $percent = round(100 * $pos / $max);
|
|
27 vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
|
|
28 }
|
|
29
|
|
30 function export_mailbox($mbox, $filename)
|
|
31 {
|
|
32 global $IMAP;
|
|
33
|
|
34 $IMAP->set_folder($mbox);
|
|
35
|
|
36 $index = $IMAP->index($mbox, null, 'ASC');
|
|
37 $count = $index->count();
|
|
38 $index = $index->get();
|
|
39
|
|
40 vputs("Getting message list of {$mbox}...");
|
|
41 vputs("$count messages\n");
|
|
42
|
|
43 if ($filename)
|
|
44 {
|
|
45 if (!($out = fopen($filename, 'w')))
|
|
46 {
|
|
47 vputs("Cannot write to output file\n");
|
|
48 return;
|
|
49 }
|
|
50 vputs("Writing to $filename\n");
|
|
51 }
|
|
52 else
|
|
53 $out = STDOUT;
|
|
54
|
|
55 for ($i = 0; $i < $count; $i++)
|
|
56 {
|
|
57 $headers = $IMAP->get_message_headers($index[$i]);
|
|
58 $from = current(rcube_mime::decode_address_list($headers->from, 1, false));
|
|
59
|
|
60 fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
|
|
61 $IMAP->get_raw_body($headers->uid, $out);
|
|
62 fwrite($out, "\n\n\n");
|
|
63
|
|
64 progress_update($i+1, $count);
|
|
65 }
|
|
66 vputs("\ncomplete.\n");
|
|
67
|
|
68 if ($filename)
|
|
69 fclose($out);
|
|
70 }
|
|
71
|
|
72
|
|
73 // get arguments
|
|
74 $opts = array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file');
|
|
75 $args = rcube_utils::get_opt($opts) + array('host' => 'localhost', 'mbox' => 'INBOX');
|
|
76
|
|
77 if ($_SERVER['argv'][1] == 'help')
|
|
78 {
|
|
79 print_usage();
|
|
80 exit;
|
|
81 }
|
|
82 else if (!$args['host'])
|
|
83 {
|
|
84 vputs("Missing required parameters.\n");
|
|
85 print_usage();
|
|
86 exit;
|
|
87 }
|
|
88
|
|
89 // prompt for username if not set
|
|
90 if (empty($args['user']))
|
|
91 {
|
|
92 vputs("IMAP user: ");
|
|
93 $args['user'] = trim(fgets(STDIN));
|
|
94 }
|
|
95
|
|
96 // prompt for password
|
|
97 $args['pass'] = rcube_utils::prompt_silent("Password: ");
|
|
98
|
|
99
|
|
100 // parse $host URL
|
|
101 $a_host = parse_url($args['host']);
|
|
102 if ($a_host['host'])
|
|
103 {
|
|
104 $host = $a_host['host'];
|
|
105 $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
|
|
106 $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
|
|
107 }
|
|
108 else
|
|
109 {
|
|
110 $host = $args['host'];
|
|
111 $imap_port = 143;
|
|
112 }
|
|
113
|
|
114 // instantiate IMAP class
|
|
115 $IMAP = new rcube_imap(null);
|
|
116
|
|
117 // try to connect to IMAP server
|
|
118 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
|
|
119 {
|
|
120 vputs("IMAP login successful.\n");
|
|
121
|
|
122 $filename = null;
|
|
123 $mailboxes = $args['mbox'] == '*' ? $IMAP->list_folders(null) : array($args['mbox']);
|
|
124
|
|
125 foreach ($mailboxes as $mbox)
|
|
126 {
|
|
127 if ($args['file'])
|
|
128 $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
|
|
129 else if ($args['mbox'] == '*')
|
|
130 $filename = asciiwords($mbox) . '.mbox';
|
|
131
|
|
132 if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
|
|
133 continue;
|
|
134
|
|
135 export_mailbox($mbox, $filename);
|
|
136 }
|
|
137 }
|
|
138 else
|
|
139 {
|
|
140 vputs("IMAP login failed.\n");
|
|
141 }
|
|
142
|
|
143 ?>
|