question

FaizanSayed-1938 avatar image
0 Votes"
FaizanSayed-1938 asked FaizanSayed-1938 answered

Passing the content of txt file to body of Send-MailMessage in powershell

I want to Pass the content of txt file to body of Send-MailMessage in powershell I tried using the out-sting function and also use FT combination but its giving output in single line over email body I have to print the list of users one below other same as in text file but its getting printer in one line on email body


  $path = "H:\userlist.txt" 
 $groups = Get-Content $path 
 $groups1 = $groups|format-table|out-string 
 Send-MailMessage -From "xyz@abg.com" -To test@abc.com -Subject test -BodyAsHtml -Body "User list below $groups1" -SmtpServer smtpgate.net 


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

You can add the <br> tags to insert line breaks since you use the "-BodyAsHtml" parameter.

 $path = "H:\userlist.txt" 
 $groups = (Get-Content $path) -join '<br>'
 Send-MailMessage -From "xyz@abg.com" -To test@abc.com -Subject test -BodyAsHtml -Body "User list below $groups1" -SmtpServer smtpgate.net 

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

The -Body parameter expects a string as its argument. To get the contents of your file as a single string, add the -Raw switch to the Get-Content. The result will contain all the CrLf line-endings, so you'll see pretty much what you see if you opened the file in NotePad.

Give this a try:

 $path = "H:\userlist.txt" 
 $groups = Get-Content $path -Raw
 Send-MailMessage -From "xyz@abg.com" -To test@abc.com -Subject test -BodyAsHtml -Body "User list below`r`n$groups" -SmtpServer smtpgate.net 
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

FaizanSayed-1938 avatar image
0 Votes"
FaizanSayed-1938 answered

Thanks @IanXue-MSFT by adding -join '<br>' its working now

 $groups = (Get-Content $path) -join '<br>'
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.