Skip to content

Instantly share code, notes, and snippets.

@Joaquim3
Created July 28, 2024 11:37
Show Gist options
  • Select an option

  • Save Joaquim3/a0891befa3301ca67385d78f7673ea7c to your computer and use it in GitHub Desktop.

Select an option

Save Joaquim3/a0891befa3301ca67385d78f7673ea7c to your computer and use it in GitHub Desktop.
POWERSHELL ⇢ Sends an email
#-----------------------------------------------------------
# purpose : sends an email
# variables : $body
# $Subject
# $email
# $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