Guest User

Untitled

a guest
Dec 11th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Script variables
  6.  
  7. */
  8.  
  9. $target_connectionstring = "{mail.service.com:993/imap/ssl/novalidate-cert}";
  10. $target_username = "user1";
  11. $target_password = "password1";
  12. $target_folder = 'migration';
  13.  
  14. $source_connectionstring = "{mail.server.com:143/imap/notls}INBOX";
  15. $source_username = "user2";
  16. $source_password = "password2";
  17.  
  18. $migratedfolders = array();
  19.  
  20. /*
  21.  
  22. Functions
  23.  
  24. */
  25.  
  26. /* Write log to the filesystem */
  27. function errorlog($message){
  28.  
  29. $data = "[" . date("F j, Y, g:i a") . "]\t" . $message . "\n\n";
  30. file_put_contents('/tmp/imap_migration_log', $data, FILE_APPEND);
  31.  
  32. }
  33.  
  34. /* Keep track on processed folders */
  35. function migratedfolders($folder){
  36.  
  37. file_put_contents('/tmp/imap_migration_history', $folder . "\n", FILE_APPEND);
  38.  
  39. }
  40.  
  41.  
  42. /*
  43.  
  44. Start the job
  45.  
  46. */
  47.  
  48. /* Get the complete list of all source folders */
  49. $source_imap = imap_open($source_connectionstring, $source_username, $source_password, "", 10)
  50. or die("can't connect: " . imap_last_error());
  51.  
  52. $folders = imap_list($source_imap, $source_connectionstring, "*");
  53.  
  54. imap_close($source_imap);
  55.  
  56. if ($folders !== false) {
  57.  
  58. foreach ($folders as $value) {
  59.  
  60. /* Remove connection info in the path */
  61. $folderpath = str_replace($source_connectionstring, "", $value);
  62.  
  63. /* Check if the folder already are completely migrated */
  64. if(is_file('/tmp/imap_migration_history')){
  65. $migratedfolders = explode("\n", file_get_contents('/tmp/imap_migration_history'));
  66. }
  67.  
  68. if(!in_array($folderpath, $migratedfolders)){
  69.  
  70. /* Output info */
  71. print "\n\n\nWorking on $folderpath/\n";
  72.  
  73. /* Remove the folder name and keep the path */
  74. $targetpath = explode("/", $folderpath);
  75. array_pop($targetpath);
  76. $targetroot = mb_convert_encoding(implode("/", $targetpath), "UTF7-IMAP","ISO-8859-1");
  77.  
  78. /* Connect to the server */
  79. $target_imap = imap_open($target_connectionstring . $target_folder . $targetroot, $target_username, $target_password, "", 10)
  80. or die("can't connect: " . imap_last_error());
  81.  
  82. /* Create folder */
  83. if(!imap_createmailbox($target_imap, mb_convert_encoding($target_connectionstring . $target_folder . $folderpath, "UTF7-IMAP","ISO-8859-1") . '/')){
  84.  
  85. errorlog("Error creating the targetfolder $folderpath");
  86.  
  87. }
  88.  
  89. /* Disconnect */
  90. imap_close($target_imap);
  91.  
  92. /* Connect to the servers */
  93. $target_imap = imap_open(mb_convert_encoding($target_connectionstring . $target_folder . $folderpath, "UTF7-IMAP","ISO-8859-1"), $target_username, $target_password, "", 10)
  94. or die("can't connect: " . imap_last_error());
  95.  
  96. $source_imap = imap_open($source_connectionstring . $folderpath, $source_username, $source_password, "", 10)
  97. or die("can't connect: " . imap_last_error());
  98.  
  99. /* Get information about the source mailbox */
  100. $MC = imap_check($source_imap);
  101.  
  102. print "$MC->Nmsgs mails to migrate:";
  103.  
  104. /* Fetch messages from source mailbox */
  105. $result = imap_fetch_overview($source_imap,"1:{$MC->Nmsgs}",0);
  106.  
  107. foreach ($result as $overview) {
  108. $message = imap_fetchheader($source_imap, $overview->msgno) . imap_body($source_imap, $overview->msgno);
  109.  
  110. /* Write message to target mailbox */
  111. if(!imap_append($target_imap,mb_convert_encoding($target_connectionstring . $target_folder . $folderpath, "UTF7-IMAP","ISO-8859-1"),$message,"\\Seen")){
  112.  
  113. /* Log errors */
  114. errorlog("Coundn't migrate the email \"$overview->subject\" from $overview->date in the $folderpath");
  115.  
  116. /* Output info */
  117. print "|";
  118. }
  119. else{
  120. /* Output info */
  121. print ".";
  122. }
  123.  
  124. }
  125.  
  126. /* Fetch messages from source mailbox */
  127. imap_close($source_imap);
  128. imap_close($target_imap);
  129.  
  130. /* Write to the history that we are finished with the folder */
  131. migratedfolders($folderpath);
  132. }
  133. }
  134.  
  135. /* Delete history file */
  136. unlink('/tmp/imap_migration_history');
  137.  
  138. }
  139. else{
  140. print "Couldn't get source IMAP trees!";
  141. }
  142.  
  143. ?>
Add Comment
Please, Sign In to add comment