Fredrik L
Fredrik L

Reputation: 51

Read Gmail emails using Powershell

We want to automate actions based on emails we receive by email from different management systems from devices in our network

I have tried this small script, but it lists only the subject, not the body

# load rss-feed
$webclient = new-object System.Net.WebClient

# access the rss-feed
$webclient.Credentials = new-object System.Net.NetworkCredential ("scominbox@domain", "Password")

# download the rss as xml
[xml]$xml= $webclient.DownloadString("https://mail.google.com/mail/feed/atom")

# display only sender name and message title as custom table
$format= @{Expression={$_.title};Label="Title"},@{Expression={$_.author.name};Label="Author"}

# display the table
$xml.feed.entry | format-table $format

How can I read the emails?

Upvotes: 3

Views: 2671

Answers (3)

Amneet Bector
Amneet Bector

Reputation: 1

You can use:

$xml.feed.entry | Select *

It will return the data in the following format:

title           : 
summary         : Test body
link            : link
modified        : <date>
issued          : <date>
id              : <date>
author          : author
Name            : entry
LocalName       : entry
NamespaceURI    : http://purl.org/atom/ns#
Prefix          : 
NodeType        : Element
ParentNode      : feed
OwnerDocument   : #document
IsEmpty         : False
Attributes      : {}
HasAttributes   : False
SchemaInfo      : System.Xml.XmlName
InnerXml        : <OMITTED>
InnerText       : <OMITTED>
NextSibling     : 
PreviousSibling : modified
Value           : 
ChildNodes      : {title, summary, link, modified...}
FirstChild      : title
LastChild       : author
HasChildNodes   : True
IsReadOnly      : False
OuterXml        : <OMITTED>
BaseURI         : 
PreviousText    :

Upvotes: 0

attiq_khan
attiq_khan

Reputation: 11

Please change code under #display only sender name and message title as custom table

to:

$format= @{Expression={$_.title};Label="Title"},
         @{Expression={$_.author.name};Label="Author"}, 
         @{Expression={$_.summary};Label="Body"}

In XML file email body is saved under summary tag.

Upvotes: 1

030
030

Reputation: 11719

According to this documentation one could define format with the value full or raw:

Optional query parameters format string The format to return the message in.

Acceptable values are:

"full": Returns the full email message data with body content parsed in the 
payload field; the raw field is not used. (default)
"raw": Returns the full email message data with body content in the raw field 
as a base64url encoded string; the payload field is not used. 

Upvotes: 2

Related Questions