Skip to content

Instantly share code, notes, and snippets.

@yukoff
Created April 11, 2024 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukoff/f990e3daf4fbac2eab1e7fceaeaeeb6a to your computer and use it in GitHub Desktop.
Save yukoff/f990e3daf4fbac2eab1e7fceaeaeeb6a to your computer and use it in GitHub Desktop.
Example PowerShell script to send SMTP emails from Windows
# curl / wget are aliases for Invoke-WebRequest
$ip = Invoke-WebRequest -Uri https://ifconfig.co -ContentType "text/plain" -UserAgent curl
$hostname = hostname
$EmailFrom = "no-reply@$hostname"
$EmailTo = "email@address.tld"
$Subject = "Remote address"
$Body = "Current IP address: $ip"
$SMTPServer = "smtp.mailgun.org"
$SMTPPort = 587
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(
"login",
"password"
);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment