Raspberry Pi Email Autoresponder using Python3

I needed an intelligent autoresponder that would only reply to a specific list of senders and not to any old spam as cPanel does. I toyed with the idea of writing something to run on my server but decided a more flexible solution would be to use a Raspberry Pi which was already doing other regular work for me. Whilst I now have four decades of professional programming experience, I hadn’t written any Python before. It turns out to be quite easy to use.

I created one Python script which brings in mail server credentials, the reply and the list of sending email addresses to look out for. The approach is to first check whether any new emails have already arrived today and to process those first. If not then enter a while loop that sits in idle mode waiting for the mailserver to alert to a new mail. New mail processing checks that the email is unseen and from today. It sets the email’s Flag and marks it as read; then it sends the autoreply to the sender. As connections with mailservers can be prone to timeouts, the program will recover from these and restart connections when needed.

The native IMAP libraries available to Python are at a very low level making programming difficult. Imap_Tools simplifies the programming but could be better documented. The native SMTP libraries on the other hand are pretty straightforward to use.

This runs quite nicely on a RPi Zero that was already doing other things. There is a slight spike in processer utilisation when it is processing an email but as it only needs the sender and subject and doesn’t download attachments etc it is lightweight with hardly any impact.

Prerequisits

Although Python is installed as part of the RPi OS build, we need to install the Python package installer PIP to install the imap_tools library

sudo apt install python3-pip

sudo pip3 install imap-tools

Program: Autoresponder.py

If you haven’t used Python before, be aware that it uses line indentation to identify and group blocks of code. You should be able to just copy and use the code below.

#! /usr/bin/python3
# Autoresponder - David Brewerton 2023
# ____________________________________

import configparser, os, sys
import time, socket, imaplib, traceback, datetime
from imap_tools import AND, OR, NOT, MailBox, MailboxLoginError, MailboxLogoutError, MailMessageFlags
from smtplib import SMTP_SSL, SMTP_SSL_PORT
from email.message import EmailMessage

def get_config_file_path():
    # use the configparser to bring in the mailserver credentials
    if "--help" in sys.argv or "-h" in sys.argv:
        display_help_text()
    if "--config-path" in sys.argv and len(sys.argv) >= 3:
        global config_file_path
        config_file_path = sys.argv[2]
    if not os.path.isfile(config_file_path):
        print("Configuration file not found. Expected it at '" + config_file_path + "'.")
        exit(-1)

def display_help_text():
    print("Options:")
    print("\t--help: Display this help information")
    print("\t--config-path <path/to/config/file>: "
          "Override path to config file (defaults to same directory as the script is)")
    exit(0)

def autoreply():
    # Assemble the reply email using email.message.EmailMessage
    from_email = myname + ' <' + username + '>'
    to_emails = msg.from_values.full
    email_message = EmailMessage()
    email_message.add_header('To', to_emails)
    email_message.add_header('From', from_email)
    email_message.add_header('Subject', replyprefix + '.. RE:' + msg.subject)
    email_message.add_header('X-Priority', '1')
    email_message.set_content(replybody)

    # Connect, authenticate, and send mail
    smtp_server = SMTP_SSL(mailserver, port=SMTP_SSL_PORT)
    smtp_server.set_debuglevel(0)  # Show SMTP server interactions = 1
    smtp_server.login(username, password)

    # print(time.strftime("%a %H:%M:%S"),'REPLY:', msg.uid, '"',from_email, ',', to_emails, ',', email_message.as_bytes())
    smtp_server.sendmail(from_email, to_emails, email_message.as_bytes())

    # Disconnect
    smtp_server.quit()


