How to send email messages with 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     Example Robot 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.

  1. 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.
  2. In the *** Variables *** section, we are setting the required variables: the Gmail account username and password, and the recipient of the email message.
  3. In the *** Tasks *** section we add a new task Send test email, where we call the Authorize keyword passing our credentials, and then the Send 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     Example Robot 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     Example Robot 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}
  1. We are adding the @{ATTACHMENTS} list variable, filling it with the paths of our two files.
  2. We are passing the variable containing the list as the attachments arguments of the Send 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     Example Robot 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:

Email with inline images