1. Home
  2. Sample Projects
  3. Reading Email Inbox

Reading Email Inbox

◷ Reading Time: 8 minutes

The example shows how to use the MailBox extension to read emails from a given email account.

Scenario

A company is hiring a new software developer. In their job post, they have asked the applicants to email the resumes with the subject including the position name. (E.g. When applying for the software development position, the subject should contain ‘software developer’).

How to Run

1. Open the file ‘Flow.xml’.

2. Click on Debug with JSON composer.

3. Enter position, email address, and password.

4. Click OK

5. Click Next Step to go step by step.

Once the execution is completed successfully, the folders and files can be seen inside the folder ‘Applicants’

Folders were created inside ‘Applicants’ folder

position > email address > sent date

Attachments were downloaded and the email content is saved in ‘Email Content.html’ file.

If there is no matching email, it will show a notification in the Notifications window

Project Description

Flow was created to filter the emails according to the subject. The position, email address, and password should be provided as input. It will then look for the emails with the subject containing the position. After reading each email, the email content and attachments are saved under the folder ‘Applicants’ creating folders for each applicant and the email sent date.

Video Tutorial

Process Steps

1. Crete a flow file.

2. Define variables.

NameDirectionTypeDescription
positionInstringPosition of the candidates to filter
emailAddressInstringEmail address of the account to log in
passwordInstringPassword of the account to log in
mailLocalMail server variable
messagesLocalStore messages after filtering
iLocalintTemporary item holder for each occurrence in the ‘Read Emails’ loop
jLocalintTemporary item holder for each occurrence in the ‘Attachments’ loop
emailAddressFolderLocationLocalstringStore email address folder location to create folders for each applicant
sentDateFolderLocalstringStore sent date to create folders for each applicant’s application sent date
emailAddressFolderLocalstringStore email address of each applicant
fileLocationLocalstringStore the file location name to save the application data

3. Add a Start node.

4. Add an Activity node to connect to the mail server ( Gmail Incoming Mail (IMAP) Server: ‘imap.gmail.com’ and the port is ‘993’)

mail =mailBox ('imap.gmail.com:993',
                 {user:'emailAddress', password:'password'}
              ) 

5. Add an Activity node to query emails from the inbox.

messages = mail |mailQuery (
                 field subject contains(position)
              )  

6. Add a Decision node. If emails were found, proceed. If not, show a Notification and terminate the flow.

Found Condition: 'messages|length()>0'
Notification Message: No emails found with a subject containing '{position}'

7. Create a folder for the position.

pathCombine(pathCurrent(),'\\'+'Applicants'+'\\'+ position)

8. Add a loop to read each email as all the matching emails are stored in the array ‘messages’.

List Source: 'messages'
Item Name: 'i'
Initializer: Mark as seen node
Finalizer: Attachments loop

Add the following nodes inside the loop.

9. Add an Activity node to mark the email as seen.

i.MarkSeen(true)

10. Define folder names and locations in a Multiple Expression node

emailAddressFolder=i.From.Email
sentDateFolder=(((i.SentDate.DateTime)|asString()) |replace('/',))|replace(':','.')
emailAddressFolderLocation='\\'+'Applicants'+'\\'+position+'\\'+emailAddressFolder
fileLocation=emailAddressFolderLocation+'\\'+sentDateFolder+'\\'

11. Create a folder for the email address

pathCombine(pathCurrent(),emailAddressFolderLocation)

12. Create a folder for the sent date

pathCombine(pathCurrent(),fileLocation)

13. Add a Write File node to write email content to the file ‘Email Content.html’

Path: 'pathCombine(pathCurrent(),fileLocation +'Email Content.html')'
Content: 'i.Message.HtmlBody'

14. Add another loop to read attachments as there can be multiple attachments per email.

List Source: 'i.Attachments'
Item Name: 'j'
Initializer: Download attachments node
Finalizer: Download attachments node

15. Add a Write File node to download attachments inside the ‘Attachments’ loop.

Path: 'pathCombine(pathCurrent(),fileLocation+j.Filename)'
Content: 'j.Content'

16. Add an End node to end the flow.

Download the project

The project can be downloaded from the attachment at the end of the page.

Updated on April 29, 2022

Article Attachments

Was this article helpful?

Related Articles