# *** RUN ROUTINE ***
# Initialise and read in config
config_file_path = "autoresponder.config.ini"
get_config_file_path()
# this is so the credentials config can be passed on the command line if required 
config_file = configparser.ConfigParser()
config_file.read(config_file_path, encoding="UTF-8")
myname = (config_file["login credentials"]["mailserver.myname"])
username = (config_file["login credentials"]["mailserver.username"])
password = (config_file["login credentials"]["mailserver.password"])
mailserver = (config_file["login credentials"]["mailserver.server"])
# this allows the non secure config to be passed through a fixed filename
config_file_path = "autoresponder.config.ini"
config_file = configparser.ConfigParser()
config_file.read(config_file_path, encoding="UTF-8")
replyprefix = (config_file["mail content settings"]["mail.reply.prefix"])
replybody = (config_file["mail content settings"]["mail.reply.body"])
fromlist = tuple((config_file["email addresses"]["addresses.fromlist"]).split("\n"))

# Main Loop 
done = False
while not done:
    connection_start_time = time.monotonic()
    connection_live_time = 0.0
    try:
        with MailBox(mailserver).login(username, password, 'INBOX') as mailbox:
            # after first connection or reconnection 
            print('@',end="") # prints @ to signify a new connection with the mailbox 
            while connection_live_time < 29 * 60: try: # deal with any unprocessed newmails before program start for today # do not mark them as read for msg in mailbox.fetch( AND(seen=False, date=datetime.date.today(), ), mark_seen=False ): # if newmail has a partial match with a string on the config fromlist # first set the mail to read and flagged # then send a reply if any(ele in msg.from_ for ele in fromlist): print("\n",time.strftime("%a %H:%M:%S"), "1>", msg.uid, '"', msg.from_values.full, ' "', msg.subject, '" ', msg.flags)
                              flags = (MailMessageFlags.SEEN, MailMessageFlags.FLAGGED)
                              mailbox.flag(mailbox.uids(
                                AND(seen=False,uid=msg.uid)
                                ), flags, True)
                              autoreply()
                    # now go idle until the mailserver wakes me up
                    responses = mailbox.idle.wait(timeout=600)
                    # print(time.strftime("%H:%M:%S"), 'IDLE responses:', responses)
                    if responses:
                        # mailserver has woken up and alerted us to newmail to process
                        # do not mark them as read
                        for msg in mailbox.fetch(
                            AND(seen=False,
                                date=datetime.date.today(),
                                    ),
                                mark_seen=False 
                                ):
                            # if newmail has a partial match with a string on the config fromlist 
                            # first set the mail to read and flagged
                            # then send a reply
                            if any(ele in msg.from_ for ele in fromlist):
                              print("\n",time.strftime("%a %H:%M:%S"), "=>", msg.uid, '"', msg.from_values.full, ' "', msg.subject, '" ', msg.flags)
                              flags = (MailMessageFlags.SEEN, MailMessageFlags.FLAGGED)
                              mailbox.flag(mailbox.uids(
                                AND(seen=False,uid=msg.uid)
                                ), flags, True)
                              autoreply()
                # stop with ctrl z 
                except KeyboardInterrupt:
                  print('~KeyboardInterrupt')
                  done = True
                  break
                connection_live_time = time.monotonic() - connection_start_time
    # deals primarily with timeouts on the mailserver which happen frequently  
    # so just dust yourself down and reconnect again
    except (TimeoutError, ConnectionError,
            imaplib.IMAP4.abort, MailboxLoginError, MailboxLogoutError,
            socket.error, socket.gaierror, socket.timeout) as e:
        # print(f'{e} reconnecting...')
        time.sleep(15)

Configuration file: autoresponder.config.ini

The configuration file is parsed using ConfigParser and in its simplest form is just a plain text file as below. It is possible to use modules to encrypt the password but I haven’t used that approach here.

[login credentials]
mailserver.username = your-email-address@thingy.com
mailserver.password = your-email-password
mailserver.server = your-IMAP-and-SMTP-server-url
mailserver.myname = Your Normal Name

[mail content settings]
mail.reply.prefix = Autoreply
mail.reply.body =
 Hello

 Thank you for your email. This is an automated reply to let you know why I can not reply properly at the moment.
 
 Any multi-line values in this config file need to be indented 
 
 Kind regards
 
 Me

