SCRT-HQ / PSGSuite

Powershell module for Google / G Suite API calls wrapped in handy functions. Authentication is established using a service account via P12 key to negate the consent popup and allow for greater handsoff automation capabilities
https://psgsuite.io/
Apache License 2.0
235 stars 67 forks source link

Add support for downloading inline images using the -SaveAttachmentsTo parameter #209

Closed higginsta closed 5 years ago

higginsta commented 5 years ago

Is your feature request related to a problem? Please describe.

The -SaveAttachmentsTo parameter does not work for all messages that contain inline images. Some email clients like iPhone Mail do not specify inline images as being attachments.

Describe the solution you'd like

Please add the ability for the -SaveAttachmentsTo parameter to parse files that are included inline with a message (located within BodyParts instead of Attachments)

Describe alternatives you've considered The sample code you provided as a workaround for inline images works as expected.

Additional context

You helped me work through this issue via Discord on July 10th, 2019 (using code similar to what Get-GSGmailMessage is already doing)

scrthq commented 5 years ago

Thanks, @higginsta !! Sample code for (my own) reference that was shared on Discord:

Import-Module PSGSuite
$msg = Get-GSGmailMessage -Id $id -Verbose -ParseMessage

$downloadPath = "~\Downloads"

foreach ($att in ($msg.BodyParts | Where-Object {$_.FileName})) {
    $cleanedName = $att.FileName -replace "[$([RegEx]::Escape("$(([System.IO.Path]::GetInvalidFileNameChars() + [System.IO.Path]::GetInvalidPathChars()) -join '')"))]","_" 
    $resPath = (Resolve-Path $downloadPath).Path
    $fileName = Join-Path $resPath $cleanedName
    Write-Verbose "Saving attachment to path '$fileName'"
    $stream = [System.IO.File]::Create($fileName)
    $att.ContentObject.DecodeTo($stream)
    $stream.Close()
}
scrthq commented 5 years ago

hey @higginsta - This is fixed and confirmed working as expected! The behavior now is that it will grab both BodyParts and Attachments that have a FileName listed for them, then sort by unique FileName + ContentId, then download the resulting list to the path specified in SaveAttachmentsTo. The unique sort is due to inline images also showing as attachments depending on the sending mail service, which would result in duplicate attachment downloads without the unique sort. ContentId and FileName match when the file is listed in both areas and I've tested with a couple sample messages that this works as expected.

scrthq commented 5 years ago

I'll let you know as soon as available on the Gallery!

scrthq commented 5 years ago

deployed!