Hopefully, you can now use Graph API to send emails easily using Mailozaurr, but one thing that often is more difficult than sending the email itself is creating HTML content. In the old days, you would need to play with HTML and CSS and hope it worked. Or you could install the PSWriteHTML PowerShell module and let the hard work be done for you. PSWriteHTML takes approach of never using HTML/CSS again to create HTML pages, but also emails. If you combine Mailozaurr and PSWriteHTML, you will see those two greatly simplify the process of sending nicely formatted emails. Would you believe the following email was created in 27 lines of PowerShell code?
It contains standard, advanced, lists, tables, and images, and all that is done with ZERO HTML/CSS.
$Body = EmailBody {
EmailText -Text "Hello Dear Reader," -LineBreak
EmailText -Text "I would like to introduce you to ", "PSWriteHTML", " way of writting emails. " -Color None, SafetyOrange, None -FontWeight normal, bold, normal
EmailText -Text "You can create standard text, or more advanced text by simply using provided parameters. " -Color Red
EmailText -Text @(
"Write your ", "text", " in ", "multiple ways: ", " colors", " or ", "fonts", " or ", "text transformations!"
) -Color Blue, Red, Yellow, GoldenBrown, SeaGreen, None, Green, None, SafetyOrange -FontWeight normal, bold, normal, bold, normal, normal, normal, normal, normal -LineBreak
EmailText -Text "You can create lists: "
EmailList {
EmailListItem -Text "First item"
EmailListItem -Text "Second item"
EmailListItem -Text "Third item"
EmailList {
EmailListItem -Text "Nested item 1"
EmailListItem -Text "Nested item 2"
}
} -Type Ordered -FontSize 15
EmailText -Text "You can create tables: " -LineBreak
EmailTable -DataTable (Get-Process | Select-Object -First 5 -Property Name, Id, PriorityClass, CPU, Product) -HideFooter
EmailText -LineBreak
EmailText -Text "Everything is customizable. " -Color California -FontStyle italic -TextDecoration underline
EmailText -Text "You can even add images: " -LineBreak
EmailImage -Source "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" #-Width 200 -Height 200
EmailText -Text "It's all just a command away. " -Color None -FontStyle normal -TextDecoration none
EmailText -Text "You no longer have to use HTML/CSS, as it will be used for you!"
EmailText -Text "With regards," -LineBreak
EmailText -Text "Przemysław Kłys" -TextTransform capitalize -BackGroundColor Salmon
}
$Credential = ConvertTo-GraphCredential -ClientID $ClientID -ClientSecretEncrypted $EncryptedClientSecret -DirectoryID $DirectoryID
# sending email
$sendEmailMessageSplat = @{
From = 'przemyslaw.klys@test.pl'
To = 'przemyslaw.klys@test.pl'
Credential = $Credential
HTML = $Body
Subject = 'This is another test email'
Graph = $true
Verbose = $false
Priority = 'Low'
Attachment = "C:\test.svg"
ReplyTo = "kontakt@test.pl"
RequestReadReceipt = $false
RequestDeliveryReceipt = $false
}
Send-EmailMessage @sendEmailMessageSplat
Using EmailBody, EmailText, EmailList, EmailListItem, EmailTable, and EmailImage functions, we could quickly build an email that is readable, nice looking, easy to modify, can contain variables as needed, and fully works with Microsoft Graph API and Send-EmailMessage. PSWriteHTML provides a few other experimental email features. Those are EmailLayout, EmailLayoutRow, and EmailLayoutColumn. Using those functions, you can display content next to each other quickly creating really advanced view for your emails. Take a look at example below
$Body = EmailBody {
EmailText -Text "Hello Dear Reader," -LineBreak
EmailText -Text "I would like to introduce you to ", "PSWriteHTML", " way of writting emails. " -Color None, SafetyOrange, None -FontWeight normal, bold, normal
EmailText -Text "You can create standard text, or more advanced text by simply using provided parameters. " -Color Red
EmailText -Text @(
"Write your ", "text", " in ", "multiple ways: ", " colors", " or ", "fonts", " or ", "text transformations!"
) -Color Blue, Red, Yellow, GoldenBrown, SeaGreen, None, Green, None, SafetyOrange -FontWeight normal, bold, normal, bold, normal, normal, normal, normal, normal -LineBreak
EmailText -Text "You can create lists, but also a multi-column layout with them: " -LineBreak
EmailLayout {
EmailLayoutRow {
EmailLayoutColumn {
EmailList {
EmailListItem -Text "First item"
EmailListItem -Text "Second item"
EmailListItem -Text "Third item"
EmailList {
EmailListItem -Text "Nested item 1"
EmailListItem -Text "Nested item 2"
}
} -Type Ordered -FontSize 15
}
EmailLayoutColumn {
EmailList {
EmailListItem -Text "First item - but on the right"
EmailListItem -Text "Second item - but on the right"
EmailListItem -Text "Third item"
EmailList {
EmailListItem -Text "Nested item 1"
EmailListItem -Text "Nested item 2"
}
} -Type Ordered -FontSize 10 -Color RedBerry
}
}
EmailLayoutRow {
EmailText -Text "Lets see how you can have multiple logos next to each other" -LineBreak
}
EmailLayoutRow {
EmailLayoutColumn {
EmailImage -Source "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" -Width 150
} -PaddingTop 30
EmailLayoutColumn {
EmailImage -Source "https://evotec.pl/wp-content/uploads/2015/05/Logo-evotec-012.png" -Width 150
} -PaddingTop 30
EmailLayoutColumn {
EmailImage -Source "https://upload.wikimedia.org/wikipedia/commons/9/96/Microsoft_logo_%282012%29.svg" -Width 150
} -PaddingTop 30
EmailLayoutColumn {
EmailImage -Source "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Pepsi_logo_%282014%29.svg/2560px-Pepsi_logo_%282014%29.svg.png" -Width 150
} -PaddingTop 30
}
EmailLayoutRow {
EmailText -LineBreak
EmailText -LineBreak
}
EmailLayoutRow {
EmailText -Text "You can create tables: " -LineBreak
EmailTable -DataTable (Get-Process | Select-Object -First 5 -Property Name, Id, PriorityClass, CPU, Product) -HideFooter
EmailText -LineBreak
EmailText -Text "Everything is customizable. " -Color California -FontStyle italic -TextDecoration underline
EmailText -Text "You can even add images: " -LineBreak
EmailImage -Source "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" #-Width 200 -Height 200
EmailText -Text "It's all just a command away. " -Color None -FontStyle normal -TextDecoration none
EmailText -Text "You no longer have to use HTML/CSS, as it will be used for you!"
EmailText -Text "With regards," -LineBreak
EmailText -Text "Przemysław Kłys" -TextTransform capitalize -BackGroundColor Salmon
}
}
}
Save-HTML -FilePath "test.html" -ShowHTML -HTML $Body
Notice how using EmailLayoutRow/EmailLayoutColumn, I put two lists and four images next to each other. It's still in the experimental stage but feel free to play and report feedback on Github. Using the Save-HTML function helps test HTML in a browser rather than sending emails all the time, but be careful that what you see in the browser often may look different in email clients. Always test before production rollout!