使用IMAPSync使用用户界面在服务器之间传输邮件

本文将向您展示如何使用IMAPSync实用程序通过原始用户界面在不同服务器之间传输邮件。

在目标服务器上,您必须具有包含所需用户名和密码的邮箱。使用Imapsync之前,请确保安装它(https://imapsync.lamiral.info/#install)。

由于组织禁止在脚本中使用员工邮箱中的密码,因此我们将迁移过程传递给用户。为此,开发了一个Web用户界面,该界面由一个表单模块(gis.html)和一个imapsync脚本启动器(gis.php)组成。通过分析邮箱名称字段的内容,可以自动填充imap服务器。使用Fetchmail作为roundcube插件是不可能的。我尚未找到对此问题的详细连贯分析。

Web界面由以下字段组成:有关邮箱的信息,用于操作和控制按钮的输出区域(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脚本启动器(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!");
?>

邮箱迁移脚本(startimapsync.sh)接受命令行参数:源邮箱和目标邮箱的imap服务器的登录名和密码。

#!/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