Geeks With Blogs

Lance's TextBox » About Me » My Resume »Twitter

Over at the PowerScripting Podcast, the winners of the NetCmdlets scripting contest were announced. Meanwhile I was having some awesome vacation time in the beautiful Outer Banks of North Carolina.

Winners for the /n software Netcmdlets contest are:

  1. Steve Hiner - Wrote a wrapper for Send-Email script to send email using Gmail's SMTP server
  2. Mark Schill - Sent in two entries, the one we've chosen to highlight is his Twitter IM client bot thingy. It uses Send-IM and Get-Http
  3. Quintus - A script to download an ESPN podcast using the Get-Rss cmdlet

Here's one of the winning scripts, from Steve Hiner:

The script is very straightforward, it takes details about the email on the command line (or prompts for them if they are not specified there), and calls NetCmdlets send-email cmdlet to securely connect to the GMail SMTP server and send the email.
param(
      [string]$recipient=$(read-host "Who gets it")
    , [string]$subject=$(read-host "What's it about")
    , [string]$message=$(read-host "What do you want to say")
    , [string]$userid="GMAIL_USER_ID" #replace this with your ID or pass it on the command line
    , [System.Security.SecureString]$password=$(read-host -AsSecureString "Gmail Password")
)

function Extract-SecureString {
    param([System.Security.SecureString]$secstring)
    $ptr=[System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($secstring)
    [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)
    [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($ptr)
}

if (!(Get-PSSnapin NetCmdlets -ErrorAction SilentlyContinue)) {
     Add-PSSnapin NetCmdlets
}

$GmailServer = "smtp.gmail.com"
$Me = "$userid@gmail.com"

Send-Email -server $GmailServer -from $Me -to $recipient -subject $subject -message $message -SSL automatic -User $userid -Password $(Extract-SecureString($password))
Technorati Tags: ,
Posted on Wednesday, September 10, 2008 5:28 PM PowerShell | Back to top


Comments on this post: PowerShell: send-gmail

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net