Search code examples
powershelloutlook

Powershell, Outlook, Signature


Got this code to create a new e-mail in outlook to send from a specific account and it works perfectly.

Now, as soon as i add the "$Mail.HTMLBody = $Msg" to it, it deletes the signature and all i have is just blank text.

To me it looks like by adding the "$Msg" it replaces the whole HTLM body.

Is there a way to just add the text to the existing html body of the e-mail or a way to re-add the correct signature to the e-mail?

$Msg = "<span style='font-family:Calibri;font-size:12pt;'>Sehr geehrte Damen und Herren</span>"

$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Logon($null, $null, $false, $true)
$EmailFrom = ('[email protected]')
$account = $outlook.Session.Accounts.Item($EmailFrom)
$Mail = $Outlook.CreateItem(0)
#$Mail.HTMLBody = $Msg

Solution

  • Outlook user signatures are stored here:

    Get-ChildItem -Path "$env:APPDATA\Microsoft\Signatures"
    # Results
    <#
        Directory: C:\Users\SomeUserName\AppData\Roaming\Microsoft\Signatures
    
    
    Mode                 LastWriteTime         Length Name                                                                                                                                              
    ----                 -------------         ------ ----                                                                                                                                              
    d-----         24-May-21     21:53                SomeUserName - full_files                                                                                                                             
    d-----         24-May-21     21:53                SomeUserName - short_files                                                                                                                            
    -a----         24-May-21     21:53          43114 SomeUserName - full.htm                                                                                                                               
    -a----         24-May-21     21:53          46643 SomeUserName - full.rtf                                                                                                                               
    -a----         24-May-21     21:53           1468 SomeUserName - full.txt                                                                                                                               
    -a----         24-May-21     21:53          41008 SomeUserName - short.htm                                                                                                                              
    -a----         24-May-21     21:53          41724 SomeUserName - short.rtf                                                                                                                              
    -a----         24-May-21     21:53            120 SomeUserName - short.txt
    #>
    

    Thus you can examine each and determine which to use in your message body. Say something like this.

    $UserSignature = Get-Content -Path ((Get-ChildItem -Path "$env:APPDATA\Microsoft\Signatures" -filter '*full.htm').FullName)
    $Mail.HTMLBody = "$Msg`n`n
    $UserSignature"