Skip to content

Instantly share code, notes, and snippets.

@hartez hartez/bodytext.ps1
Last active Feb 9, 2017

Embed
What would you like to do?
PowerShell script to pull email body text out of GMVault
$emails = @()
$gmvaultdb = "[path to your gmvault data]"
$total = (Get-ChildItem $gmvaultdb -Recurse -Filter *.eml | measure).Count
Add-Type -Path "MimeKit.1.2.10.0\lib\net45\MimeKit.dll"
$formats = @{
[MimeKit.Text.TextFormat]::Text = 0;
[MimeKit.Text.TextFormat]::Flowed = 0;
[MimeKit.Text.TextFormat]::Html = 0;
[MimeKit.Text.TextFormat]::Enriched = 0;
[MimeKit.Text.TextFormat]::RichText = 0;
[MimeKit.Text.TextFormat]::CompressedRichText = 0
}
$unknownFormat = 0
Get-ChildItem $gmvaultdb -Recurse -Filter *.eml | ForEach-Object {$i=0} {
Write-Host "Processing $_ ($i of $total)"
$mimeMessage = [MimeKit.MimeMessage]::Load($_.FullName)
$mimeMessage.From.ToString() -match '"\s<(.*)>$' | Out-Null;
$fromEmail = $Matches[1]
$bodyText = $null
$actualFormat = $null
# Run through all the enumeration values
# The pipe through sort ensures that we check them in the enum order,
# which is great because we prefer text over flowed over HTML, etc.
$formats.Keys | sort | % {
# try each Format until we find one that works
if($actualFormat -eq $null) {
# Try to get the body in the current format
$bodyText = $mimeMessage.GetTextBody($_)
if($bodyText) {
$actualFormat = $_
}
}
}
if($actualFormat -eq $null) {
$unknownFormat += 1;
$actualFormat = "Unknown"
} else {
$formats[$actualFormat] += 1;
}
$props = @{
Id = $mimeMessage.MessageId
To = $mimeMessage.To.ToString()
From = $mimeMessage.From.ToString()
FromEmail = $fromEmail
Subject = $mimeMessage.Subject
Body = $bodyText
Format = $actualFormat
}
$emails += (New-Object PSObject -Property $props)
$i++
}
$emails | Select Id, To, From, FromEmail, Subject, Body, Format | Export-Csv bodytext.csv -NoTypeInformation
$formats.Keys | sort | % { Write-Host "There were"($formats[$_])"emails in $_ format" }
Write-Host "There were $unknownFormat emails with an indeterminate format"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.