Powershell send a table to gmail

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 = @(“me@gmail.com”)
$EmailFrom = “notifications@gmail.com
$Subject = “Notifications”
$Body = $EmailBody
$SMTPServer = “smtp.gmail.com

$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(“notification@gmail.com”, “xxxxxx”);
$SMTPClient.Send($SMTPMessage)

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 =  "me@gmail.com" 
    From = "notifications@gmail.com" 
    Subject = "$Subject" 
    SmtpServer = "smtp.gmail.com" 
    } 
$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. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1

Do you send to Gmail or Exchange?

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

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

Take this FWIW

https://googlesystem.blogspot.com/2014/08/html-tags-supported-in-gmail.html#gsc.tab=0

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 = “me@gmail.com
[String] $From = “notifications@gmail.com
[String] $Subject = “Notifications”
[String] $SMTPServer = “smtp.gmail.com
[String] $$SMTPPort = “587”
[bool] $UseSsl = $true
$Credential = new-object Management.Automation.PSCredential “notification@gmail.com”, ("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