E-mail¶
Filter, search, read and write e-mails using IMAP and SMTP protocols.
Installation¶
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
SMTP and IMAP Permissions
In some cases, in order to be able to use the email functionalities through the IMAP and SMTP protocols, it is necessary to activate the permission to use both protocols in the account settings. In addition, it may also be necessary to enable the option for less secure applications.
Two-Factor Authentication
We recommend disabling two-factor authentication for email accounts to avoid any type of bot blocking. If you are using an account with two-factor authentication and it is not possible to disable it, see the Two-Factor Authentication section.
Setting up email account¶
First, let's configure our email account and login with the credentials.
Info
You can use different mail servers, the information about some servers can be seen on this page.
Alternative setup¶
You have the option to configure the servers that will be used the way you want. However, in some cases you can simply use the default settings of a known server and connect your account quickly.
The configuration above could also look like this:
Fetching some email messages¶
With the configured plugin instance, we will retrieve some email messages.
Note
Please, avoid using words that contain accents or special characters when using the methods to filter and search for messages. This is a issue from the email servers, causing an exception when trying to search for emails using special characters in the filter.
# Search for all emails with subject: Test Message
messages = email.search('SUBJECT "Test Message"')
# For each email found: prints the date, sender address and text content of the email
for msg in messages:
print("\n---------------------------")
print("Date => " + msg.date_str)
print("From => " + msg.from_)
print("Msg => " + msg.text)
Info
In this example, the SUBJECT attribute was used as a filter, you can have an idea of the attributes that can be used in this link.
See more details on how to create advanced filters in this section.
Another way for fetching some email messages¶
In the search method used above it is necessary to pass a string containing the filter that will be used. However, in some cases it can be confusing and it is difficult to build the filter correctly.
Therefore, through the filter by method it is possible to select the attribute that will be used in the filter and pass the value of this attribute. Making it much easier to build filters when retrieving messages.
Sending a new message from scratch¶
After retrieving some emails, we will send a new message.
# Defining the attributes that will compose the message
to = ["<RECEIVER_ADDRESS_1>", "<RECEIVER_ADDRESS_2>"]
subject = "Hello World"
body = "<h1>Hello!</h1> This is a test message!"
files = ["my_file.txt"]
# Sending the email message
email.send_message(subject, body, to, attachments=files, use_html=True)
# Close the conection with the IMAP and SMTP servers
email.disconnect()
Complete code¶
Let's take a look into the complete code:
# Import the plugin
from botcity.plugins.email import BotEmailPlugin
# Instantiate the plugin
email = BotEmailPlugin()
# Configure IMAP with the gmail server
email.configure_imap("imap.gmail.com", 993)
# Configure SMTP with the gmail server
email.configure_smtp("imap.gmail.com", 587)
# Login with a valid email account
email.login("<USER_EMAIL>", "<USER_PASSWORD>")
# Search for all emails with subject: Test Message
messages = email.search('SUBJECT "Test Message"')
# For each email found: prints the date, sender address and text content of the email
for msg in messages:
print("\n---------------------------")
print("Date => " + msg.date_str)
print("From => " + msg.from_)
print("Msg => " + msg.text)
# Defining the attributes that will compose the message
to = ["<RECEIVER_ADDRESS_1>", "<RECEIVER_ADDRESS_2>"]
subject = "Hello World"
body = "<h1>Hello!</h1> This is a test message!"
files = ["my_file.txt"]
# Sending the email message
email.send_message(subject, body, to, attachments=files, use_html=True)
# Close the conection with the IMAP and SMTP servers
email.disconnect()