Create Outlook email draft using PowerShell - powershell

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("XXX#YYY.ZZZ")
$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?

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.

$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("XXX#YYY.ZZZ")
$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()

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.

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()

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!

Related

Cannot use Powershell variable to set Outlook HTMLBody property

I seem to be unable to use a Powershell variable to set the HTMLBody property of a new Outlook e-mail message. See below. The only difference is that in the second example, the desired HTML string is stored in a Powershell variable.
# this works fine.
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.HTMLBody= "<html><head></head><body><p>Hello, World</p></body></html>"
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
# this does not work. The email body remains empty
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$body = "<html><head></head><body><p>Hello, World</p></body></html>"
$mail.HTMLBody= $body
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
I am using Powershell 5 and Outlook 2019 on Windows 10. What do I have to make the $body variable take effect?
It seems this is related how PowerShell treats string variables. The HTMLBody is a string property which contains HTML markup of the message body. From the Outlook object model point of view the code looks good.
But I've noticed the following lines of code after setting the HTMLBody property:
$inspector = $mail.GetInspector
$inspector.Display()
There is no need to get an instance of the Inspector class to get the item displayed in Outlook. You could call the Display method of the MailItem class instead.

Send email with Outlook application with cutomized permissions (Using PowerShell)

I'm trying to create and send emails using the Outlook application with PowerShell applying Permissions for the email (Example: Encrypt-Only, Do Not Forward, etc.), is currently working on the outlook application (when creating the email manually), however, couldn't find a way to send emails using PowerShell applying this setting:
Permission templates in Outlook
I'm currently using the following code to create and send the emails (Working):
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "test#test.com"
$Mail.Subject = "Test Email"
$Mail.Body = "Email sent using PowerShell"
$file = "C:\ExampleFolder\test.txt"
$Mail.Attachments.Add($file)
$Mail.Send()
Is there a way to apply this setting using PowerShell?
I founded the solution for this, this solution involve two properties: "$Outlook.CreateItem("olMailItem").Permission" and "$Outlook.CreateItem("olMailItem").PermissionTemplateGuid", and work as follow:
If $Outlook.CreateItem("olMailItem").Permission is set to "0": The email is sent as unrestricted message, no PermissionTemplateGuid is needed for this value.
If $Outlook.CreateItem("olMailItem").Permission is set to "1": The email is sent as olDoNotForward automatically, no PermissionTemplateGuid is needed for this value.
If $Outlook.CreateItem("olMailItem").Permission is set to "2": The email require a PermissionTemplateGuid, therefore, it's required to get the GUID of the template, since each organization have a different template according with the configuration, send an email with the encryption template to your inbox and read it using the following code:
$olFolderInbox = 6
$outlook = new-object -com outlook.application
$mapi = $outlook.GetNameSpace("MAPI")
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$items = $inbox.items
$items[1].PermissionTemplateGuid
This is going to provide the correct permissionTemplateGuid with the desired encryption.
Therefore, to send the email the following formula works:
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "test#test.com"
$Mail.Subject = "Test Email"
$Mail.Body = "Email sent using PowerShell"
$file = "C:\ExampleFolder\test.txt"
$Mail.Attachments.Add($file)
$Mail.PermissionTemplateGuid = XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
$Mail.Permission = "2"
$Mail.Send()
For more information visit this website: http://jon.glass/blog/reads-e-mail-with-powershell/

Open draft email with signature

