You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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