Tuesday, May 14, 2024
HomePowershellStraightforward option to ship emails utilizing Microsoft Graph API (Workplace 365) with...

Straightforward option to ship emails utilizing Microsoft Graph API (Workplace 365) with PowerShell


Hopefully, now you can use Graph API to ship emails simply utilizing Mailozaurr, however one factor that usually is tougher than sending the e-mail itself is creating HTML content material. Within the outdated days, you would want to play with HTML and CSS and hope it labored. Or you can set up the PSWriteHTML PowerShell module and let the exhausting work be completed for you. PSWriteHTML takes strategy of by no means utilizing HTML/CSS once more to create HTML pages, but additionally emails. In case you mix Mailozaurr and PSWriteHTML, you will notice these two significantly simplify the method of sending properly formatted emails. Would you imagine the next e mail was created in 27 traces of PowerShell code?

It comprises normal, superior, lists, tables, and pictures, and all that’s completed with ZERO HTML/CSS.

$Physique = EmailBody {
    EmailText -Textual content "Whats up Expensive Reader," -LineBreak
    EmailText -Textual content "I wish to introduce you to ", "PSWriteHTML", " means of writting emails. " -Colour None, SafetyOrange, None -FontWeight regular, daring, regular
    EmailText -Textual content "You possibly can create normal textual content, or extra superior textual content by merely utilizing offered parameters. " -Colour Purple
    EmailText -Textual content @(
        "Write your ", "textual content", " in ", "a number of methods: ", " colours", " or ", "fonts", " or ", "textual content transformations!"
    ) -Colour Blue, Purple, Yellow, GoldenBrown, SeaGreen, None, Inexperienced, None, SafetyOrange -FontWeight regular, daring, regular, daring, regular, regular, regular, regular, regular -LineBreak
    EmailText -Textual content "You possibly can create lists: "
    EmailList {
        EmailListItem -Textual content "First merchandise"
        EmailListItem -Textual content "Second merchandise"
        EmailListItem -Textual content "Third merchandise"
        EmailList {
            EmailListItem -Textual content "Nested merchandise 1"
            EmailListItem -Textual content "Nested merchandise 2"
        }
    } -Kind Ordered -FontSize 15
    EmailText -Textual content "You possibly can create tables: " -LineBreak
    EmailTable -DataTable (Get-Course of | Choose-Object -First 5 -Property Title, Id, PriorityClass, CPU, Product) -HideFooter
    EmailText -LineBreak
    EmailText -Textual content "Every thing is customizable. " -Colour California -FontStyle italic -TextDecoration underline
    EmailText -Textual content "You possibly can even add photos: " -LineBreak
    EmailImage -Supply "https://www.google.com/photos/branding/googlelogo/2x/googlelogo_color_272x92dp.png" #-Width 200 -Top 200
    EmailText -Textual content "It is all only a command away. " -Colour None -FontStyle regular -TextDecoration none
    EmailText -Textual content "You not have to make use of HTML/CSS, as it will likely be used for you!"
    EmailText -Textual content "With regards," -LineBreak
    EmailText -Textual content "Przemysław Kłys" -TextTransform capitalize -BackGroundColor Salmon
}

$Credential = ConvertTo-GraphCredential -ClientID $ClientID -ClientSecretEncrypted $EncryptedClientSecret -DirectoryID $DirectoryID

# sending e mail
$sendEmailMessageSplat = @{
    From                   = 'przemyslaw.klys@check.pl'
    To                     = 'przemyslaw.klys@check.pl'
    Credential             = $Credential
    HTML                   = $Physique
    Topic="That is one other check e mail"
    Graph                  = $true
    Verbose                = $false
    Precedence               = 'Low'
    Attachment             = "C:check.svg"
    ReplyTo                = "kontakt@check.pl"
    RequestReadReceipt     = $false
    RequestDeliveryReceipt = $false
}

Ship-EmailMessage @sendEmailMessageSplat

Utilizing EmailBody, EmailText, EmailList, EmailListItem, EmailTable, and EmailImage features, we may rapidly construct an e mail that’s readable, good wanting, straightforward to switch, can include variables as wanted, and absolutely works with Microsoft Graph API and Ship-EmailMessage. PSWriteHTML supplies just a few different experimental e mail options. These are EmailLayout, EmailLayoutRow, and EmailLayoutColumn. Utilizing these features, you may show content material subsequent to one another rapidly creating actually superior view in your emails. Check out instance beneath

