As I mentioned at the beginning of this blog post, Send-MailMessage is obsolete. While it doesn't mean it will stop working anytime soon, I've decided to create my command and enhance it with features. Send-EmailMessage should be able to be drag and drop replacement for your standard scenarios and then used in all other situations you may have.
if (-not $MailCredentials) {
$MailCredentials = Get-Credential
}
# this is simple replacement (drag & drop to Send-MailMessage)
Send-EmailMessage -To 'przemyslaw.klys@test.pl' -Subject 'Test' -Body 'test me' -SmtpServer 'smtp.office365.com' -From 'przemyslaw.klys@test.pl' `
-Attachments "$PSScriptRoot\..\README.MD", "$PSScriptRoot\..\Mailozaurr.psm1" -Encoding UTF8 -Cc 'przemyslaw.klys@test.pl' -Priority High -Credential $MailCredentials `
-UseSsl -Port 587 -Verbose
$Body = EmailBody {
EmailText -Text 'This is my text'
EmailTable -DataTable (Get-Process | Select-Object -First 5 -Property Name, Id, PriorityClass, CPU, Product)
}
$Text = 'This is my text'
Send-EmailMessage -From @{ Name = 'Przemysław Kłys'; Email = 'przemyslaw.klys@test.pl' } -To 'przemyslaw.klys@test.pl' `
-Server 'smtp.office365.com' -Credential $MailCredentials -HTML $Body -Text $Text -DeliveryNotificationOption OnSuccess -Priority High `
-Subject 'This is another test email' -SecureSocketOptions Auto
As you can see above, I'm using PSWriteHTML to create a simple HTML $Body for demonstration purposes. I'm also defining the same text as a string. This is because Send-EmailMessage can take both at the same time, but it's not necessary. Finally, I'm sending emails two times using similar, but not the same parameter sets with attached files.
Since we requested confirmation from server that our message was sent, we got that as well.
We can do the same thing with a bit more complicated HTML (built with PSWriteHTML).
$Body = EmailBody -FontFamily 'Calibri' -Size 15 {
EmailText -Text 'Hello ', $UserNotify, ',' -Color None, Blue, None -Verbose -LineBreak
EmailText -Text 'Your password is due to expire in ', $PasswordExpiryDays, 'days.' -Color None, Green, None
EmailText -LineBreak
EmailText -Text 'To change your password: '
EmailText -Text '- press ', 'CTRL+ALT+DEL', ' -> ', 'Change a password...' -Color None, BlueViolet, None, Red
EmailText -LineBreak
EmailTextBox {
'If you have forgotten your password and need to reset it, you can do this by clicking here. '
'In case of problems please contact the HelpDesk by visiting [Evotec Website](https://evotec.xyz) or by sending an email to Help Desk.'
}
EmailText -LineBreak
EmailText -Text 'Alternatively you can always call ', 'Help Desk', ' at ', '+48 22 00 00 00' `
-Color None, LightSkyBlue, None, LightSkyBlue -TextDecoration none, underline, none, underline -FontWeight normal, bold, normal, bold
EmailText -LineBreak
EmailTextBox {
'Kind regards,'
'Evotec IT'
}
}
if (-not $MailCredentials) {
$MailCredentials = Get-Credential
}
Send-EmailMessage -From @{ Name = 'Przemysław Kłys'; Email = 'przemyslaw.klys@test.pl' } -To 'przemyslaw.klys@test.pl' `
-Server 'smtp.office365.com' -SecureSocketOptions Auto -Credential $MailCredentials -HTML $Body -DeliveryNotificationOption OnSuccess -Priority High `
-Subject 'This is another test email'
Send-MailMessage -To 'przemyslaw.klys@test.pl' -Subject 'Test' -Body 'test me' -SmtpServer 'smtp.office365.com' -From 'przemyslaw.klys@test.pl' `
-Attachments "$PSScriptRoot\..\Mailozaurr.psd1" -Encoding UTF8 -Cc 'przemyslaw.klys@test.pl' -DeliveryNotificationOption OnSuccess -Priority High -Credential $MailCredentials -UseSsl -Port 587 -Verbose
As you can see, two emails arrived, actually four because I also received two emails with confirmation about delivery. There was PSD1 attached to one of the emails which was blocked by my Outlook. You can also use UserName and Password using clear text but I don't recommend it. I've added it because you never know, but I would suggest using the PSCredential parameter.