1

As part of our onboarding process, we send user creds to the hiring managers. I have a DRAFT template saved in Outlook and run a PS script that uses the Outlook template and replaces keywords with credential information (username, password, etc.). It’s been working great for years, now, MARCOM wants to spruce it up and wants us to add inline images to the new hire emails. So, I added the images to the template but when I run my PS script now, the images are not there in the newly created emails. There is simply a red “X” and Outlook reports that the image cannot be displayed.

Some of my key requirements are:

  • From what I can tell, I can’t send the email using SMTP (I must use Outlook), because delivery of the newly created emails must be delayed (via DeferredDeliveryTime). If I could send with SMTP, I could probably use something like but I can’t use SMTP. I tried attaching the images to the DRAFT email in Outlook and using the cid: method to call the attached file names but I couldn’t get that to work.
  • We include phonetic spellings of the passwords (unique to each email), so a mail merge won’t work either
  • The images all have hyperlinks (to various sites/services)

I’ve tried a lot of approaches trying to solve this, including saving the DRAFT email as HTML and using Get-Content -Raw to read in the HTML code, etc. and that didn’t work

I tried using Mozilla Thunderbird (as referenced here, powershell email with html picture showing red x) to get the Base64 of the image but that didn’t work either.

The general basics of my PS code (as it relates to the interactions with Outlook):

If ($arrNewHireCSVobjects.Count -ge 1)
    {
    [string]$strTemplateSubjectToUse = 'Account Information - xyz'
    $oDraftsFolderIndex = 16
    $objOutlook = New-Object -comObject Outlook.Application
    $oNameSpace = $objOutlook.GetNameSpace("MAPI")
    $oDraftsFolder = $oNameSpace.GetDefaultFolder($oDraftsFolderIndex)
    $oTemplateToUse = $oDraftsFolder.Items | Where-Object {$_.Subject -eq $strTemplateSubjectToUse}
        $arrNewHireCSVobjects | ForEach-Object {
        $strTemp = $_."Manager ID"
        $ManagerUPN = ($arrManagerInfo | Where-Object {$_.employeeID -eq $strTemp}).userPrincipalName
        $NewOutlookEmail = $objOutlook.CreateItem(0)
        $NewOutlookEmail.SentonBehalfofName = "IT.Service.Desk@domain.com"
        $NewOutlookEmail.Recipients.Add($_."Current email address") | Out-Null
##
        [string]$strPhoneticPassword = ""
        For ($i = 0; $i -le $_."Temp password".Length; $i++) {
        [string]$decimal = [byte][char]($_."Temp password"[$i])
        If ($i -eq 0)
            {
            $strPhoneticPassword = $PhoneticTable[$decimal]
            }
        ElseIf ($i -eq $_."Temp password".Length)
            {
            $strPhoneticPassword = $strPhoneticPassword
            }
        Else
            {
            $strPhoneticPassword = $strPhoneticPassword + " / " + $PhoneticTable[$decimal]
            }
        }
        $strPhoneticPassword = ($strPhoneticPassword.Trim()).Replace('  ',' ')
##
        $NewOutlookEmail.Subject = $strTemplateSubjectToUse -replace 'xyz', ($_."First".Trim() + " " + $_."Last".Trim())
        $NewOutlookEmail.HTMLBody = $NewOutlookEmail.HTMLBody -replace "TEMPnewhirename", ($_."First".Trim() + " " + $_."Last".Trim())
        $NewOutlookEmail.HTMLBody = $NewOutlookEmail.HTMLBody -replace "TEMPusername", $_.Username
        $NewOutlookEmail.HTMLBody = $NewOutlookEmail.HTMLBody -replace "TEMPemailaddress", $_."Email Address"
        $NewOutlookEmail.HTMLBody = $NewOutlookEmail.HTMLBody -replace "TEMPtemppassword", $_."Temp password"
        $NewOutlookEmail.HTMLBody = $NewOutlookEmail.HTMLBody -replace "TEMPphoneticpassword", $strPhoneticPassword
        $NewOutlookEmail.DeferredDeliveryTime = (Date).AddHours(4)
        $NewOutlookEmail.Send()
        }
    }

The employee information is imported in from a CSV, the images in the template are standard, inline images (except for the links). The phonetic password is a separate function. They want like a dozen inline images in this template and they are there in the DRAFT email, but none of them show in the final email that is sent (but the hyperlinks still work with the images).

Given my requirements of having to use the Outlook client versus using something like Send-MailMessage, can PS work to send inline images that are part of an existing DRAFT email in Outlook (2013 or 2016) successfully (and have the DRAFT template modified to replace specific variables before sending), which includes a signature example/template? If so, what am I missing in my PS code that will allow the inline images to display properly?

Thanks in advance!!

UCG

SOSidb
  • 564
  • 1
  • 13
  • 25

1 Answers1

0

I've developed a solution that works for me. First, this is the line of POSH code (from above) that is causing my problem:

$NewOutlookEmail.HTMLBody = $oTemplateToUse.HTMLBody

This line copies the content of the "template" email but not the images in that email. To make this work, I took a different approach and created a complete copy of the original "template" email and stored it in another folder. Once copied, I can edit/send the copied email and the images/links remain intact because I'm not copying message content but the entire email itself.

To do this, first I created a folder (under DRAFTS) named Temp.

Next, I replaced this line of script code (from my question above):

$NewOutlookEmail = $objOutlook.CreateItem(0)

With the following:

$oSubFolder = $oDraftsFolder.Folders | ? {$_.FolderPath.EndsWith('Temp')}
$oDraftsFolder.Items | ForEach-Object {If ($_.Subject -Like $strTemplateSubjectToUse){$_.Copy().Move($oSubFolder)}} | Out-Null
$NewOutlookEmail = $oSubFolder.Items(1)

And I removed the following line:

$NewOutlookEmail.HTMLBody = $oTemplateToUse.HTMLBody

After creating the Temp folder under DRAFTS, this will copy the "template" email to a new mail message in the Temp folder, which will then be referenced as Items(1), then edited and then sent. The next email will repeat the process (so it will always be referenced with an index of 1 as long as it is the only email in the Temp folder).

Hope this helps someone in the future!!

SOSidb
  • 564
  • 1
  • 13
  • 25