$Physique = EmailBody {
    EmailText -Textual content "Whats up Expensive Reader," -LineBreak
    EmailText -Textual content "I wish to introduce you to ", "PSWriteHTML", " means of writting emails. " -Colour None, SafetyOrange, None -FontWeight regular, daring, regular
    EmailText -Textual content "You possibly can create normal textual content, or extra superior textual content by merely utilizing offered parameters. " -Colour Purple
    EmailText -Textual content @(
        "Write your ", "textual content", " in ", "a number of methods: ", " colours", " or ", "fonts", " or ", "textual content transformations!"
    ) -Colour Blue, Purple, Yellow, GoldenBrown, SeaGreen, None, Inexperienced, None, SafetyOrange -FontWeight regular, daring, regular, daring, regular, regular, regular, regular, regular -LineBreak

    EmailText -Textual content "You possibly can create lists, but additionally a multi-column format with them: " -LineBreak

    EmailLayout {
        EmailLayoutRow {
            EmailLayoutColumn {
                EmailList {
                    EmailListItem -Textual content "First merchandise"
                    EmailListItem -Textual content "Second merchandise"
                    EmailListItem -Textual content "Third merchandise"
                    EmailList {
                        EmailListItem -Textual content "Nested merchandise 1"
                        EmailListItem -Textual content "Nested merchandise 2"
                    }
                } -Kind Ordered -FontSize 15
            }
            EmailLayoutColumn {
                EmailList {
                    EmailListItem -Textual content "First merchandise - however on the proper"
                    EmailListItem -Textual content "Second merchandise - however on the proper"
                    EmailListItem -Textual content "Third merchandise"
                    EmailList {
                        EmailListItem -Textual content "Nested merchandise 1"
                        EmailListItem -Textual content "Nested merchandise 2"
                    }
                } -Kind Ordered -FontSize 10 -Colour RedBerry
            }
        }
        EmailLayoutRow {
            EmailText -Textual content "Lets see how one can have a number of logos subsequent to one another" -LineBreak
        }
        EmailLayoutRow {
            EmailLayoutColumn {
                EmailImage -Supply "https://www.google.com/photos/branding/googlelogo/2x/googlelogo_color_272x92dp.png" -Width 150
            } -PaddingTop 30
            EmailLayoutColumn {
                EmailImage -Supply "https://evotec.pl/wp-content/uploads/2015/05/Brand-evotec-012.png" -Width 150
            } -PaddingTop 30
            EmailLayoutColumn {
                EmailImage -Supply "https://add.wikimedia.org/wikipedia/commons/9/96/Microsoft_logo_percent282012percent29.svg" -Width 150
            } -PaddingTop 30
            EmailLayoutColumn {
                EmailImage -Supply "https://add.wikimedia.org/wikipedia/commons/thumb/f/fe/Pepsi_logo_percent282014percent29.svg/2560px-Pepsi_logo_percent282014percent29.svg.png" -Width 150
            } -PaddingTop 30
        }
        EmailLayoutRow {
            EmailText -LineBreak
            EmailText -LineBreak
        }
        EmailLayoutRow  Choose-Object -First 5 -Property Title, Id, PriorityClass, CPU, Product) -HideFooter
            EmailText -LineBreak
            EmailText -Textual content "Every thing is customizable. " -Colour California -FontStyle italic -TextDecoration underline
            EmailText -Textual content "You possibly can even add photos: " -LineBreak
            EmailImage -Supply "https://www.google.com/photos/branding/googlelogo/2x/googlelogo_color_272x92dp.png" #-Width 200 -Top 200
            EmailText -Textual content "It is all only a command away. " -Colour None -FontStyle regular -TextDecoration none
            EmailText -Textual content "You not have to make use of HTML/CSS, as it will likely be used for you!"
            EmailText -Textual content "With regards," -LineBreak
            EmailText -Textual content "Przemysław Kłys" -TextTransform capitalize -BackGroundColor Salmon
        
    }
}

Save-HTML -FilePath "check.html" -ShowHTML -HTML $Physique

Discover how utilizing EmailLayoutRow/EmailLayoutColumn, I put two lists and 4 photos subsequent to one another. It is nonetheless within the experimental stage however be at liberty to play and report suggestions on Github. Utilizing the Save-HTML operate helps check HTML in a browser somewhat than sending emails on a regular basis, however watch out that what you see within the browser typically could look totally different in e mail purchasers. All the time check earlier than manufacturing rollout!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments