Get notified via email#

This example shows how to set-up an emailbot.

First, you need to setup an email account.

Warning

Create a brand new account!

It is STRONGLY advised to create a new email account (must be GMAIL)!! You will have to save the login credentials on your system, which is a huge security issue!

Note

Allow ‘less secure app access’

in order for the bot to be able to send messages, you have to turn on the less secure app acces option in the gmail security settings.

Now you have to register your credentials. You can simply do it from the command line (obviously change the placeholders with your credentials):

$ emailbot set-token "username@gmail.com" "PaSsWoRd!"

Alternatively, manually create a secret json file somewhere hidden on your system with the username and the password. Then run the following command:

$ codebots set-tokens-path "path=to-the-folder-where-the-json-file-is-saved"

The json file should look like this:

{
    "username": "username@gmail.com"
    "password": "PaSsWoRd!"
}

Here an example on how to receive an email from the bot:

from codebots.bots import EmailBot

# Crete the bot: not it can send emails on behalf of the Sender account
bot = EmailBot()

# # If you want send an email from another account, initiate a Sender
# # with some credentials and feed it to the emailbot:
# # - you can specify the location of the access config file ".tokens/email" (keep this secret!)
# # - or you can pass username and password on the fly (not a great idea...)
# from codebots.bots import Sender
# sender = Sender.form_file(".tokens/email")
# bot = EmailBot(sender)

# set the email content
receiver = "receiver@email.com"
subject = "message from bot"
body = "This message was sent by a bot"

# Ask the bot to compose your email and send it
bot.send_email(receiver, subject, body)