r/PowerShell • u/MadBoyEvo • Oct 09 '22
Script Sharing Replacing SMTP Basic Auth with Graph API is as easy as drag & drop replacement
Microsoft has now disabled basic auth for Office 365, with the minor exception of SMTP. While SMTP Basic Auth is not going away because of mostly legacy devices or applications, it's better to switch to oAuth 2.0 and Graph API as per Microsoft's recommendation.
The more significant issue is that Microsoft points you to Graph API documentation, specifically this link: https://learn.microsoft.com/en-us/graph/api/user-sendmail. If you want to upload attachments higher than 4MB, they send you to this link https://learn.microsoft.com/en-us/graph/outlook-large-attachments where after a brief moment, you decide to drop that idea and not send emails with larger attachments ;-)
Over one year ago, I wrote a PowerShell module called Mailozaurr, which simplifies sending emails with SMTP using BASIC Authentication, OAuth 2.0 and Graph API, or even SendGrid API. Last week I added functionality to send a that big attachment with Graph.
As Microsoft just disabled basic auth, I thought it would be a good idea to write a new blog post on how easy it is to replace Send-MailMessage with Send-EmailMessaage (part of the Mailozaurr module).
Blog post: https://evotec.xyz/easy-way-to-send-emails-using-microsoft-graph-api-office-365-with-powershell/ with examples/how-to and some configuration options and screenshots
Sources: https://github.com/EvotecIT/Mailozaurr (MIT license)
$Credential = ConvertTo-GraphCredential -ClientID $ClientID -ClientSecret $ClientSecret -DirectoryID $DirectoryID
# Sending email
Send-EmailMessage -From @{ Name = 'Przemysław Kłys'; Email = '[email protected]' } -To '[email protected]' `
-Credential $Credential -HTML $Body -Subject 'This is another test email 1' -Graph -Verbose -Priority High
# sending an email with From as a string (it won't matter for Exchange )
Send-EmailMessage -From '[email protected]' -To '[email protected]' `
-Credential $Credential -HTML $Body -Subject 'This is another test email 2' -Graph -Verbose -Priority LowThe only difference between using Standard SMTP and SMTP with Basic auth is renaming Send-MailMessage to Send-EmailMessage, adding a Graph switch, and changing SMTP Server, SMTP port to Graph API application which unfortunately requires some effort. But it's no longer necessary to play with what Microsoft offers as an alternative :-)
As part of the blog post, I'm also showing how you no longer have to use HTML/CSS to build nice-looking emails, and you can skip having to learn it, so it doesn't look ugly.
6
u/Roman1410S Oct 09 '22
Great article. The Microsoft.Graph Commandlets are frankly spoken terrible and the output objects are a myracle to me. Automation is not always the best option, especially when generating PowerShell Modules from OpenAPI specs...
4
u/MadBoyEvo Oct 09 '22
Those are pain. It will take a lot of community work to make them usable, or Microsoft has to change its approach.
3
u/sircruxr Oct 10 '22
It makes you wonder why Jeffery (founder of PS) left Microsoft and went to Google. This switch to the graph platform has been awful.
8
u/[deleted] Oct 09 '22
Well done! Although I suppose devices that don’t support oauth are SOL and stuck with basic auth, right?