Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to force the width of New-HTMLTable? #469

Open
TrevorW7 opened this issue Jan 10, 2025 · 6 comments
Open

How to force the width of New-HTMLTable? #469

TrevorW7 opened this issue Jan 10, 2025 · 6 comments

Comments

@TrevorW7
Copy link

I am using PSWriteHTML to format email. When I try New-HTMLTable, it seems the width is ignored and the table is always forced to 100% of the screen width. How do I force the size to less than the full screen width?

I tried:

  $html = New-HTML {
		New-HTMLPanel -Width '60%' -Content {
			New-HTMLTable -DataTable $data -HideFooter -ScreenSizePercent 60 {
				New-HTMLTableHeader -Names 'Filename' -FontFamily "Calibri" -FontSize 10 -Alignment Left -BackGroundColor '#2d5a8f' -Color White
				New-HTMLTableHeader -Names 'LineNumber' -FontFamily "Calibri" -FontSize 10 -Alignment Center -BackGroundColor '#2d5a8f' -Color White
				New-HTMLTableHeader -Names 'Line' -FontFamily "Calibri" -FontSize 10 -Alignment Left -BackGroundColor '#2d5a8f' -Color White
				#New-HTMLTableHeader -BackGroundColor Grey -Color White -Title 'Summary of Printer Backup Errors'
			}
		}
}

Neither the New-HTMLPanel resizes the table, nor the ScreenSizePercent parameter for New-HTMLTable.

How do I set the width when the HTML table is in the body of an email (I.e. read in Outlook)? Everything else works.

Thanks

@TrevorW7
Copy link
Author

I resolved this issue myself and actually improved significantly what I wanted anyway. So, for anyone else wanting to use PSWriteHTML to create email messages with the intent to eliminate the need for HTML expertise, and so HTML is no longer embedded in the script/code... here is the method I found effective:

$tableHTML = New-HTMLTable -DataTable $data -HideFooter {
	New-HTMLTableHeader -Names 'Filename' -FontFamily "Calibri" -FontSize 12 -Alignment Left -BackGroundColor '#2d5a8f' -Color White
	New-HTMLTableHeader -Names 'LineNumber' -FontFamily "Calibri" -FontSize 12 -Alignment Center -BackGroundColor '#2d5a8f' -Color White
	New-HTMLTableHeader -Names 'Line' -FontFamily "Calibri" -FontSize 12 -Alignment Left -BackGroundColor '#2d5a8f' -Color White
	#New-HTMLTableHeader -BackGroundColor Grey -Color White -Title 'Summary of Printer Backup Errors'
} -PreContent {
	New-HTMLHeading -Heading h2 -HeadingText 'A nice header can go here'
	$scriptBlock = {"An example having links to local file shares:  \\ServerName\SomeFileShare and \\AnotherServer\AnotherShare"} 
	New-HTMLTextBox -TextBlock $scriptBlock
} -PostContent {
	New-HTMLTextBox -TextBlock {"Here we should put some form of footer in the message"}
}

$tableHTML = $tableHTML.Replace('table width="100%"','table width="1000"')		
$Message = New-Object Net.Mail.MailMessage "[email protected]", "[email protected]"
$Message.IsBodyHtml = $true
$Message.Subject = "This is a test email"
$Message.Body = $tableHTML

$SMTPClient = New-Object Net.Mail.SmtpClient("smtpserver.somedomain.com")
$SMTPClient.EnableSsl = $false
$SMTPClient.Send($Message)

I no longer use 'New-HTML' as that embeds an enormous amount of javascript and other HTML in order to support an interactive web page. That isn't supported in email; especially, in Outlook so the method I provided above provides clean, limited, HTML that only includes what is necessary to be displayed in an email message (the goal for me).

I use 'New-TableHTML' to get the HTML necessary to display an HTML table in the email (no java script, unneeded functions, or unnecessary HTML is included). It's much cleaner. NOTE: The data for the table is a PowerShell array of PSCustomObjects.