[email addresses]
addresses.fromlist = smithy
 jones
 any-part-of-an-email-address-goes-in-this-list-but-new-lines-must-be-indented
 fred

Running the program

There are many ways of running this program of course. I assumed that the autoresponder would be used on a temporary basis so I opted not to put it in CRON as a scheduled job or to be in the start up routines. I decided to run it as a background job from the command line and pipe any output to a file which I’ve called log.txt
Note: that in the command below, -u means that output is unbuffered so is immediately written to the log file. 1> and 2>&1 are used to direct both stdout and stderr to the log file.  The trailing ampersand indicates that this job is to be run in background.

python3 -u autoresponder.py 1>log.txt 2>&1 &

Make a note of the process-id that this gives you because you may later want to stop the program using KILL {process-id}. If you forget to do this you can find the process-id using the command PS for processes run in the same session or PS -u {pi} for all your user’s processes.

Supplying the credentials more covertly

If you want to start the program supplying a separate configuration file for the credentials via the command line then you can split the config into two files. Provide the email addresses and auto reply in the standard autoresponder.config.ini and provide the credentials config file through the command line such as aardvark.ini as follows:-

python3 -u autoresponder.py --config-path aardvark.ini 1>log.txt 2>&1 &

Program output

The program reports when it has processed a reply by outputting to the screen (or log.txt if in background). The message-id, sender, “subject title” and (flags) are reported. eg.

Mon 14:01:58 => 14996 " John Smith <blahblah@gmail.com> " Info that we need to discuss "  ()

It also outputs a single “@” each time it has to reconnect to the mailserver.

Version 3

I realised a major issue was that running the autoresponder in this way would be tripped up by reboots and so it needed to be triggered by CRON rather than waiting for the server to respond. I also wanted to add a start and expiry date in configuration so I didn’t need to specifically switch it on and off in real time. Consequently I streamlined the code and added another two parameters, mail.reply.untilend and mail.reply.startfrom, to the config.ini file which takes a date of format DD/MM/YY.
Important: ensure you only have a carriage return/new line at the end of the date otherwise you may get a KeyError(key) error message.

[mail content settings]
mail.reply.fromstart = 21/12/23
mail.reply.untilend = 02/01/24
mail.reply.prefix = Autoreply
mail.reply.body =
 Hello

 Thank you for your email. I am on holiday 21st Dec to 3rd Jan inclusive although I will work a few hours for a couple of days between Boxing Day and New Year to deal with any pressing issues.
 

The new program, autoresponder3.py is:-

 # Autoresponder - David Brewerton 2023
# v 20231217
# ____________________________________

import configparser, os, sys
import time, socket, imaplib, traceback
import datetime
from imap_tools import AND, OR, NOT, MailBox, MailboxLoginError, MailboxLogoutError, MailMessageFlags
from smtplib import SMTP_SSL, SMTP_SSL_PORT
from email.message import EmailMessage

def get_config_file_path():
    # use the configparser to bring in the mailserver credentials
    if "--help" in sys.argv or "-h" in sys.argv:
        display_help_text()
    if "--config-path" in sys.argv and len(sys.argv) >= 3:
        global config_file_path
        config_file_path = sys.argv[2]
    if not os.path.isfile(config_file_path):
        print("Configuration file not found. Expected it at '" + config_file_path + "'.")
        exit(-1)

def display_help_text():
    print("Options:")
    print("\t--help: Display this help information")
    print("\t--config-path <path/to/config/file>: "
          "Override path to config file (defaults to same directory as the script is)")
    exit(0)

