outlook email with body and signature

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
JohnTitor
Posts: 66
Joined: Fri Nov 17, 2017 8:49 pm

outlook email with body and signature

Post by JohnTitor » Wed Sep 12, 2018 10:57 pm

  1. $user = 'John'
  2.  
  3. $ADUser = (Get-ADUser $user -Properties *)
  4.  
  5. $Manager = (get-aduser (get-aduser $user -Properties manager).manager)
  6.  
  7. $sRecipientAddr = "$($manager.userprincipalname); $($ADUser.mail)"
  8.  
  9. $sMsgSubject = "Notice of closure for Incident"
  10.  
  11. $oOutlook = New-Object -ComObject Outlook.Application
  12.  
  13. $oMapiNs = $oOutlook.GetNameSpace("MAPI")
  14.  
  15. $oMailMsg = $oOutlook.CreateItem(0)
  16.  
  17. [Void]$oMailMsg.Recipients.Add($sRecipientAddr)  
  18.  
  19. $oMailMsg.Subject = $sMsgSubject
  20.  
  21. $oMailMsg.Display()
If I do this, it will open Outlook with the to, subject and signature filled in. The problem I'm running into, is adding a Body plus the signature. I cannot find any property or method to alter to achieve this.

User avatar
jvierra
Posts: 12678
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: outlook email with body and signature

Post by jvierra » Wed Sep 12, 2018 11:32 pm

To assign the body do this:

$oMailMsg.Body = 'Hello World'

There is no method for adding a signature. To add the default signature the "Display()" must be called before the body is assigned.

Code: Select all

$oOutlook = New-Object -ComObject Outlook.Application
$oMapiNs = $oOutlook.GetNameSpace("MAPI")
$oMailMsg = $oOutlook.CreateItem(0)
$oMailMsg.Display()
 
[Void]$oMailMsg.Recipients.Add($sRecipientAddr)  
$oMailMsg.Subject = $sMsgSubject
$oMailMsg.Body = 'Hello World'

User avatar
JohnTitor
Posts: 66
Joined: Fri Nov 17, 2017 8:49 pm

Re: outlook email with body and signature

Post by JohnTitor » Thu Sep 13, 2018 8:01 am

That is not what I'm experiencing. The signature gets automatically put on there. If I add in the .body such as you did, it wipes out the signature
NoSig.png
NoSig.png (26.83 KiB) Viewed 115 times
Sig.png
Sig.png (28.27 KiB) Viewed 115 times

User avatar
jvierra
Posts: 12678
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: outlook email with body and signature

Post by jvierra » Thu Sep 13, 2018 9:05 am

To preserve the inserted signature do the following.

$oMailMsg.Body = 'Hello World' + $oMailMsg.Body

User avatar
JohnTitor
Posts: 66
Joined: Fri Nov 17, 2017 8:49 pm

Re: outlook email with body and signature

Post by JohnTitor » Thu Sep 13, 2018 9:29 am

I thought that as well.. but it still wipes out the sig :(

User avatar
jvierra
Posts: 12678
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: outlook email with body and signature

Post by jvierra » Thu Sep 13, 2018 9:41 am

Try it like this.

Code: Select all

$recipients = 'jjones@domain.com'
$subject = 'Notice of closure for Incident'
 
$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")
$mailItem = $ol.CreateItem(0)
$insp = $mailItem.GetInspector()

[Void]$mailItem.Recipients.Add($recipients)  
$mailItem.Subject = $subject
#$mailItem.HTMLBody = ('<body><p>Hello World 3</body></p>' + $mailItem.HTMLBody)
$mailItem.Body = ('Hello World 3' + $mailItem.Body)

$mailItem.Send()
[/b]

User avatar
JohnTitor
Posts: 66
Joined: Fri Nov 17, 2017 8:49 pm

Re: outlook email with body and signature

Post by JohnTitor » Thu Sep 13, 2018 10:00 am

Thank you! I add to add HTMLBody, but I got it
  1. $recipients = 'jjones@domain.com'
  2.  
  3. $subject = 'Notice of closure for Incident'
  4.  
  5. $ol = New-Object -ComObject Outlook.Application
  6.  
  7. $ns = $ol.GetNameSpace("MAPI")
  8.  
  9. $mailItem = $ol.CreateItem(0)
  10.  
  11. $insp = $mailItem.GetInspector()
  12.  
  13. [Void]$mailItem.Recipients.Add($recipients)  
  14.  
  15. $mailItem.Subject = $subject
  16.  
  17. $mailItem.HTMLBody = ('Hello World' + $mailItem.HTMLBody)
  18.  
  19. $mailItem.Display()

Post Reply