PowerShell – Quick “Todo” Gmail Emailer


I put the following function in my PowerShell profile so that I can just bring up my PowerShell console and type myself a quick “todo” note that automatically gets emailed to me for later.  There are several things of note in this tiny script:

1.  Note the use of the $args variable to effectively allow the user to enter $msg parameter without quotes even if it contains whitespace.

2.  In this particular example I’m sending the email through Gmail.  smtp.gmail.com, port 587, SSL and authentication required.

3.  Check outhe $gmailcred variable, which gets set earlier in my profile.  It gets read from a secure string file.

function todo {
  param([string] $target = "work",
        [string] $msg )
  switch ($target)
  "work" { $target = "lance@3birdsmarketing.com" }
  "home" { $target = "lmrobins@gmail.com" }
  $emailFrom = "lmrobins@gmail.com"
  if ($args -ne "") {
    $msg = $msg + " " + $args
  $subject = "Todo: " + $msg
  $body = $msg
  $smtpServer = "smtp.gmail.com"  
  $smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
  $smtp.EnableSsl = $true
  $smtp.Credentials = [Net.NetworkCredential]($gmailcred)
  $smtp.Send($emailFrom, $target, $subject, $body)

Leave a Reply

Your email address will not be published. Required fields are marked *