def autoreply():
    # Assemble the reply email using email.message.EmailMessage
    from_email = myname + ' <' + username + '>'
    to_emails = msg.from_values.full
    email_message = EmailMessage()
    email_message.add_header('To', to_emails)
    email_message.add_header('From', from_email)
    email_message.add_header('Subject', replyprefix + '.. RE:' + msg.subject)
    email_message.add_header('X-Priority', '1')
    email_message.set_content(replybody)

    # Connect, authenticate, and send mail
    smtp_server = SMTP_SSL(mailserver, port=SMTP_SSL_PORT)
    smtp_server.set_debuglevel(0)  # Show SMTP server interactions = 1
    smtp_server.login(username, password)

    # print(time.strftime("%a %H:%M:%S"),'REPLY:', msg.uid, '"',from_email, ',', to_emails, ',', email_message.as_bytes())
    smtp_server.sendmail(from_email, to_emails, email_message.as_bytes())

    # Disconnect
    smtp_server.quit()


# *** RUN ROUTINE ***
# Initialise and read in config
config_file_path = "autoresponder.config.ini"
get_config_file_path()
# this is so the credentials config can be passed on the command line if required 
config_file = configparser.ConfigParser()
config_file.read(config_file_path, encoding="UTF-8")
myname = (config_file["login credentials"]["mailserver.myname"])
username = (config_file["login credentials"]["mailserver.username"])
password = (config_file["login credentials"]["mailserver.password"])
mailserver = (config_file["login credentials"]["mailserver.server"])
# this allows the non secure config to be passed through a fixed filename
config_file_path = "autoresponder.config.ini"
config_file = configparser.ConfigParser()
config_file.read(config_file_path, encoding="UTF-8")
autoreply_active = ((config_file["mail content settings"]["mail.reply.on"]).lower())

autoreply_fromstart = datetime.datetime.strptime((config_file["mail content settings"]["mail.reply.fromstart"]),"%d/%m/%y")
fromstart = autoreply_fromstart.strftime("%Y%j")
autoreply_untilend = datetime.datetime.strptime((config_file["mail content settings"]["mail.reply.untilend"]),"%d/%m/%y")
untilend = autoreply_untilend.strftime("%Y%j")
currday = datetime.datetime.now().strftime("%Y%j")

replyprefix = (config_file["mail content settings"]["mail.reply.prefix"])
replybody = (config_file["mail content settings"]["mail.reply.body"])
fromlist = tuple((config_file["email addresses"]["addresses.fromlist"]).split("\n"))

# Main
if ((currday > untilend) | (currday < fromstart)):
   print("Autoresponder3 is inactive") # today is after end OR today is before start
   sys.exit(0)
# else run
print(time.strftime("%a %H:%M:%S"),"Autoresponder3..", end="")
try:
    with MailBox(mailserver).login(username, password, 'INBOX') as mailbox:
        # after first connection or reconnection 
        print(time.strftime("(%S)"), "connected")
        # deal with any unprocessed newmails for today
        # do not mark them as read
        for msg in mailbox.fetch(
            AND(seen=False,
            date=datetime.date.today(),
            ),
            mark_seen=False 
            ):
            # if newmail has a partial match with a string on the config fromlist 
            # first set the mail to read and flagged
            # then send a reply
            if any(ele in msg.from_ for ele in fromlist):
                print("\n",time.strftime("%a %H:%M:%S"), ">", msg.uid, '"', msg.from_values.full, ' "', msg.subject, '" ', msg.flags,end="")
                flags = (MailMessageFlags.SEEN, MailMessageFlags.FLAGGED)
                mailbox.flag(mailbox.uids(
                    AND(seen=False,uid=msg.uid)
                    ), flags, True)
                autoreply()
                print(".. replied")
        print(time.strftime("(%S)"), "finished")
# deals primarily with timeouts on the mailserver which happen frequently  
# so just dust yourself down and reconnect again
except (TimeoutError, ConnectionError,
    imaplib.IMAP4.abort, MailboxLoginError, MailboxLogoutError,
    socket.error, socket.gaierror, socket.timeout) as e:
    print(f'{e} ')

CRON can be configured to run every five minutes. It will read the normal autoresponder.config.ini and optionally also read your credentials file with the –config option as:-

*/5 * * * * /usr/bin/python3 -u /home/pi/autoresponder3.py --config /home/pi/your-credentials-file