Showing posts with label Html email in PowerShell. Show all posts
Showing posts with label Html email in PowerShell. Show all posts

Tuesday, October 18, 2011

Send HTML formatted email using PowerShell


Hi Guys,
Did you ever tried to send HTML formatted colour coded email using PowerShell ? I was trying to do it but didn’t get any good and useful information on it, but i finally manage to get it done.
So lets start:
For example i am sending a test email from my official account to my GMAIL account.
 the script will look like  this:

##############  Script start Here ##########
# $smtp variable contain the name/ip of your email server ##
# $to contain the email id whom you want to send email ###
# $from contain email of sender ###
# $Subject contain subject of the email.
# In Body we are defining some HTML coding in to our email message body
# <b> means BOLD
#<br> means Break go to next Line
#<a href> provide a link to the text
# <font color=red> , give the color to the font
$smtp = "Exchange-Server"
$to = "Aman Dhally <amandhally@gmail.com>"
$from = "Aman Singh <aman.dhally@analysysmason.com>"
$subject = "This is a Test of HTML Email"

$body = "Dear <b><font color=red>$to</b></font> <br>"
$body += "We are testing <b>HTML</b> email <br>"
$body += "Click <a href=
http://www.google.com>here</a> to open google <br>"
#### Now send the email using \> Send-MailMessage
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high
########### End of Script################

Note: Make sure you use –BodyasHtml parameter in Send-MailMessage otherwise it send as plain text format.
Now lets see how this email will look like :)
Send-MailMessage
Cool!!! it look exactly how we have define it in our Script :)
you can find more HTML coded which you can use in script from here http://www.w3schools.com/html/default.asp

take care
aman dhally