Skip to content

Commit

Permalink
+MimeMessage handling using MimeKit and -ParseMessage switch to Get-G…
Browse files Browse the repository at this point in the history
…SGmailMessageInfo
  • Loading branch information
Nate Ferrell committed Feb 11, 2017
1 parent 725cd85 commit e09b1dd
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 33 deletions.
Binary file modified PSGSuite.psd1
Binary file not shown.
28 changes: 15 additions & 13 deletions Private/Convert-EpochToDate.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
function Convert-EpochToDate {
Param
(
[parameter(Mandatory=$true)]
[string[]]
$EpochString,
[parameter(Mandatory=$false)]
[ValidateSet("Seconds","Milliseconds")]
[parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[string]
$UnitOfTime="Seconds"
$EpochString
)
$result = @()
$Method = "Add$UnitOfTime"
$UnixEpoch = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970'))
foreach ($Epoch in $EpochString)
$UnixEpoch = [timezone]::CurrentTimeZone.ToLocalTime([datetime]'1/1/1970')
try
{
$result += [pscustomobject]@{
Converted=$UnixEpoch.$Method($Epoch)
Original=$Epoch
$result = $UnixEpoch.AddSeconds($EpochString)
}
catch
{
try
{
$result = $UnixEpoch.AddMilliseconds($EpochString)
}
catch
{
$result = $UnixEpoch.AddTicks($EpochString)
}
}
return $result
Expand Down
2 changes: 1 addition & 1 deletion Private/Get-MimeType.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Get-MimeType {
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
[ValidateScript({Test-Path $_})]
[String]$File
)
Expand Down
89 changes: 89 additions & 0 deletions Private/New-MimeMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
function New-MimeMessage {
[cmdletbinding()]
Param
(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string[]]
$To,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$From,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$Subject,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$Body,
[parameter(Mandatory=$false)]
[string[]]
$CC,
[parameter(Mandatory=$false)]
[string[]]
$BCC,
[parameter(Mandatory=$false)]
[ValidateScript({Test-Path $_})]
[string[]]
$Attachment,
[parameter(Mandatory=$false)]
[switch]
$BodyAsHtml
)
#[System.Reflection.Assembly]::LoadFrom("$ModuleRoot\nuget\MimeKit.1.10.1\lib\net451\MimeKit.dll") | Out-Null
[System.Reflection.Assembly]::LoadFrom("C:\GDrive\PSModules\PSGSuite\nuget\MimeKit.1.10.1\lib\net451\MimeKit.dll") | Out-Null
$message = [MimeKit.MimeMessage]::new()
$message.From.Add($From)
$message.Subject = $Subject
foreach ($T in $To)
{
$message.To.Add($T)
}
if ($CC)
{
foreach ($C in $CC)
{
$message.Cc.Add($C)
}
}
if ($BCC)
{
foreach ($B in $BCC)
{
$message.Bcc.Add($B)
}
}
if ($BodyAsHtml)
{
$TextPart = [MimeKit.TextPart]::new("html")
}
else
{
$TextPart = [MimeKit.TextPart]::new("plain")
}
$TextPart.Text = $Body
if ($Attachment)
{
[System.Reflection.Assembly]::LoadWithPartialName('System.IO') | Out-Null
$Multipart = [MimeKit.Multipart]::new("mixed")
$Multipart.Add($TextPart)
foreach ($Attach in $Attachment)
{
$MimeType = (Get-MimeType -File $Attach) -split "/"
$MimePart = [MimeKit.MimePart]::new($MimeType[0], $MimeType[1])
$MimePart.ContentObject = [MimeKit.ContentObject]::new([IO.File]::OpenRead($Attach), [MimeKit.ContentEncoding]::Default)
$MimePart.ContentDisposition = [MimeKit.ContentDisposition]::new([MimeKit.ContentDisposition]::Attachment)
$MimePart.ContentTransferEncoding = [MimeKit.ContentEncoding]::Base64
$MimePart.FileName = [IO.Path]::GetFileName($Attach)
$Multipart.Add($MimePart)
}
$message.Body = $Multipart
}
else
{
$message.Body = $TextPart
}
return $message
}
47 changes: 47 additions & 0 deletions Private/Read-MimeMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function Read-MimeMessage {
[cmdletbinding(DefaultParameterSetName="String")]
Param
(
[parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ParameterSetName="String")]
[ValidateNotNullOrEmpty()]
[string[]]
$String,
[parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ParameterSetName="EmlFile")]
[ValidateScript({Test-Path $_})]
[string[]]
$EmlFile
)
Begin
{
[System.Reflection.Assembly]::LoadFrom("$ModuleRoot\nuget\MimeKit.1.10.1\lib\net451\MimeKit.dll") | Out-Null
$results = @()
}
Process
{
if ($PSCmdlet.ParameterSetName -eq "String")
{
$Guid = (New-Guid).Guid
$String | Set-Content "$env:TEMP\Mime$Guid.eml" -Force
$results += [MimeKit.MimeMessage]::Load("$env:TEMP\Mime$Guid.eml")
do
{
try
{
Remove-Item "$env:TEMP\Mime$Guid.eml" -Force
}
catch
{}
}
until
(!(Test-Path "$env:TEMP\Mime$Guid.eml"))
}
else
{
$results += [MimeKit.MimeMessage]::Load($EmlFile)
}
}
End
{
return $results
}
}
49 changes: 30 additions & 19 deletions Public/Get-GSGmailMessageInfo.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function Get-GSGmailMessageInfo {
[cmdletbinding()]
[cmdletbinding(DefaultParameterSetName="Format")]
Param
(
[parameter(Mandatory=$false)]
Expand All @@ -9,14 +9,18 @@ function Get-GSGmailMessageInfo {
[Alias('id')]
[String[]]
$MessageID,
[parameter(Mandatory=$false)]
[parameter(Mandatory=$false,ParameterSetName="ParseMessage")]
[switch]
$ParseMessage,
[parameter(Mandatory=$false,ParameterSetName="ParseMessage")]
[ValidateScript({Test-Path $_})]
[string]
$AttachmentOutputPath,
[parameter(Mandatory=$false,ParameterSetName="Format")]
[ValidateSet("Full","Metadata","Minimal","Raw")]
[string]
$Format="Full",
[parameter(Mandatory=$false)]
[switch]
$HighLevelView,
[parameter(Mandatory=$false)]
[String]
$AccessToken,
[parameter(Mandatory=$false)]
Expand All @@ -37,6 +41,14 @@ Begin
$header = @{
Authorization="Bearer $AccessToken"
}
if ($ParseMessage)
{
$Format = "Raw"
}
if ($AttachmentOutputPath)
{
[System.Reflection.Assembly]::LoadWithPartialName('System.IO') | Out-Null
}
$response = @()
}
Process
Expand All @@ -45,21 +57,20 @@ Process
try
{
$result = Invoke-RestMethod -Method Get -Uri $URI -Headers $header
if ($HighLevelView)
if ($ParseMessage)
{
$bodyParts = $result.payload.parts | % {ConvertFrom-Base64String -Base64String $($_.body.data) -FromWebSafeBase64}
$tempObj = New-Object psobject
$tempObj | Add-Member -MemberType NoteProperty -Name id -Value $result.id -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name threadId -Value $result.threadId -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name labelIds -Value $($result.labelIds -join ", ") -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name snippet -Value $result.snippet -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name internalDate -Value $(Convert-EpochToDate -EpochString $result.internalDate -UnitOfTime Milliseconds | Select-Object -ExpandProperty Converted) -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name sizeEstimate -Value $result.sizeEstimate -Force -ErrorAction SilentlyContinue
$result.payload.headers | ForEach-Object {$tempObj | Add-Member -MemberType NoteProperty -Name $_.name -Value $_.value -Force} -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name BodyNonHtml -Value $(($bodyParts | ? {$_ -notlike "<html>*"}) -join "`n`n") -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name BodyHtml -Value $(($bodyParts | ? {$_ -like "<html>*"}) -join "`n`n") -Force -ErrorAction SilentlyContinue
$tempObj | Add-Member -MemberType NoteProperty -Name BodyRaw -Value $($bodyParts -join "`n`n") -Force -ErrorAction SilentlyContinue
$response += $tempObj
$converted = Read-MimeMessage -String $(Convert-Base64 -From WebSafeBase64String -To NormalString -String $result.raw)
$response += $converted
if ($AttachmentOutputPath)
{
$attachments = $det.BodyParts | ? {![string]::IsNullOrEmpty($_.FileName)}
foreach ($att in $attachments)
{
$fileName = "$AttachmentOutputPath\$($att.FileName)"
$stream = [IO.File]::Create($fileName)
$att.ContentObject.DecodeTo($stream)
}
}
}
else
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file added nuget/MimeKit.1.10.1/MimeKit.1.10.1.nupkg
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added nuget/MimeKit.1.10.1/lib/net35/MimeKit.dll
Binary file not shown.
Binary file added nuget/MimeKit.1.10.1/lib/net40/MimeKit.dll
Binary file not shown.
Binary file added nuget/MimeKit.1.10.1/lib/net45/MimeKit.dll
Binary file not shown.
Binary file added nuget/MimeKit.1.10.1/lib/net451/MimeKit.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added nuget/MimeKit.1.10.1/lib/wpa81/MimeKit.dll
Binary file not shown.
Binary file not shown.

0 comments on commit e09b1dd

Please sign in to comment.