'New-HTMLTableHeader' formats the table's column headers.

The 'PreContent' section is used to include HTML above the table.
The 'PostContent' section is used to include HTML below the table.

To solve my table size option, I replaced text in the output (stored in the '$tableHTML' variable)

$tableHTML = $tableHTML.Replace('table width="100%"','table width="1000"')

NOTE: My problem is that the table width was set to 100% and I found no option to change the table width. The code above replaces 100% with a fixed value of "1000". It could be any value that works including a percentage of the screen width and this works.

This is just standard PowerShell to send the HTML as the body of the email message.

$Message = New-Object Net.Mail.MailMessage "[email protected]", "[email protected]"
$Message.IsBodyHtml = $true
$Message.Subject = "This is a test email"
$Message.Body = $tableHTML

$SMTPClient = New-Object Net.Mail.SmtpClient("smtpserver.somedomain.com")
$SMTPClient.EnableSsl = $false
$SMTPClient.Send($Message) 

Works great. You can see from the code, no HTML expertise is really needed. Just find the raw HTML commands like 'New-HTMLTextbox' in the Public section use what you need.

NOTE: Email does not support interactive content, for email, you likely don't need anything more than my example to send HTML formatted messages with color, various fonts and font sizes as well as including formatted data tables.

This is what I was after... Simple and easy to automate HTML formatted messages that look professional.

@PrzemyslawKlys
Copy link
Member

You're using it wrong... there is dedicated code for email body functionality in PSWriteHTML.

Multiple other examples are shown in examples for emails, including on my blog page.

If you use it properly it doesnt add javascript and other non working content.

@PrzemyslawKlys
Copy link
Member

@TrevorW7
Copy link
Author

TrevorW7 commented Jan 10, 2025

Okay, if I use EmailBody to format the HTML, how do I prevent the table from spanning the width of the screen (my original question)?

Simple example provided:

$Table = (Get-Process | Select-Object -First 2 -Property Name, BasePriority, Company, CompanyName)

$Body = EmailBody -FontSize 12px -FontFamily 'Tahoma' {
    EmailText -Text 'This should be font 8pt, table should also be font 8pt' -FontSize 8pt
    EmailTable -DataTable $Table -HideFooter {
        EmailTableCondition -Name 'Name' -Operator eq -Value 'svchost' -BackgroundColor TeaGreen -FailBackgroundColor Salmon -Inline
    }
}

Send-MailMessage -Body $Body

NOTE/FYI: You will get this message when using "Send-MailMessage -Body"

WARNING: The command 'Send-MailMessage' is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage at this time. See https://aka.ms/SendMailMessage for more information.

My example of $SMTPClient.Send($Message) is a better option

@TrevorW7 TrevorW7 reopened this Jan 10, 2025
@PrzemyslawKlys
Copy link
Member

It actually isnt. It uses same .NET code as the obsolete version. Thats why I wrote Mailozaurr module. Your original question - in short its not done, maybe email layout example would work. I havent tested, but it may work.

@TrevorW7
Copy link
Author

Thanks for letting me know setting the table width hasn't been added yet. I was able to use a similar workaround. Here is the modified example:

$Table = (Get-Process | Select-Object -First 2 -Property Name, BasePriority, Company, CompanyName)

$Body = EmailBody -FontSize 8px -FontFamily 'Tahoma' {
EmailText -Text 'This should be font 8pt, table should also be font 8pt' -FontSize 8pt
EmailTable -DataTable $Table -HideFooter {
EmailTableCondition -Name 'Name' -Operator eq -Value 'svchost' -BackgroundColor TeaGreen -FailBackgroundColor Salmon -Inline
}
}
$Body = $Body.Replace('table width="100%"','table width="600"')
$Body = $Body.Replace('width: 100%','width: 600')

Send-MailMessage -Body $Body

That allows me to override the table's width and gives me what I need.

Thanks

NOTE: I left this open because I suspect a lot of users will want setting the table width supported without a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants