Created
July 28, 2024 11:37
-
-
Save Joaquim3/a0891befa3301ca67385d78f7673ea7c to your computer and use it in GitHub Desktop.
POWERSHELL ⇢ Sends an email
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
| #----------------------------------------------------------- | |
| # purpose : sends an email | |
| # variables : $body | |
| # $Subject | |
| # $From | |
| # $SMTP | |
| # $myPassword | |
| # just adapt to your credentials | |
| #----------------------------------------------------------- | |
| Function SendToEmail(){ | |
| $From = "MYEMAIL@***MYDOMAIN.***" | |
| $SMTP = "MYSMTPSERVER***" | |
| # Force TLS1.2 | |
| [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; | |
| $myPassword = ConvertTo-SecureString "***MYPASSWORD***" -AsPlainText -Force | |
| $emailCredentials = New-Object pscredential ($From, $myPassword) | |
| try { | |
| Send-MailMessage -From $From -To $email -Subject $Subject -Body $body -BodyAsHtml -SmtpServer $SMTP -Port 587 -Credential $emailCredentials -Encoding UTF8 -UseSsl -ErrorAction SilentlyContinue | |
| } | |
| catch { | |
| echo $error[0] | |
| } | |
| write-host "Message sent to" $email; | |
| } | |
| cls | |
| #-------------------------------------------------------------------- | |
| # For Powershell : just clic YES to ALL when prompted. | |
| #-------------------------------------------------------------------- | |
| # Disallow script restrictions policies | |
| #-------------------------------------------------------------------- | |
| #Set-Executionpolicy RemoteSigned | |
| #Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted" | |
| #-------------------------------------------------------------------- | |
| $body = "Hello Space" | |
| $Subject = "Space is ready" | |
| $email = "ANYRECEIVER@***DOMAIN***" | |
| #example: | |
| SendToEmail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment