Simple python script that connects to a mail server via IMAP and SMTP and replies to all emails in the inbox coming from a certain sender address using the Reply-To header.

sunborn23 sunborn23 Last update: Aug 26, 2022

Python E-Mail Autoresponder

Simple python script that connects to a mail server via IMAP and SMTP and replies toall emails in the inbox coming from a certain sender address using the Reply-To header.Mails that have been replied to are deleted afterwards.

Dependencies

This script runs on python3 and is written for the purpose of running as a cronjob.

To run it you'll only need python3.

Installation

To install, simply download a copy of the project (see screenshot below) and extract it to whatever folder you like.

Github Archive Download

Alternatively, on UNIX systems you can run

wget https://github.com/sunborn23/python-email-autoresponder/archive/master.zip && unzip master.zip

to download and then extract all project files.

Configuration

Before first run, you need to adapt the project to your needs by editing the autoresponder.config.ini file with a text editor.

The required configuration items for the individual sections are listed below.

Section [login credentials]

Configuration ItemDescription
mailserver.incoming.usernameThe username to use for logging in at the IMAP server hosting the inbox, usually an email address.
mailserver.incoming.passwordThe password to use for logging in at the IMAP server hosting the inbox.
mailserver.outgoing.usernameThe username to use for logging in at the SMTP server for sending the reply, usually an email address.
mailserver.outgoing.passwordThe password to use for logging in at the SMTP server for sending the reply.
mailserver.outgoing.display.nameThe name to use in the email's "From" field indicating where the reply email is from.
mailserver.outgoing.display.mailThe email address to use in the email's "From" field, should normally be a no-reply address.

Section [mail server settings]

Configuration ItemDescription
mailserver.incoming.imap.hostThe hostname, domain or IP of the IMAP server hosting the inbox.
mailserver.incoming.imap.port.sslThe port to use for SSL communication with the IMAP server.
mailserver.outgoing.smtp.hostThe hostname, domain or IP of the SMTP server for sending the reply.
mailserver.outgoing.smtp.port.tlsThe port to use for TLS communication with the SMTP server.
mailserver.folders.inbox.nameThe name of the inbox folder, normally this is "Inbox".

Section [mail content settings]

Configuration ItemDescription
mail.request.fromThe sender email address to check new mails against. If an email is found in the inbox with this sender address, a reply is triggered.
mail.reply.subjectThe subject line of the reply email.
mail.reply.bodyThe plain text body of the reply email.

After configuring the project, you can run it manually to test if your configuration works.

Manual Usage

For testing purposes you can run the script from the shell. To do so, navigate to the project directory and run

python3 run_autoresponder.py

If you want to run the script multiple times with different configurations on each executions,you can achieve that by running

python3 run_autoresponder.py --config-path /the/path/to/your/config/file/autoresponder.config.ini

Usage as a cronjob

For production use you can configure it as a cronjob.

From the shell, run:

crontab -e

Then append to the file (replace "/the/path/to/the/project/folder" by the actual path to these files):

*/1 * * * * python3 /the/path/to/the/project/folder/run_autoresponder.py

This will run the script every minute.

You can use any OnlineCronExpressionGenerator for generating other cron expressions.

For info on how to craft the cron expression yourself, run man crontab.

Subscribe to our newsletter