Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: msgimport -h imap-host -u user-name -m mailbox -f message-file\n";
  9. print "--host IMAP host\n";
  10. print "--user IMAP user name\n";
  11. print "--mbox Target mailbox\n";
  12. print "--file Message file to upload\n";
  13. }
  14. // get arguments
  15. $opts = array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file');
  16. $args = rcube_utils::get_opt($opts) + array('host' => 'localhost', 'mbox' => 'INBOX');
  17. if ($_SERVER['argv'][1] == 'help')
  18. {
  19. print_usage();
  20. exit;
  21. }
  22. else if (!($args['host'] && $args['file']))
  23. {
  24. print "Missing required parameters.\n";
  25. print_usage();
  26. exit;
  27. }
  28. else if (!is_file($args['file']))
  29. {
  30. rcube::raise_error("Cannot read message file.", false, true);
  31. }
  32. // prompt for username if not set
  33. if (empty($args['user']))
  34. {
  35. //fwrite(STDOUT, "Please enter your name\n");
  36. echo "IMAP user: ";
  37. $args['user'] = trim(fgets(STDIN));
  38. }
  39. // prompt for password
  40. if (empty($args['pass']))
  41. {
  42. $args['pass'] = rcube_utils::prompt_silent("Password: ");
  43. }
  44. // parse $host URL
  45. $a_host = parse_url($args['host']);
  46. if ($a_host['host'])
  47. {
  48. $host = $a_host['host'];
  49. $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
  50. $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
  51. }
  52. else
  53. {
  54. $host = $args['host'];
  55. $imap_port = 143;
  56. }
  57. // instantiate IMAP class
  58. $IMAP = new rcube_imap(null);
  59. // try to connect to IMAP server
  60. if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
  61. {
  62. print "IMAP login successful.\n";
  63. print "Uploading messages...\n";
  64. $count = 0;
  65. $message = $lastline = '';
  66. $fp = fopen($args['file'], 'r');
  67. while (($line = fgets($fp)) !== false)
  68. {
  69. if (preg_match('/^From\s+-/', $line) && $lastline == '')
  70. {
  71. if (!empty($message))
  72. {
  73. if ($IMAP->save_message($args['mbox'], rtrim($message)))
  74. $count++;
  75. else
  76. rcube::raise_error("Failed to save message to {$args['mbox']}", false, true);
  77. $message = '';
  78. }
  79. continue;
  80. }
  81. $message .= $line;
  82. $lastline = rtrim($line);
  83. }
  84. if (!empty($message) && $IMAP->save_message($args['mbox'], rtrim($message)))
  85. $count++;
  86. // upload message from file
  87. if ($count)
  88. print "$count messages successfully added to {$args['mbox']}.\n";
  89. else
  90. print "Adding messages failed!\n";
  91. }
  92. else
  93. {
  94. rcube::raise_error("IMAP login failed.", false, true);
  95. }
  96. ?>