web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Save Email As Draft - ...
Power Automate
Answered

Save Email As Draft - Power Automate Desktop

(0) ShareShare
ReportReport
Posted on by 229 Super User 2024 Season 1

Hi ,

 

I am trying to save email as a draft. I know there is no direct activity to do this. I am using Run PowerShell Script action to do this. I am using below script. 

 

 

# Create an Outlook application object
$outlook = New-Object -ComObject Outlook.Application
# Create a new email item
$email = $outlook.CreateItem(0) # 0 represents an olMailItem, which is an email item
# Set email properties (Subject, Recipients, Body)
$email.Subject = "Updated Schedule."
$email.To = """ %ToEmail% """
$email.HTMLBody = %FinalHTML% 
# Display the email as a draft
$email.Save()
# Release the COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($email) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
Remove-Variable email, outlook -ErrorAction SilentlyContinue

 

I am using below html code in finalHTML variable used in above script. 

 

"<!DOCTYPE html> 
<html> 
<head> 
 <style> 
 /*Internal CSS using element name*/ 
 body{background-color:lavender; 
 text-align: center;} 
 h2{font-style: italic; 
 font-size: 30px; 
 color: #f08080;} 
 p{font-size: 20px;} 
 /*Internal CSS using class name*/ 
 .blue{color: blue;} 
 .red{color: red;} 
 .green{color: green;} 
 </style> 
 </head> 
 <body> 
 <h2>Learning HTML with internal CSS</h2> 
 <p class="blue">This is a blue color paragraph</p> 
 <p class="red">This is a red color paragraph</p> 
 <p class="green">This is a green color paragraph</p> 
 </body> 
</html>"

 

 Error I am receiving. 

 

 

At C:\Users\bysanil\AppData\Local\Temp\Robin\4czlod5enu3.tmp.ps1:30 char:15
+ <p class="blue">This is a blue color paragraph</p>
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'blue">This is a blue color paragraph</p> 
 <p class="red">This is a red color paragraph</p> 
 <p class="green">This is a green color paragraph</p> 
 </body> 
</html>"' in expression or statement.
 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : UnexpectedToken

 

 

Can you please let me know what's the issue here. Is there any other way that I can accomplish this.

 

Thank you in advance.

  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    You need to escape the double quotes in your HTML with a single quote, so it is interpreted as a literal part of a string instead of closing the string.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • kinuasa Profile Picture
    801 Most Valuable Professional on at

    Hi,
    I think it would be easier to use VBScript for scripted Outlook operations.
    I also suggest using the WordEditor object for text decoration.
    You can write code like a Word macro.

     

    Regards,
    kinuasa

     

    The code below is a sample:

    Dim appOl: Set appOl = Nothing 'Outlook.Application
    Set appOl = CreateObject("Outlook.Application")
    If Not appOl Is Nothing Then
     Dim itm 'Outlook.MailItem
     Dim ins 'Outlook.Inspector
     Const olMailItem = 0
     Const olSave = 0
     Set itm = appOl.CreateItem(olMailItem)
     With itm
     .Subject = "Updated Schedule."
     .To = "hogehoge@***.com"
     .Display
     Set ins = .GetInspector
     End With
     
     Dim bodyText
     bodyText = "Learning HTML with internal CSS" & vbNewLine
     bodyText = bodyText & "This is a blue color paragraph" & vbNewLine
     bodyText = bodyText & "This is a red color paragraph" & vbNewLine
     bodyText = bodyText & "This is a green color paragraph"
     With ins.WordEditor
     .Range.InsertBefore bodyText
     With .Sentences(1)
     .Font.Size = 14
     .Font.Bold = True
     End With
     .Sentences(2).Font.Color = RGB(0, 0, 255)
     .Sentences(3).Font.Color = RGB(255, 0, 0)
     .Sentences(4).Font.Color = RGB(0, 255, 0)
     End With
     itm.Close olSave
    End If
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Pretty sure they need the HTML body as a variable. And there isn't really much difference in either using VBscript or PowerShell. Both can do the thing perfectly fine, so it's more of a matter of preference on which one to use - i.e. it's best to use the one that you're personally more familiar with.

     

  • kinuasa Profile Picture
    801 Most Valuable Professional on at

    I think you can say that is no need to release references to COM objects explicitly is an advantage of VBA / VBS.
    That aside, If you are using PowerShell, you could use Here-Strings as follows.

     

     

    $email.HTMLBody = @"
    %FinalHTML%
    "@ 

     

  • LokeshBysani Profile Picture
    229 Super User 2024 Season 1 on at

    Thank you @Agnius for the inputs. Yes I had to escape double quotes and it worked. 

    Thank  you @kinuasa  for your inputs. 

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    If it works now, please mark this topic as solved.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
11manish Profile Picture

11manish 227 Super User 2026 Season 2

#2
David_MA Profile Picture

David_MA 213 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 151

Last 30 days Overall leaderboard