Transferring mail between servers using the user interface using IMAPSync

This article will show you how to transfer mail between different servers using the IMAPSync utility through a primitive user interface.

On the destination server, you must have a mailbox with the required username and password. Before using Imapsync, be sure to install it (https://imapsync.lamiral.info/#install).

Due to the organization's prohibition to use passwords from employees' mailboxes in the script, we pass the migration process to the user. For this, a web user interface has been developed, which consists of a form module (gis.html) and an imapsync script launcher (gis.php). Filling imap servers can be automated by analyzing the contents of the mailbox name field. Using Fetchmail as a roundcube plugin is out of the question. I have not found a detailed coherent analysis of this issue.

The web interface consists of fields with information about mailboxes, an area for outputting the operation and control buttons (gis.html).

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script>
          //  sh    linux
     function isexe() {
      var ta = document.getElementById('output');
      document.getElementById('output').value += 'Start import, please wait...\n';
      var source = new EventSource('gis.php');
      source.addEventListener('message', function(e) {
       if (e.data !== '') {
        ta.value += e.data + '\n';
       }
      }, false);
      source.addEventListener('error', function(e) {
       source.close();
      }, false);
     }//isexe

    // 
    function Complete() {
      document.cookie = "mail1="+document.maildata.mail1.value;
      document.cookie = "pass1="+document.maildata.pass1.value;
      document.cookie = "mail2="+document.maildata.mail2.value;
      document.cookie = "pass2="+document.maildata.pass2.value;
      document.cookie = "msrv1="+document.maildata.msrv1.value;
      document.cookie = "msrv2="+document.maildata.msrv2.value;
      //alert(document.cookie); //   
      isexe();
      document.cookie = "mail1="+document.maildata.mail1.value+"; max-age=0";
      document.cookie = "pass1="+document.maildata.pass1.value+"; max-age=0";
      document.cookie = "mail2="+document.maildata.mail2.value+"; max-age=0";
      document.cookie = "pass2="+document.maildata.pass2.value+"; max-age=0";
      document.cookie = "msrv1="+document.maildata.msrv1.value+"; max-age=0";
      document.cookie = "msrv2="+document.maildata.msrv2.value+"; max-age=0";
    }//Complete

    function ShowCookie() {
     alert(document.cookie); //   
    }
    </script>
</head>

<body>
<H1>  </H1>
<FORM NAME="maildata">
   <TABLE>
        <TR><TD><B>  :<B></TD>
            <TD><INPUT NAME="mail1" SIZE=20 VALUE=""
        <TR><TD><B>:<B>
            <TD><INPUT TYPE="password" NAME="pass1" SIZE=20 VALUE=""
        <TR><TD><B>IMAP :<B></TD>
            <TD><INPUT NAME="msrv1" SIZE=20 VALUE=""<TD>
        <TR><TD><B>  :<B></TD>
            <TD><INPUT NAME="mail2" SIZE=20 VALUE=""
        <TR><TD><B>:<B>
            <TD><INPUT TYPE="password" NAME="pass2" SIZE=20 VALUE=""
        <TR><TD><B>IMAP :<B></TD>
            <TD><INPUT NAME="msrv2" SIZE=20 VALUE=""<TD>
    </TABLE>
    <p> :<br/><textarea id="output" style="width: 50%; height: 25em;"></textarea></p>
   <INPUT TYPE="button" VALUE="" onClick="Complete();">
    <INPUT TYPE="reset" VALUE="">
    <INPUT TYPE="button" VALUE=" cookie" onClick="ShowCookie();">
</FORM>
</body>
</html>

Imapsync script launcher (gis.php).

<?php
 ob_end_flush();
 ini_set("output_buffering", "0");
 ob_implicit_flush(true);
 header('Content-Type: text/event-stream');
 header('Cache-Control: no-cache');

 //   
function echoEvent($datatext) {
  echo "data: ".implode("\ndata: ", explode("\n", $datatext))."\n\n";
 }//echoEvent

 echoEvent("Start!");
 //    imapsync  
 $strexe = "/bin/bash /home/user/imapsync/startimapsync.sh "
           .htmlspecialchars($_COOKIE["mail1"]).' '
           .htmlspecialchars($_COOKIE["pass1"])." "
           .htmlspecialchars($_COOKIE["mail2"])." "
           .htmlspecialchars($_COOKIE["pass2"])." "
           .htmlspecialchars($_COOKIE["msrv1"])." "
           .htmlspecialchars($_COOKIE["msrv2"]);
 echoEvent($strexe);
// sh     linux
 $proc = popen($strexe,'r');
//   php,      
 while (!feof($proc)) {
  echoEvent(fread($proc, 4096));
 }
  echoEvent("Finish!");
?>

The mailbox migration script (startimapsync.sh) accepts command line parameters: logins and passwords for the imap server of the source and destination mailboxes, respectively.

#!/bin/bash
#       
cd `dirname $0`
# imapsync   
 /home/user/imapsync/./imapsync \
#   , , 
  --host1 $5:993    --user1 $1 --password1 $2 \
#   , , 
  --host2 $6:993    --user2 $3 --password2 $4 \
#      
  --ssl1  --ssl2 \
# 
  --automap \
#   
  --folderfirst INBOX \
# 
  --regextrans2 "s/&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-/Sent/" \
  --regextrans2 "s/&BBoEPgRABDcEOAQ9BDA-/Trash/" \
  --regextrans2 "s/&BCEEPwQwBDw-/Junk/" \
  --regextrans2 "s/&BCcENQRABD0EPgQyBDgEOgQ4-/Drafts/" \
#      
  --regexflag 's/\\Unseen//g' \
#        
  --useheader Message-Id




All Articles