How to work with email using Gmail and RPA Framework
Sending email messages is one of the most common requirements when automating processes: it could be a report about the results of the process or maybe part of the automation itself, like emailing invoices as attachments.
The RPA Framework adds libraries that make handling email easy and convenient. This article will show you how to use the RPA.Email.ImapSmtp
library to send email messages from a Gmail account.
The library is meant to work with any SMTP server, not just Gmail. It can also read and manage email messages in any email account accessible via IMAP.
Configuration of the Gmail account
If the Google account you use for sending has Two Factor Authentication enabled, you will need to set up an App password and use that password here (YOUR_GMAIL_PASSWORD
).
Depending on the configuration of your Gmail account, or if your account is part of Google Suite, you might need to enable the "Less secure app access" setting.
Sending plain text email messages
Here is an example robot that sends a plain text message:
*** Settings ***
Documentation Sending plain text email via Gmail.
Library RPA.Email.ImapSmtp smtp_server=smtp.gmail.com smtp_port=587
*** Variables ***
${USERNAME} YOUR_GMAIL_ACCOUNT
${PASSWORD} YOUR_GMAIL_PASSWORD
${RECIPIENT} [email protected]
*** Tasks ***
Send test email
Authorize account=${USERNAME} password=${PASSWORD}
Send Message sender=${USERNAME}
... recipients=${RECIPIENT}
... subject=Message generated by robot
... body=This is the body of the email.
- We are adding the
RPA.Email.ImapSmtp
library at the top in the*** Settings ***
section, passing the server name for Gmail and the SMTP port to use as configuration. The port can be omitted when using the default value (in this case,587
). It is used here to display that it can be configured if needed. - In the
*** Variables ***
section, we are setting the required variables: the Gmail account username and password, and the recipient of the email message. - In the
*** Tasks ***
section we add a new taskSend test email
, where we call theAuthorize
keyword passing our credentials, and then theSend Message
keyword, specifying the sender, recipient, subject and body of the message.
Note: It is not a good practice to write credentials in your robot code. We recommend to use Robocorp Cloud vault to store your secrets securely.
Sending email messages with attachments
You can send files as attachments to a message.
To send a file called results.csv
residing in the same directory where you run the robot, the code becomes:
*** Settings ***
Documentation Sending email via Gmail with a file attachment.
Library RPA.Email.ImapSmtp smtp_server=smtp.gmail.com smtp_port=587
*** Variables ***
${USERNAME} YOUR_GMAIL_ACCOUNT_NAME
${PASSWORD} YOUR_GMAIL_ACCOUNT_PASSWORD
${RECIPIENT} RECIPIENT_OF_THE_EMAIL
*** Tasks ***
Send test email
Authorize account=${USERNAME} password=${PASSWORD}
Send Message sender=${USERNAME}
... recipients=${RECIPIENT}
... subject=Message generated by robot
... body=This is the body of the email.
... attachments=results.csv
All we needed to do was adding the line ... attachments=results.csv
to the Send Message
keyword.
Sending an email with multiple attachments
If you want to send an email message with multiple attachments, you have to pass a list
of file paths as the attachments
arguments of the Send Message
keyword instead of a simple string.
If you have two files results1.csv
and results2.csv
in the same directory where you run the robot, the example code becomes:
*** Settings ***
Documentation Sending email via Gmail with multiple file attachments.
Library RPA.Email.ImapSmtp smtp_server=smtp.gmail.com smtp_port=587
*** Variables ***
${USERNAME} YOUR_GMAIL_ACCOUNT_NAME
${PASSWORD} YOUR_GMAIL_ACCOUNT_PASSWORD
${RECIPIENT} RECIPIENT_OF_THE_EMAIL
@{ATTACHMENTS} results1.csv results2.csv
*** Tasks ***
Send test email
Authorize account=${USERNAME} password=${PASSWORD}
Send Message sender=${USERNAME}
... recipients=${RECIPIENT}
... subject=Message generated by robot
... body=This is the body of the email.
... [email protected]{ATTACHMENTS}
- We are adding the
@{ATTACHMENTS}
list variable, filling it with the paths of our two files. - We are passing the variable containing the list as the
attachments
arguments of theSend Message
keyword.
To create and manage lists inside your keywords, check out the
Collections
library, part of the Built-in libraries provided by Robot Framework.
Sending HTML email messages with inline images
You can send images in the body of the email message using HTML.
To embed an image file called image.png
residing in the same directory where you run the robot, the code becomes:
*** Settings ***
Documentation Sending email via Gmail with an inline image.
Library RPA.Email.ImapSmtp smtp_server=smtp.gmail.com smtp_port=587
*** Variables ***
${USERNAME} YOUR_GMAIL_ACCOUNT_NAME
${PASSWORD} YOUR_GMAIL_ACCOUNT_PASSWORD
${RECIPIENT} RECIPIENT_OF_THE_EMAIL
*** Tasks ***
Send test email
Authorize account=${USERNAME} password=${PASSWORD}
Send Message sender=${USERNAME}
... recipients=${RECIPIENT}
... subject=Message generated by robot
... html=True
... images=image.png
... body=<p>See this image: <img src='image.png' alt='approved image'/></p>
In this case, we need to modify the Send Message
keyword:
- adding
html=True
to tell the SMTP server that we are sending an email in HTML format - adding the names (and path) of the images that we will use in the email body using the
images
parameter (images=image.png
) - now we can use the image in the body of the message, using HTML formatting:
body=<p>See this image: <img src='image.png' alt='approved image'/></p>
This is how the generated message will look like in an email client:
Sending multiple attachments and saving them
This robot sends multiple attachments, collects the messages by criterion (email subject) using the List Messages
keyword, logs some information regarding each email, and saves the attachments using the Save Attachment
keyword. It also uses a vault for the credentials.
*** Settings ***
Documentation Sending email via Gmail with multiple file attachments,
... and saving the attachments.
Library RPA.Email.ImapSmtp
... smtp_server=smtp.gmail.com
... smtp_port=587
Library RPA.Robocloud.Secrets
*** Variables ***
${BODY}= This is the body of the email.
${RECIPIENT}= [email protected]
${SENDER}= [email protected]
${SUBJECT}= Message generated by robot
@{ATTACHMENTS}=
... ${CURDIR}${/}devdata${/}attachment1.csv
... ${CURDIR}${/}devdata${/}attachment2.csv
*** Keywords ***
Send Email With Attachments
${secret}= Get Secret emailCredentials
Authorize account=${secret}[username] password=${secret}[password]
Send Message
... sender=${SENDER}
... recipients=${RECIPIENT}
... subject=${SUBJECT}
... body=${BODY}
... [email protected]{ATTACHMENTS}
*** Keywords ***
Get Messages Where Subject Contains
[Arguments] ${subject}
@{emails} List Messages SUBJECT "${subject}"
FOR ${email} IN @{emails}
Log ${email}[Subject]
Log ${email}[From]
Log ${email}[Date]
Log ${email}[Received]
Log ${email}[Has-Attachments]
END
[Return] @{emails}
*** Keywords ***
Download Attachments
[Arguments] @{emails}
FOR ${email} IN @{emails}
Run Keyword If ${email}[Has-Attachments] == True
... Save Attachment ${email} target_folder=${CURDIR}${/}output overwrite=True
END
*** Tasks ***
Send email with attachments, list messages, and download the attachments
Send Email With Attachments
@{emails}= Get Messages Where Subject Contains ${SUBJECT}
Download Attachments @{emails}
Listing email messages by criterion
The above example robot uses the List Messages
keyword to list messages matching the given criterion. Learn more about the search syntax in the RFC 3501, IMAP4rev1 Protocol Specification. Here's a IMAP Search criteria cheatsheet that might be easier to read :-).