outlook email with body and signature

Ask your 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.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

outlook email with body and signature

Post by apowershelluser »

  1. $user = 'John'
  2. $ADUser = (Get-ADUser $user -Properties *)
  3. $Manager = (get-aduser (get-aduser $user -Properties manager).manager)
  4. $sRecipientAddr = "$($manager.userprincipalname); $($ADUser.mail)"
  5. $sMsgSubject = "Notice of closure for Incident"
  6. $oOutlook = New-Object -ComObject Outlook.Application
  7. $oMapiNs = $oOutlook.GetNameSpace("MAPI")
  8. $oMailMsg = $oOutlook.CreateItem(0)
  9. [Void]$oMailMsg.Recipients.Add($sRecipientAddr)  
  10. $oMailMsg.Subject = $sMsgSubject
  11. $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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: outlook email with body and signature

Post by jvierra »

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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: outlook email with body and signature

Post by apowershelluser »

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 3577 times
Sig.png
Sig.png (28.27 KiB) Viewed 3577 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: outlook email with body and signature

Post by jvierra »

To preserve the inserted signature do the following.

$oMailMsg.Body = 'Hello World' + $oMailMsg.Body
User avatar
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: outlook email with body and signature

Post by apowershelluser »

I thought that as well.. but it still wipes out the sig :(
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: outlook email with body and signature

Post by jvierra »

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
apowershelluser
Posts: 194
Last visit: Fri Mar 22, 2024 4:38 am
Answers: 2

Re: outlook email with body and signature

Post by apowershelluser »

Thank you! I add to add HTMLBody, but I got it
  1. $recipients = 'jjones@domain.com'
  2. $subject = 'Notice of closure for Incident'
  3. $ol = New-Object -ComObject Outlook.Application
  4. $ns = $ol.GetNameSpace("MAPI")
  5. $mailItem = $ol.CreateItem(0)
  6. $insp = $mailItem.GetInspector()
  7. [Void]$mailItem.Recipients.Add($recipients)  
  8. $mailItem.Subject = $subject
  9. $mailItem.HTMLBody = ('Hello World' + $mailItem.HTMLBody)
  10. $mailItem.Display()
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked