locked
Send HTML email within Powershell Script RRS feed

  • Question

  • Hello all,

    I have a PowerShell script that looks up when the users password was last set, I can't seem to get the message format to send as HTML, I have the message body a "text" currently through a variable $MessageContent nested within a Function.  Please see example below:

    $MessageContent=



    "

          

          Hello

    $firstname,

          

           Your password was reset on

    $lastset


          

           If this was not your intention please contact the IT Helpdesk.

          

          

           "

    Would like to replace the above quoted text within the variable with:

    <html>

          <body>

               <img src=./myimage.gif>

               <font face=verdana color=blue>Hello $firstname</font><br>

               <font face=verdana color=red> Your password was last set on $lastset</font><br>

          </body>

    </html>

    Any suggestions would be helpful.

    Thanks In Advance.




    Thursday, January 18, 2018 4:36 PM

Answers

  • Get-Help Send-MailMessage -Full

    Parameter switch of -BodyAsHtml would be the key

    $body = @"
      <html>
    
          <body>
    
               <img src=./myimage.gif>
    
               <font face=verdana color=blue>Hello $firstname</font><br>
    
               <font face=verdana color=red> Your password was last set on $lastset</font><br>
    
          </body>
    
    </html>
    "@
    
    Send-MailMessage -Body $body -BodyAsHtml
    You need to add a few more parameters to Send-MailMessage, but hopefully you get the idea


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Thursday, January 18, 2018 5:21 PM added script
    • Marked as answer by PowerShellNewb77 Thursday, January 18, 2018 7:22 PM
    Thursday, January 18, 2018 5:18 PM

All replies

  • Get-Help Send-MailMessage -Full

    Parameter switch of -BodyAsHtml would be the key

    $body = @"
      <html>
    
          <body>
    
               <img src=./myimage.gif>
    
               <font face=verdana color=blue>Hello $firstname</font><br>
    
               <font face=verdana color=red> Your password was last set on $lastset</font><br>
    
          </body>
    
    </html>
    "@
    
    Send-MailMessage -Body $body -BodyAsHtml
    You need to add a few more parameters to Send-MailMessage, but hopefully you get the idea


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Thursday, January 18, 2018 5:21 PM added script
    • Marked as answer by PowerShellNewb77 Thursday, January 18, 2018 7:22 PM
    Thursday, January 18, 2018 5:18 PM
  • OT: I think users will be more happy for information when their password will expire :)  

    I see no reason for this "helpdesk" message.  If it wasnt their intention (someone else changed their password), they propably never read that mail because they propably wouldnt have access to the mail system.   

    As I said, a little bit offtopic.  

    Thursday, January 18, 2018 5:47 PM