Use Powershell To Send Email With Gmail Account

Pavol Kutaj
2 min readMar 31, 2021

--

The concern is documenting sending emails from CLI which I use to quickly sending emails to myself as not to distract myself from the task-at-hand. Just type eml and write a TODO to get the idea out of your head into the mailbox.

1. Allow less secure apps

  • note that this is not allowed for 2-step verification enabled

2. Script

  • here’s the script, with hardcoded to and from, since I am sending emails to myself and using inbox as todo list
  • save a password to your Gmail account into an persistant environmental variable for your user account with
function sendEmail {
param (
[int]$i
)
$from = "pkutaj@gmail.com" #CHANGE THIS
$to = "pavol@snowplowanalytics.com" #CHANGE THIS
$password = ConvertTo-SecureString -String $env:emailPassword -AsPlainText -Force
$credentials = New-Object Management.Automation.PSCredential -ArgumentList $from, $password
$subject = Read-Host "enter TODO"
$smtpServer = "smtp.gmail.com"
Send-MailMessage -From $from -To $to[$i] -Subject $subject -SmtpServer $smtpServer -Credential $credentials -Verbose -UseSsl -BodyAsHtml -Encoding "Unicode"
#cls
Write-Host "the raven flew to" $to[$i];
}

--

--

No responses yet