Archiving local emails | MDaemon Technologies Community Forum

Archiving local emails


  • Hello, good day,

    I have a question. I am working on the archiving feature in SG. But how can I also archive local emails?

    Should I use Mdaemon Inbound and Outbound Mail to send everything to SG?

    Also, is there a way to archive old emails to SG?

    Thank you!

    Abel Herrera

     



  • You can use the journaling feature in MDaemon that is found by going to Setup | Server Settings | Archiving, "Send copies of all inbound and outbound mail to these addresses" and enter the Journal address, for example, journal@mydomain.com that is configured in SecurityGateway.

    To configure the Journal address in SecurityGateway go to Setup / Users | Archiving | Configuration.  SecurityGateway is expecting a local address to be used.  For example, journal@mydomain.com, you'll only enter "journal" in field.

    For this to work, you'll also need to create an account in MDaemon for journal@mydomain.com, and configure the account to forward to the IP address or host name of the SecurityGateway server.  

    If you want to use a content filter rule, the rule would look something like this:

    RuleName=Archive Internal Emails
    Enable=Yes
    ThisRuleCondition=All
    ProcessQueue=LOCAL
    Condition01=FROM|contains|AND|@mydomain.com|
    Condition02=X-MDAEMON-DELIVER-TO|contains|AND|@mydomain.com|
    Action01=copy to|"journal@mydomain.com"

    You can find more information on journal in MDaemon and SecurityGateway in the help at:

    https://help.mdaemon.com/MDaemon/en/archiving.html

    https://help.mdaemon.com/SecurityGateway/en/archiving.html

     


  • Hi Arron,

    Just, I am not sure how to forward to an IP or host name. If I enable mail forwarding for journal, it asks for an email. I cannot use only the Domain, [host], or IP field.

    Edit: I added the same journal email address in the address field and SG IP address. It seems to be working.

    Thank you!


  • So now, is there a way to archive old emails? Emails before we started using SG.

    Thank you!


  • Currently the only way to archive old messages is to use a script like the following.  This script will need to be ran from the MDaemon server and will generate a LOT of mail traffic.  It takes a copy of every MSG file that fits the parameters and puts a copy in MDaemon's remote queue that will be forwarded to SG.  Be sure to put the paths and hostnames correct.  Basically everyplace you find "mydomain.com" needs to be updated with your domain.  Also make sure you update the paths to point to the correct locations for your server.

    I'd suggest running some tests before running this for all users on your production server.  The script is setup to stop after 20,000 messages and wait for user input.  This allows MDaemon to catch up on message delivery.

    The Start Cutoff and End CutOff allow you to control the dates you want to archive.

    Let us know if you have any questions.

    $MyPath = "C:\MDaemon\Users\mydomain.com\"
    $StartCutOff = [datetime] "9/23/2025 15:59:01"
    $EndCutOff = [datetime] "10/27/2025 11:46:47"
    $RemoteQ = "C:\mdaemon\queues\remote"
    $RTEContent = "[RemoteHost]
    DeliverTo=sg.mydomain.com
    [Port]
    Port=25
    [RemoteRcpts]
    Rcpt0=Journal@mydomain.com"
    
    $MSGContentToInsert = @(
        "X-MDRedirect: 1",
        "X-MDRedirect_From: journal@mydomain.com",
        "X-Return-Path: <journal@mydomain.com>",
        "X-MDaemon-Deliver-To: <Journal@mydomain.com>"
    )
    
    #Use this to include messages from local users.  Just remove the # from in front of $Files.  Be sure to put a # in front of the $Files below
    #$Files = Get-ChildItem -Path $MyPath -File -Filter *.msg -Recurse | Where {$_.LastWriteTime -gt $StartCutOff -and $_.LastWriteTime -lt $EndCufOff -and $_.DirectoryName -notlike "*\Spam.IMAP" -and $_.DirectoryName -notlike "*\Deleted Items.IMAP" -and $_.DirectoryName -notlike "*\Drafts.IMAP" -and $_.DirectoryName -notlike "*\Calendar.IMAP" -and $_.DirectoryName -notlike "*\Contacts.IMAP" -and $_.DirectoryName -notlike "*\Tasks.IMAP" -and $_.DirectoryName -notlike "*\Notes.IMAP" -and $_.DirectoryName -notlike "*\Journal.IMAP"}
    
    #Use this to exclude messages from local users.  If you don't want to use this put a # in front of $Files on the next line.  
    $Files = Get-ChildItem -Path $MyPath -File -Filter *.msg -Recurse | Where-Object {$_.LastWriteTime -gt $StartCutOff -and $_.LastWriteTime -lt $EndCutOff -and $_.DirectoryName -notlike "*\Spam.IMAP" -and $_.DirectoryName -notlike "*\Deleted Items.IMAP" -and $_.DirectoryName -notlike "*\Drafts.IMAP" -and $_.DirectoryName -notlike "*\Calendar.IMAP" -and $_.DirectoryName -notlike "*\Contacts.IMAP" -and $_.DirectoryName -notlike "*\Tasks.IMAP" -and $_.DirectoryName -notlike "*\Notes.IMAP" -and $_.DirectoryName -notlike "*\Journal.IMAP" -and (-not (Select-String -Path $_.FullName -Pattern "FROM: .+@mydomain.com" -Quiet))}
    $Count = 0
    ForEach($File in $Files){
    #This requires user input every 20000 messages.
        if($Count -gt 20000){
            $null = Read-Host "Please press enter."
            $Count = 0
        }
        Write-Host "Processing $($File.FullName)"
        $LCKFileName = "$($File.BaseName).lck"
        $RTEFilename = "$($File.BaseName).rte"
        $NewPath = join-path $RemoteQ $File.Name
        
    
    
        Write-Host ""
        While((Test-Path "$RemoteQ\$RTEFilename") -or (Test-Path $NewPath)){
            Write-Host "." -NoNewline
            Start-Sleep -s 5
        }
    
        Write-Host ""
    
        Write-Host "LCK file created."
        $null = New-Item -Path $RemoteQ -Name $LCKFileName -ItemType file 
        Write-Host "RTE file created."
        $null = New-Item -Path $RemoteQ -Name $RTEFileName -ItemType file -Value $RTEContent
    
        Write-Host "MSG file copied to remote queue"
        Copy-Item $File.FullName -Destination $RemoteQ
    
        $MSGContent = Get-Content -Path $NewPath
        
    
        $blankLineIndex = -1
        for ($i = 0; $i -lt $MSGcontent.Count; $i++) {
            if ([string]::IsNullOrWhiteSpace($MSGcontent[$i])) {
                $blankLineIndex = $i
                break
            }
        }
    
        if ($blankLineIndex -ne -1) {
            # Get lines before the blank line
            $newContent = $MSGContent[0..($blankLineIndex - 1)]
    
            # Add the lines to insert
            $newContent += $MSGContentToInsert
    
            # Add the remaining lines from the original file (including the blank line and subsequent lines)
            $newContent += $MSGcontent[$blankLineIndex..($MSGcontent.Count - 1)]
    
            # Write the new content back to the file
            $newContent | Set-Content -Path $NewPath
            Write-Host "Lines inserted successfully before the first blank line."
    
            Write-Host "Removing the LCK file."
            Remove-Item "$Remoteq\$LCKFileName" -Force
            $Count ++
        } else {
            Write-Host "No blank line found in the file. No lines were inserted."
        }
    }
        

  • It worked. I just needed to fix $EndCuOff (missing a T) and made some changes to the line $Files.

    Thank you!


  • I'm glad it worked, thank you for letting us know about the variable issues.


Please login to reply to this topic!
Loading