Skip to content

Instantly share code, notes, and snippets.

@Logicbloke
Created January 1, 2024 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Logicbloke/9fdee7752226d8c711e977efb2fa1d07 to your computer and use it in GitHub Desktop.
Save Logicbloke/9fdee7752226d8c711e977efb2fa1d07 to your computer and use it in GitHub Desktop.
PHP Import Mailbox using IMAP
<?php
$sourceServer = "{mail.mtlmaroc.com:993/imap/ssl}"; // ISP Mailserver Example (Menara.ma)
$destinationServer = "{ssl0.ovh.net:993/imap/ssl}"; // OVH Mailserver
// A list of folders that will be skipped.
// NOTE - If a parent folder is listed, all children folders are skipped too
$folders_skip = array(
"Protected",
"Calendar",
"Contacts",
"Notes",
"Journal",
"Tasks",
"Spam",
"Junk",
"Public Folders");
?>
<?php
function imap_list_source_mailboxes($server, $user, $pwd) {
global $folders_skip;
// Get list of mailboxes from src_server for $username
$mailbox = @imap_open($server, $user, $pwd);
$list = @imap_list($mailbox, $server,"*");
if (is_array($list)) {
reset($list);
} else {
$error = "source server <code>imap_list</code> failed: <code>". imap_last_error()."</code>\n";
return $error;
}
while (list($key, $val) = each($list)) {
$mailbox = @imap_utf7_decode($val);
$fullmailbox = $mailbox;
$mailbox = str_replace($server, "", $mailbox);
// Skip UNIX hidden files
if (preg_match("^\.", $mailbox)) {
continue;
}
if (!in_array(str_replace("INBOX.", "", $mailbox), $folders_skip)) {
$count = @imap_open($server . $mailbox, $user, $pwd);
$mbArray[] = array('mailbox' => $mailbox, 'messages' => @imap_num_msg($count));
@imap_close($countä);
}
}
@imap_close($sourceMailbox);
return $mbArray;
}
function imap_mailbox_exists($connection, $server, $mailbox) {
$mailboxes = @imap_list($connection, $server, $mailbox);
if (empty($mailboxes)) {
return false;
} else {
return true;
}
}
function imap_mailbox_create($connection, $server, $mailbox) {
if( @imap_createmailbox($connection, imap_utf7_encode($server . $mailbox))) {
return true;
} else {
return false;
}
}
// check destination-user check
function destinationUserCheck() {
global $destinationUser, $sourceUser;
if ($destinationUser=="") {
$destinationUser = $sourceUser;
}
return $destinationUser;
}
// check destination-pwd check
function destinationPwdCheck() {
global $destinationPwd, $sourcePwd;
if ($destinationPwd=="") {
$destinationPwd = $sourcePwd;
}
return $destinationPwd;
}
/*
* parses a given byte count
*
* thanks drupal!
*/
function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
?>
<?php
require("./config.php");
require("./functions.php");
$data = file_get_contents("data.csv");
$lines = explode("\n", $data);
foreach($lines as $line) {
if(empty($line)) break;
// Getting each user
$user = explode("~",$line);
$sourceUser = $user[1];
$sourcePwd = $user[2];
$destinationUser = $user[3];
$destinationPwd = $user[4];
echo $sourceUser." >> ".$destinationUser." >> ";
$destinationUser = destinationUserCheck();
$destinationPwd = destinationPwdCheck();
$mailbox = "Inbox";
// Open sourceServer and mailbox
$sourceMailbox = @imap_open($sourceServer, $sourceUser, $sourcePwd);
if(imap_last_error()) {
echo "SourceError: ".imap_last_error()."\n";
continue;
}
// Open destinationServer, choose no mailbox
$destinationMailbox = @imap_open($destinationServer, $destinationUser, $destinationPwd);
if(imap_last_error()) {
echo "DestinationError: ".imap_last_error()."\n";
continue;
}
// check if mailbox exists
$mailbox_exists = imap_mailbox_exists($destinationMailbox, $destinationServer, $mailbox);
$destinationMailbox = @imap_open($destinationServer . $mailbox, $destinationUser, $destinationPwd);
$sourceMailsStatus = imap_check($sourceMailbox);
for ($i=1; $i<=$sourceMailsStatus->Nmsgs; $i++) {
$soureMail = imap_fetchheader($sourceMailbox, $i) . "\r" . imap_body($sourceMailbox, $i, FT_PEEK);
imap_append($destinationMailbox, $destinationServer . $mailbox, $soureMail);
$messageInfo = imap_headerinfo($sourceMailbox, $i);
}
echo $sourceMailsStatus->Nmsgs . "\n";
imap_close($sourceMailbox);
imap_close($destinationMailbox);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment