[Update: I have updated this blog to display the StackOverflow question using the new StackTack jQuery plugin.]
In a previous post, I looked at reducing the number of mouse clicks and key strokes required to send an email and other common tasks using Launchy. The solution involved using Outlook, as this was for my work PC. I started thinking about doing the same on my home laptop, where I don’t have Outlook installed. I remembered a blog article from Scott Hansleman on using the command line email tool Blat, and thought I could do something similar using a PowerShell script send email via Gmail.
Just to note, the script below is only valid for PowerShell V1; in PowerShell V2, there is a cmdlet available to do this, Send-MailMessage. I’ll look at this using this cmdlet in my next blog post.
A quick search of StackOverflow found the following:
My script is based on the answer submitted by user Christian:
param
(
[string]$email = $(read-host "Enter a recipient email"),
[string]$subject = $(read-host "Enter the subject header"),
[string]$body = $(read-host "Enter the email text (optional)")
)
# ==========================================================================
# Constants
# ==========================================================================
set-variable -name EmailAddress -value "testuser@gmail.com" -option constant
set-variable -name UserName -value "testuser" -option constant
set-variable -name PassWord -value "Password123" -option constant
# ==========================================================================
# Functions
# ==========================================================================
function Send-Email
(
[string]$recipientEmail = $(Throw "At least one recipient email is required!"),
[string]$subject = $(Throw "An email subject header is required!"),
[string]$body
)
{
$EmailFrom = $EmailAddress
$EmailTo = $recipientEmail
$Subject = $subject
$Body = $body
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($UserName, $PassWord);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Write-Host "Email sent!"
}
# ==========================================================================
# Main Script Body
# ==========================================================================
Write-Host "Starting Send-Gmail Script."
# Send email using Gmail
Send-Email -recipientEmail $email -subject $subject -body $body
Write-Host "Closing Send-Gmail Script."
# ==========================================================================
# End of Script Body
# ==========================================================================
Using the script above, and the previously mentioned way of calling PowerShell scripts from Launchy, I can quickly generate emails with a minimal number of keystrokes.
Download the script from here. If you have any comments, I would appreciate all feedback. Thanks.
Also, just to note that the code for this post was highlighted using the BlogTrog website.
Thanks for these scripts. I plan on using this with the new version of powershell on ubuntu.
ReplyDelete