21

I'm creating a PowerShell script to automate a process at work. This process requires an email to be filled in and sent to someone else. The email will always roughly follow the same sort of template however it will probably never be the same every time so I want to create an email draft in Outlook and open the email window so the extra details can be filled in before sending.

I've done a bit of searching online but all I can find is some code to send email silently. The code is as follows:

$ol = New-Object -comObject Outlook.Application  
$mail = $ol.CreateItem(0)  
$Mail.Recipients.Add("[email protected]")  
$Mail.Subject = "PS1 Script TestMail"  
$Mail.Body = "  
Test Mail  
"  
$Mail.Send() 

In short, does anyone have any idea how to create and save a new Outlook email draft and immediately open that draft for editing?

1
  • any final source code sample script?
    – Kiquenet
    Feb 11, 2013 at 8:20

5 Answers 5

26

Based on the other answers, I have trimmed down the code a bit and use

$ol = New-Object -comObject Outlook.Application

$mail = $ol.CreateItem(0)
$mail.Subject = "<subject>"
$mail.Body = "<body>"
$mail.save()

$inspector = $mail.GetInspector
$inspector.Display()

This removes the unnecessary step of retrieving the mail from the drafts folder. Incidentally, it also removes an error that occurred in Shay Levy's code when two draft emails had the same subject.

25
$olFolderDrafts = 16
$ol = New-Object -comObject Outlook.Application 
$ns = $ol.GetNameSpace("MAPI")

# call the save method yo dave the email in the drafts folder
$mail = $ol.CreateItem(0)
$null = $Mail.Recipients.Add("[email protected]")  
$Mail.Subject = "PS1 Script TestMail"  
$Mail.Body = "  Test Mail  "
$Mail.save()

# get it back from drafts and update the body
$drafts = $ns.GetDefaultFolder($olFolderDrafts)
$draft = $drafts.Items | where {$_.subject -eq 'PS1 Script TestMail'}
$draft.body += "`n foo bar"
$draft.save()

# send the message
#$draft.Send()
3
  • Great! Thanks. That's mostly done it! Is it possible to make the script open the draft window automatically so the last manual bits can be filled in before sending?
    – Jason
    Sep 21, 2009 at 20:23
  • np :) call the display method: $draft.Display()
    – Shay Levy
    Sep 21, 2009 at 22:04
  • 1
    Is there a way to add attachments to the draft?
    – Chris
    May 23, 2016 at 9:07
5

I think Shay Levy's answer is almost there: the only bit missing is the display of the item. To do this, all you need is to get the relevant inspector object and tell it to display itself, thus:

$inspector = $draft.GetInspector  
$inspector.Display()

See the MSDN help on GetInspector for fancier behaviour.

0
3

if you want to use HTML template please use HTMLbody instead of Body , please find sample code below:

$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.Subject = "Top demand apps-SOURCE CLARIFICATION"
$mail.HTMLBody="<html><head></head><body><b>Joseph</b></body></Html>"
$mail.save()

$inspector = $mail.GetInspector
$inspector.Display()
2

Thought I would add in to this as well. There are a few steps you can save yourself if you know a lot of the basics (subject, recipients, or other aspects). First create the template of the email and save that, e.g. somewhere maybe with the code?

As to the code itself, it follows much the same that others have posted.

Borrowing from Jason:

$ol = New-Object -comObject Outlook.Application
$msg = $ol.CreateItemFromTemplate(<<Path to template file>>)

Modify as needed. Append fields or modify body. The message can still be viewed prior to sending the same way $msg.GetInspector.Display(). Then call $msg.send() to send away!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.