# Powershell send a table to gmail

**URL:** <https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864>\
**Category:** PowerShell Help\
**Created:** [December 15, 2017, 12:29pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864 "2017-12-15T12:29:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![noprofile](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@noprofile](https://forums.powershell.org/u/noprofile)\
**Post date:** [December 15, 2017, 12:29pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/1 "2017-12-15T12:29:17Z")

</div>

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](mailto:me@gmail.com)”)  
$EmailFrom = “[notifications@gmail.com](mailto:notifications@gmail.com)”  
$Subject = “Notifications”  
$Body = $EmailBody  
$SMTPServer = “[smtp.gmail.com](http://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](mailto:notification@gmail.com)”, “xxxxxx”); $SMTPClient.Send($SMTPMessage)

---

<div class="post-metadata">

**Author:** ![jacorbello](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jacorbello](https://forums.powershell.org/u/jacorbello)\
**Post date:** [December 15, 2017, 12:47pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/2 "2017-12-15T12:47:57Z")

</div>

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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1)

---

<div class="post-metadata">

**Author:** ![noprofile](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@noprofile](https://forums.powershell.org/u/noprofile)\
**Post date:** [December 15, 2017, 1:02pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/3 "2017-12-15T13:02:17Z")

</div>

Do you send to Gmail or Exchange?

---

<div class="post-metadata">

**Author:** ![jacorbello](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jacorbello](https://forums.powershell.org/u/jacorbello)\
**Post date:** [December 15, 2017, 1:08pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/4 "2017-12-15T13:08:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noprofile](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@noprofile](https://forums.powershell.org/u/noprofile)\
**Post date:** [December 15, 2017, 1:39pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/5 "2017-12-15T13:39:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [December 15, 2017, 1:51pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/6 "2017-12-15T13:51:22Z")

</div>

Take this FWIW

> **[HTML Tags Supported in Gmail](https://googlesystem.blogspot.com/2014/08/html-tags-supported-in-gmail.html#gsc.tab=0)**

---

<div class="post-metadata">

**Author:** ![noprofile](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@noprofile](https://forums.powershell.org/u/noprofile)\
**Post date:** [December 15, 2017, 1:58pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/7 "2017-12-15T13:58:19Z")

</div>

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](mailto:me@gmail.com)”  
[String] $From = “[notifications@gmail.com](mailto:notifications@gmail.com)”  
[String] $Subject = “Notifications”  
[String] $SMTPServer = “[smtp.gmail.com](http://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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:36pm UTC](https://forums.powershell.org/t/powershell-send-a-table-to-gmail/9864/8 "2024-05-16T20:36:59Z")

</div>

Closed on May 16, 2024, 8:36pm UTC


