From 48712ac13f2bf0f425c2edf992c46eb2080c8572 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 9 Dec 2022 10:33:02 +0100 Subject: [PATCH] ImapSync example script #744 --- snappymail/v/0.0.0/imapsync.php | 124 ++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100755 snappymail/v/0.0.0/imapsync.php diff --git a/snappymail/v/0.0.0/imapsync.php b/snappymail/v/0.0.0/imapsync.php new file mode 100755 index 000000000..eb0e82a5d --- /dev/null +++ b/snappymail/v/0.0.0/imapsync.php @@ -0,0 +1,124 @@ +#!/usr/bin/php + $v) { + if ($v === $arg) { + $opts[$key] = ($required || $optional) ? $_SERVER['argv'][$i+1] : true; + } + if (($required || $optional) && 0 === \strpos($v, "{$arg}=")) { + $opts[$key] = \substr($v, 1+\strlen($arg)); + } + } + if ($required && !isset($opts[$key])) { + exit("Missing required argument {$arg}"); + } + } + return $opts; +} +$options = get_opt('', [ + // Source or "from" imap server. + 'host1::', + 'port1::', + 'user1:', + 'password1:', + 'nossl1', // Do not use a SSL connection on host1. + 'ssl1', // Use a SSL connection on host1. On by default if possible. + 'notls1', // Do not use a TLS connection on host1. + 'tls1', // Use a TLS connection on host1. On by default if possible. + + // "destination" imap server. + 'host2::', + 'port2::', + 'user2:', + 'password2:', + 'nossl2', // Do not use a SSL connection on host2. + 'ssl2', // Use a SSL connection on host2. On by default if possible. + 'notls2', // Do not use a TLS connection on host2. + 'tls2', // Use a TLS connection on host2. On by default if possible. + 'rootfolder::' // Added as prefix to destination folders +]); + +chdir(__DIR__); +define('APP_VERSION', basename(__DIR__)); +$_ENV['SNAPPYMAIL_INCLUDE_AS_API'] = true; +require __DIR__ . '/include.php'; + +function getImapClient(int $host) +{ + global $options; + + $oConfig = \RainLoop\API::Config(); + + $type = SecurityType::AUTO_DETECT; + if (isset($options["tls{$host}"])) { + $type = SecurityType::STARTTLS; + } + else if (isset($options["ssl{$host}"])) { + $type = SecurityType::SSL; + } + else if (isset($options["notls{$host}"])) { + $type = SecurityType::NONE; + } + $ImapSettings = \MailSo\Imap\Settings::fromArray([ + 'host' => $options["host{$host}"] ?? 'localhost', + 'port' => $options["port{$host}"] ?? 143, + 'type' => $type, + 'ssl' => [] + ]); + if (993 === $ImapSettings->port) { + $ImapSettings->type = SecurityType::SSL; + } + if ($oConfig->Get('labs', 'sasl_allow_scram_sha', false)) { + \array_push($ImapSettings->SASLMechanisms, 'SCRAM-SHA3-512', 'SCRAM-SHA-512', 'SCRAM-SHA-256', 'SCRAM-SHA-1'); + } + if ($oConfig->Get('labs', 'sasl_allow_cram_md5', false)) { + $ImapSettings->SASLMechanisms[] = 'CRAM-MD5'; + } + if ($oConfig->Get('labs', 'sasl_allow_plain', true)) { + $ImapSettings->SASLMechanisms[] = 'PLAIN'; + } + $ImapSettings->Login = $options["user{$host}"]; + $ImapSettings->Password = $options["password{$host}"]; +// $ImapSettings1->ProxyAuthUser = ''; +// $ImapSettings1->ProxyAuthPassword = ''; + $ImapSettings->useAuth = true; + + $oImapClient = new \MailSo\Imap\ImapClient; + //$oImapTarget->SetLogger($this->Logger()); + $oImapClient->__FORCE_SELECT_ON_EXAMINE__ = !!$oConfig->Get('imap', 'use_force_selection'); + $oImapClient->__DISABLE_METADATA = !!$oConfig->Get('imap', 'disable_metadata'); + //$oPlugins->RunHook('imap.before-connect', array($this, $oImapClient, $ImapSettings)); + $oImapClient->Connect($ImapSettings); + //$oPlugins->RunHook('imap.after-connect', array($this, $oImapClient, $ImapSettings)); + //$oPlugins->RunHook('imap.before-login', array($this, $oImapClient, $ImapSettings)); + $oImapClient->Login($ImapSettings); + //$oPlugins->RunHook('imap.after-login', array($this, $oImapClient, $bResult, $ImapSettings)); + + return $oImapClient; +} + +$oSync = new \SnappyMail\Imap\Sync; +$oSync->oImapSource = getImapClient(1); +$oSync->oImapTarget = getImapClient(2); + +$oSync->import($options['rootfolder'] ?: 'test');