I am trying to open an email with Powershell as a draft, which already has a signature displayed.
Is there any way to display the body and signature in the email? There is also often pictures in the signatures so would like to bring them across too if possible.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "my_email#outlook.com"
$Mail.Subject = "Show my signature"
$body = "<body>Test body `r`n`n</body>"
$temp = $mail.body
$mail.htmlbody=get-content $env:appdata\Microsoft\Signatures\*.htm
$mail.body= $text +$mail.body + $temp
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
EDIT Solution was to add body after .Display()
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
$body = "<body>Test body <br><br></body>"
$mail.HTMLbody = $body + $mail.HTMLbody
Outlook inserts the signature when you call the Display method. If the signature is not added then you don't have any signature set for the account under which the item is going to be sent. It seems you didn't set up a correct account that has the property set up by using the SendUsingAccount property.
The signatures are being kept as separate files in the Signatures folder. You can find this folder in the following location;
Windows XP
C:\Documents and Settings\%username%\Application Data\Microsoft\Signatures
Windows Vista, Windows 7, Windows 8 and Windows 10
C:\Users\%username%\AppData\Roaming\Microsoft\Signatures
So, you can read the file content and modify the HTMLBody property.

Script to import information from csv to auto send email from outlook

Pretty new to this so bear with me.....
I am attempting to find a way to be able to import a CSV list including name, email, subject and a word (info) to the body of the email , and send it via Outlook and then go to the next line send that out, so on and so forth till all emails have been sent out. So the email may look like:
Email: $email
Subject: $subject
Body:
$name,
Hello! blah blah blah blah blah $info blah blah blah
Thank You,
end
I am looking to send out many from one list it will all be the same generic email but with certain words changed so looking to cut down on hand emailing time.
I have tried and successfully sent out one email with a script but that is it
Here is what I found to attempt to work off of
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "todd#emconsultinginc.com"
$Mail.Subject = "test"
$Mail.Body ="test"
$Mail.Send()
This is where I got to. I was able to get it to pull from a CSV with each line. Now I am wondering if we can pull the content from a word doc, keeping formatting and send it. I tried to pull the Word Doc info but I do not think I am quite there. Any suggestions?
$Mail.Send()
$Outlook = New-Object -ComObject Outlook.Application
$csv = Import-Csv C:\code\test.csv
$wd = New-Object -ComObject Word.Application
$doc = $wd.Documents.Open("C:\code\test.html")
$doc.Range().text
foreach ($line in $csv){
$Mail = $Outlook.CreateItem(0)
$Mail.To = $line.Email
$Mail.Subject = $line.Subject
$Name = $Line.Name
$body = $doc.Text
$Mail.Body ="Hello $Name, $body
$Mail.Send()
}

Powershell open email draft with Signature

I am trying to make a button open an outlook draft that we can type in the "To:" and click on send.
I got most of it working with the following code:
$SUBJECT = ('Ticket: ' + $textticketnumber.text)
CreateLink #Function that gets a weblink and stores it to variable $rtblink.Text
$BODY = $rtblink.Text
$EMAILSIG = Get-Content ($env:USERPROFILE + "\AppData\Roaming\Microsoft\Signatures\*.htm")
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.Subject = "$SUBJECT"
$mail.Body = "`n$BODY`n$EMAILSIG"
$inspector = $mail.GetInspector
$inspector.Display()
The issue is that the body of the email has the raw HTML code not the proper signature. I can change the get-content to *.txt instead of .htm but then there is no formatting in the signature, plus it puts it all on one line.
Is there a way to make the body of the code work with html or is there another way to insert the signature into the email with proper formatting?
The following code is from one of my scripts:
$sMsg = Get-Content template.html
$sRecipientAddr = "someone#example.com"
$sMsgSubject = "Subject"
$oOutlook = New-Object -ComObject Outlook.Application
$oMapiNs = $oOutlook.GetNameSpace("MAPI")
$oMailMsg = $oOutlook.CreateItem(0)
$oMailMsg.GetInspector.Activate()
$sSignature = $oMailMsg.HTMLBody
[Void]$oMailMsg.Recipients.Add($sRecipientAddr)
$oMailMsg.Subject = $sMsgSubject
$oMailMsg.HTMLBody = $sMsg + $sSignature
$oMailMsg.Save()
I use it with Outlook 2007 and it works. Outlook is configured to automatically add a signature to new messages. template.html is a pre-formatted Outlook message saved as HTML.
The idea is not mine but I honestly can't remember where I found the snippet so my apologies to the author in advance.