Send an email from PowerShell

I’ve used similar code in a few scripts, I have here – I’m putting this post here as a fast reference for those times when I want to copy/paste some email code…

$EmailFrom = "youremail@yourdomain.com"
$EmailTo = "systemreplyemail@yourdomain.com"
$EmailSubject = "Email Subject"
$EmailBody = "Email Body"
$SMTPServer = "yoursmtpserver.yourdomain.com"
$logfile = $filenameToAttach

Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -body $EmailBody -attachments $Logfile -SmtpServer $SMTPServer

Note this is just a basic example, as always get-help send-mailmessage is the best way to get some additional info on attachments, multiple recipients, credentials etc…

 

Leave a Reply