Read Email

Reads an email message.

Command availability: IBM RPA SaaS and IBM RPA on premises

Description

Reads an email message and gets all its content, such as Subject, Email Body, From, To, Cc, Bcc, Received Date, Header and Categories, and stores them separately in their respective variables.

Limitations

This command doesn't mark the email as read, it only extracts information from the email.

Script syntax

IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

emailRead --message(EmailMessage) (String)=subject (String)=body (String)=htmlbody (String)=from (List<String>)=to (List<String>)=cc (List<String>)=bcc (DateTime)=receiveddate (StringDictionary<String>)=headers (List<String>)=labels

Dependencies

Input parameters

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
Message message Required Email Message Variable of type Email Message that contains the email to be read.

Output parameters

Designer mode label Script mode name Accepted variable types Description
Subject subject Text Returns the subject of the email message.
Email Body body Text Returns the content of the body of the email message, according to plain text formatting.
HTML Email Body htmlbody Text Returns the content of the body of the email message, according to HTML formatting.
From from Text Returns the email from the message sender.
To to List<Text> Returns a list with the message recipient's email.
Cc cc List<Text> Returns a list with the email addresses of the recipients who received a copy of the message.
Bcc bcc List<Text> Returns a list with the email addresses of the recipients who received a blind copy of the message.
Received Date receiveddate Date Time Returns the date and time of the email message.
Important:Starting at version 23.0.15, the received date is now in UTC. You must convert the date to your timezone.
Headers headers String Dictionary<Text> Key and value referring to information about the message, such as: sender, recipient and server, or any other information that is attached to the header.
Categories labels List<Text> Returns a list with the classification category of the email message.

Example

The command performs the reading of an email message, resulting in the retrieval and storage of its content in its respective variables.

defVar --name readMessage --type EmailMessage
defVar --name htmlEmailBody --type String
defVar --name emailSubject --type String
defVar --name emailBody --type String
defVar --name emailSender --type String
defVar --name emailDestinatary --type List --innertype String
defVar --name emailCopy --type List --innertype String
defVar --name emailHiddenCopy --type List --innertype String
defVar --name emailReceiveDate --type DateTime
defVar --name emailHeader --type "StringDictionary<TValue>" --innertype String
defVar --name emailCategory --type List --innertype String
defVar --name emailConnection --type EmailConnection
imapConnect --mailhost "outlook.office365.com" --mailport 993 --usessl--username "useremail@email.com" --mailusername "useremail@email.com" --mailpassword userpassword --enableLogemailConnection=value
// Using the "For Each" command allows each emails message contained in the connection established with the emails server to be read.
foreach --collection "${emailConnection}" --variable "${readMessage}"
emailRead --message ${readMessage} emailSubject=subject emailBody=body htmlEmailBody=htmlbody emailSender=from emailDestinatary=to emailCopy=cc emailHiddenCopy=bcc emailReceiveDate=receiveddate emailHeader=headers emailCategory=labels
endFor
emailDisconnect --connection ${emailConnection}
logMessage --message "${emailSubject}\r\n${emailBody}\r\n${emailCopy}\r\n${emailHiddenCopy}\r\n${emailReceiveDate}\r\n${emailSender}\r\n${emailDestinatary}\r\n${emailHeader}\r\n${emailCategory}" --type "Info"
// Each field obtained is displayed in its respective variable. Example: Subject, emails Body, Sender, Recipient, etc.
Note:Use a valid email connection to run this example script.