locked
How to format the text in mail body RRS feed

  • Question

  • I am sending a particular section of a text file from mail to myself using powershell. When i am sending the mail, the text doesn't look like readable. Can someone help in making the mail body readable as it looks in text file.

    The content of the text file is

                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :        84         2        82         0         0         0
       Files :       949       343       606         0         0         2
       Bytes :  518.00 m    3.38 m  514.62 m         0         0    20.4 k

       Times :   0:01:22   0:00:56                       0:00:00   0:00:25


       Speed :               62418 Bytes/sec.
       Speed :               3.571 MegaBytes/min.
         

    ###My code
    
    $user = "asharma5"
    $newpath = "\\homedir\home3"
    
    robocopy "\\homedirectory\home2\asharma5" "$newpath\$user" /MIR /E /V /NP /LOG+:H:\$user.txt
    
    $text = gc H:\$user.txt
    
    $report = $text[-11..-3]
    
    $body1 = $report  | %{"$_<br/>"} 
    
    $body = "$body1"
    
    Send-MailMessage -From ankit_sharma2@xyz.com -to ankit_sharma2@xyz.com -SmtpServer smtp.corpads.local -Subject "Migration report $user " -Body $body 


    Mail that i am receiving is looking like below. The body part is not readable.




    • Edited by asharma5 Tuesday, December 6, 2016 5:41 AM
    Tuesday, December 6, 2016 5:36 AM

Answers

  • $user = 'asharma5'
    $splat = @{
    	From = 'ankit_sharma2@xyz.com'
    	To = 'ankit_sharma2@xyz.com'
    	SmtpServer = 'smtp.corpads.local'
    	Subject = "Migration report $user" 
    	BodyAsHtml = $true
    }
    $newpath = '\\homedir\home3'
    robocopy \\homedirectory\home2\asharma5 "$newpath\$user" /MIR /E /V /NP /LOG+:H:\$user.txt
    
    $text = gc H:\$user.txt
    $report = $text[-11 .. -3]
    $body = '<pre>'+ ($report | Out-String) + '</pre>'
    
    Send-MailMessage -Body $body @splat
     

    This how to convert plain text to display better in an email.

    You can add a style sheet to further format and make the output fancy.


    \_(ツ)_/


    • Edited by jrv Tuesday, December 6, 2016 6:14 AM
    • Marked as answer by asharma5 Tuesday, December 6, 2016 6:32 AM
    Tuesday, December 6, 2016 6:13 AM

All replies

  • The format and font of text in a mail message is set by the mail client.  You cannot change this in a plain text mail message.

    You can send it as HTML and set the font to a fixed font and it will display better


    \_(ツ)_/

    Tuesday, December 6, 2016 5:48 AM
  • without html i am getting this 


     with html i am getting this. Still it is not readable


    Tuesday, December 6, 2016 6:01 AM
  • $user = 'asharma5'
    $splat = @{
    	From = 'ankit_sharma2@xyz.com'
    	To = 'ankit_sharma2@xyz.com'
    	SmtpServer = 'smtp.corpads.local'
    	Subject = "Migration report $user" 
    	BodyAsHtml = $true
    }
    $newpath = '\\homedir\home3'
    robocopy \\homedirectory\home2\asharma5 "$newpath\$user" /MIR /E /V /NP /LOG+:H:\$user.txt
    
    $text = gc H:\$user.txt
    $report = $text[-11 .. -3]
    $body = '<pre>'+ ($report | Out-String) + '</pre>'
    
    Send-MailMessage -Body $body @splat
     

    This how to convert plain text to display better in an email.

    You can add a style sheet to further format and make the output fancy.


    \_(ツ)_/


    • Edited by jrv Tuesday, December 6, 2016 6:14 AM
    • Marked as answer by asharma5 Tuesday, December 6, 2016 6:32 AM
    Tuesday, December 6, 2016 6:13 AM
  • awesome results. Thanks Jrv... 
    Tuesday, December 6, 2016 6:33 AM
  • You can also do this:

    $user = 'asharma5'
    $splat = @{
    	From = 'ankit_sharma2@xyz.com'
    	To = 'ankit_sharma2@xyz.com'
    	SmtpServer = 'smtp.corpads.local'
    	Subject = "Migration report $user" 
    	BodyAsHtml = $true
    }
    $pre = '
    	<h1 style="color: darkgreen">This is Your Report Title</h1>
    	<pre 
    		style="
    			background-color: antiquewhite;
    			border-style: solid;
    			border-color: black;
    			color: darkblue;
    			font-weight: bold;
    ">'
    $newpath = '\\homedir\home3'
    robocopy \\homedirectory\home2\asharma5 "$newpath\$user" /MIR /E /V /NP /LOG+:H:\$user.txt
    
    $text = gc H:\$user.txt
    $report = $text[-11 .. -3]
    $body = $pre + ($report | Out-String) + '</pre>'
    
    Send-MailMessage -Body $body @splat
     


    \_(ツ)_/

    Tuesday, December 6, 2016 7:03 AM