Last active
December 27, 2018 13:36
-
-
Save gitfvb/c3f39d8671a737531721477f9c07151b to your computer and use it in GitHub Desktop.
Send a mail via powershell without installing any additional addon. Only SMTP required.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ################################################ | |
| # | |
| # PREPARATION / ASSEMBLIES | |
| # | |
| ################################################ | |
| # Load scriptpath | |
| if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") { | |
| $scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition | |
| } else { | |
| $scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) | |
| } | |
| cd $scriptPath | |
| ################################################ | |
| # | |
| # SAVE PASSWORD | |
| # | |
| ################################################ | |
| # save password with | |
| (Get-Credential).password | ConvertFrom-SecureString > MailPW.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ################################################ | |
| # | |
| # PREPARATION / ASSEMBLIES | |
| # | |
| ################################################ | |
| # Load scriptpath | |
| if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") { | |
| $scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition | |
| } else { | |
| $scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) | |
| } | |
| cd $scriptPath | |
| ################################################ | |
| # | |
| # SEND MAIL | |
| # | |
| ################################################ | |
| # like in https://www.windowspro.de/script/send-mailmessage-e-mails-versenden-powershell | |
| # read encrypted password | |
| $pw = Get-Content .\MailPW.txt | ConvertTo-SecureString | |
| $cred = New-Object System.Management.Automation.PSCredential "faststats@apteco-faststats.de", $pw | |
| Send-MailMessage -To "receiver@example.com"` | |
| -Subject "Low space warning"` | |
| -Body "Please check the memory of the server"` | |
| -SmtpServer "smtp.server.de"` | |
| -From "username@example.de"` | |
| -UseSsl -Port 587 -Credential $cred -encoding ([System.Text.Encoding]::UTF8) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment