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

8 comments:

  1. I have been looking for a way for my script output that is sent in an email to look a lot "prettier" so this is a good find. Thanks!!!

    -Rachael

    ReplyDelete
    Replies
    1. Hi Rachael.

      I glad that this helps you :)

      thanks
      aman

      Delete
  2. The problem with send-mailmessage is that we cant give the port number as the syntax of send-mailmessage is not having that "port" option.

    Can u help me in setting port number in send-mailmessage command???

    ReplyDelete
    Replies
    1. Hi Vishnu,
      Yes Send-MailMessage in Powershell Version-2 dont have any option to define port Number, But in Powershell Version 3 we do have this option now.

      i think you can try to build a .net based SendMail Message function.

      http://blogs.msdn.com/b/rkramesh/archive/2012/03/16/sending-email-using-powershell-script.aspx

      i think if u try and search i think u can add port number in above mentioned function {not sure but worth try}.

      thanks
      aman

      Delete
  3. How to modify this script to send this email to multiple recipients...
    thank you

    ReplyDelete
  4. Is this script can be used in Powershell version 3.0? I've tried this script but it cannot recognized "send-MailMessage -SmtpServer"..what can i do? How can i modify it? Your help will be much appreciated. Thank you.

    ReplyDelete
  5. use send-mailmessage for PS3.

    ReplyDelete
  6. use send-mailmessage for PS3.

    ReplyDelete

Note: Only a member of this blog may post a comment.