Re: Moving a large amount of mail to a different account

Reindl Harald <[email protected]> Fri, 14 Jul 2017 13:33:49 +0200
Newsgroups gmane.mail.imap.dbmail
Organization the lounge interactive design
Message-ID <[email protected]>

Am 14.07.2017 um 13:17 schrieb Thomas Raschbacher:
> I am considering moving a large amount of older emails to a seperate
> account (for Archiving reasons).
> Is there an easy way (maybe in the DB directly) to do this instead of
> using an imap client (or maybe writing something simple in python )
> 
> Also I'd want to maybe merge some folders (probably ignoring duplicates
> for the moment
> 
> -- the reason I want to do this is because that account is hardly
> useable anymore with (especially) webmail clients, because it has > 3500
> IMAP folders

look at the table-structure and the attachment, these are two methods of 
our dbmail-cms-plugin, so yes it is possible, not sure how helpful the 
moethods isolated are without the other classes since i desinged the 
whole stuff in 2009 and never really had to touch the backend code since 
then

_______________________________________________
DBmail mailing list
[email protected]
http://lists.nfg.nl/mailman/listinfo/dbmail
sample.php (application/x-php, 3.8 KB)
<?php
 /**
  * Nachrichten zwischen Ordnern verschieben
  *
  * @param  int $source_mailbox Quell-Mailbox-ID
  * @param  int $target_mailbox Ziel-Mailbox-ID
  * @param  int $set_unread     Nachrichten dabei als ungelesen markieren (0/1)
  * @return bool
  * @access public
 */
 public function move_mails(int $source_mailbox, int $target_mailbox, int $set_unread=0): bool
 {
  /** Sicherstellen dass beide Ordner existieren */
  if(!$this->exists($source_mailbox) || !$this->exists($target_mailbox))
  {
   return false;
  }
  /** Sicherstellen dass Quelle und Ziel unterschiedlich sind */
  if($source_mailbox == $target_mailbox)
  {
   return false;
  }
  /** Beide Ordner sind vorhanden und unterschiedlich -> Datenbankupdate */
  else
  {
   /** Infos zur Protokollierung abfragen */
   $source_str = $this->get_owner_name($source_mailbox) . '::' . $this->get_name($source_mailbox);
   $target_str = $this->get_owner_name($target_mailbox) . '::' . $this->get_name($target_mailbox);
   /** Wenn aktiviert vor dem Zusammenfueheren Nachrichten als ungelesen markieren */
   if($set_unread)
   {
    $this->db->query("update `dbmail_messages` set seen_flag=0 where mailbox_idnr=$source_mailbox and seen_flag=1", 1, 1);
   }
   /** Aenderung durchfuehren */
   $this->db->query("update `dbmail_messages` set mailbox_idnr=$target_mailbox where mailbox_idnr=$source_mailbox", 1, 1);
   /** Protokollieren und Erfolg zurueckgeben */
   $this->interface_ref->log("Mails moved from '$source_str' to '$target_str'");
   return true;
  }
 }

 /**
  * Komplette Mailbox zu anderem Benutzer verschieben
  *
  * @param  int    $mailbox_idnr Mailbox-Nummer
  * @param  string $target_user  Benutzername der die Mailbox erhalten soll
  * @param  int    $set_unread   Nachrichten dabei als ungelesen markieren (0/1)
  * @return bool
  * @access public
 */
 public function move_to_user(int $mailbox_idnr, string $target_user, int $set_unread=0): bool
 {
  /** Ziel-User-ID ermitteln */
  $target_user_idnr = $this->interface_ref->user->get_id_by_username($target_user);
  if($target_user_idnr < 1)
  {
   return false;
  }
  else
  {
   /** Mailbox-Infos abfragen */
   $mailbox_infos = $this->get_infos($mailbox_idnr);
   /** Abbrechen wenn Quell- und Zieluser gleich sind */
   if($mailbox_infos['owner'] == $target_user_idnr)
   {
    return false;
   }
   if(empty($mailbox_infos))
   {
    return false;
   }
   /** Liste der Ordner des Zielusers abfragen um Kollisionen zu verhindern */
   else
   {
    $mailbox_name = $mailbox_infos['name'];
    $mailboxes = [];
    foreach($this->liste($target_user) as $zsp_mailbox)
    {
     $mailboxes[] = $zsp_mailbox['name'];
    }
    if(in_array($mailbox_name, $mailboxes))
    {
     /** Versuch aktuelles Datum an Namen anzuhaengen um Kollision abzufangen */
     $mailbox_name .= '_' . strftime('%Y_%m_%d', $_SERVER['REQUEST_TIME']);
     if(in_array($mailbox_name, $mailboxes))
     {
      return false;
     }
     /** Mailbox vor dem Verschieben umbenennen */
     else
     {
      if(!$this->rename($mailbox_idnr, $mailbox_name))
      {
       return false;
      }
     }
    }
    /** Nachdem Kollisionen ausgeschlossen wurden Eigentuemer aendern */
    $userdata = $this->interface_ref->user->get_by_id($mailbox_infos['owner']);
    $this->db->query("update `dbmail_mailboxes` set owner_idnr=$target_user_idnr where mailbox_idnr=$mailbox_idnr", 1, 1);
    /** Wenn aktiviert "seen_flag" auf 0 setzen */
    if($set_unread)
    {
     $this->db->query("update `dbmail_messages` set seen_flag=0 where mailbox_idnr=$mailbox_idnr and seen_flag=1", 1, 1);
    }
    /** Systemlog */
    $this->interface_ref->log("Folder '{$mailbox_infos['name']}' of user '{$userdata['username']}' moved to '$target_user'");
    return true;
   }
  }
  /** Im Zweifelsfall Fehlschlag zurueckgeben */
  return false;
 }
?>