Author Posts

December 15, 2017 at 5:59 pm

I have a table generated by powershell, I used to be able to send the table to exchange as a html in email body. but now, we have migrated to gmail and the table shows in gmail is just all html code or csv code. After some digging, looks like gmail wont support what I am trying to do. But I tried to copy the table into gmail directly and i received it successfully as no format changes.

As an alternative, I can have the table attached as html or csv files but i still want to have it in email body.

Is there anyway that I can still send my table to gmail from powershell?

$myTable = {PS scripts}

$EmailBody = $myTable | ConvertTo-html

$EmailTo = @("")
$EmailFrom = ""
$Subject = "Notifications"
$Body = $EmailBody
$SMTPServer = ""
# $filenameAndPath = "C:\temp\test.csv"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
# $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
# $SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("", "xxxxxx");

December 15, 2017 at 6:17 pm

Not sure why that isn't working, but I've had good luck with Send-MailMessage

So your syntax when using Send-MailMessage would be something like this

$smtpsettings = @{ 
    To =  "" 
    From = "" 
    Subject = "$Subject" 
    SmtpServer = "" 
$creds = Get-Credential
Send-MailMessage @smtpsettings -Body $EmailBody -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8) -Credential $creds

Check out this site for more documentation and examples.

December 15, 2017 at 6:32 pm

Do you send to Gmail or Exchange?

December 15, 2017 at 6:38 pm

I use this on o365 and Exchange. But the premise should be the same.

December 15, 2017 at 7:09 pm

No, If i send to our exchange it just works fine. but with gmail. looks like it wont take html tags.... so everything in email body is just all html codes

December 15, 2017 at 7:28 pm

It is working now. Thanks for the help

$myTable = {PS scripts}

$Header = @"

TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}

[String[]] $To = ""
[String] $From = ""
[String] $Subject = "Notifications"
[String] $SMTPServer = ""
[String] $$SMTPPort = "587"
[bool] $UseSsl = $true
$Credential = new-object Management.Automation.PSCredential “”, ("password” | ConvertTo-SecureString -AsPlainText -Force)

$EmailBody = $myTable | ConvertTo-Html -Header | Out-String

Send-MailMessage -Body $EmailBody -BodyAsHtml -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer -Credential $Credential -UseSsl