> ## Content Index
> Fetch the complete content index at: https://www.bagz.fr/llms.txt
> Use this file to discover other available public pages before exploring further.

# Using procmail with custom python script
- URL: https://www.bagz.fr/using-procmail-with-custom-python-script/
- Published: 2018-04-01T16:30:57.000Z
- Updated: 2018-04-01T16:30:57.000Z
- Author: bgazui
- Tags: Linux, Python, #Migrated-1790668826633, #wp, #wp-post, #Import 2026-09-29 08:00

If you want to execute some special commands, log or make an API call when you're receiving an email onto your server, you can easily set up this by using procmail. Procmail is designed to filter and sort emails but can make any call you want.

Here, as an example, we will set up a simple call to a python script that will read the content of the mail (headers and body) and put the information into a log file.

### 1\. Install procmail

Depending on the OS you're using, you should find a package pre-compiled in the common repositories.

For example, on a Debian-based:

apt-get install procmail  

or on a CentOS-based:

yum install procmail  

### 2\. Build the python script you want to call to analyze the message

You now have to prepare your script you will execute when receiving an email, that will read and parse the content to log interesting information in a file.

Let's create a script called **procmail\_script.py**

import os  
import time  
import email  
import sys  
import logging.handlers  
import base64  
  
\# Constants  
LOG\_FILE\_PATH = os.path.expanduser('/opt/mailAnalysis.log')  
  
\# Set up a logger  
my\_logger = logging.getLogger('MyLogger')  
my\_logger.setLevel(logging.INFO)  
handler = logging.handlers.RotatingFileHandler(LOG\_FILE\_PATH, maxBytes=500000, backupCount=4,)  
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s", "%Y-%m-%d %H:%M:%S")  
handler.setFormatter(formatter)  
my\_logger.addHandler(handler)  
  
\# Main function  
def main():  
 try:  
 \# Get message  
 full\_msg = sys.stdin.read()  
 msg = email.message\_from\_string(full\_msg)  
  
 \# Prepare dict containing data  
 data = {}  
  
 \# Fill dict   
 data\['From'\],e = email.Header.decode\_header(msg\['From'\])\[0\] if msg.has\_key('From') else ''  
 data\['To'\],e = email.Header.decode\_header(msg\['To'\])\[0\] if msg.has\_key('To') else ''  
 data\['Subject'\],e = email.Header.decode\_header(msg\['Subject'\])\[0\] if msg.has\_key('Subject') else ''  
 data\['Body'\] = msg.get\_payload()  
  
 \# Add information to log  
 my\_logger.info('From: ' + data\['From'\])  
 my\_logger.info('To: ' + data\['To'\])  
 my\_logger.info('Subject: ' + data\['Subject'\])  
 my\_logger.info('Body: ' + data\['Body'\])  
 except Exception,e:  
 my\_logger.error('----- ERROR ENCOUNTERED')  
 my\_logger.error(str(e))  
 my\_logger.error('----- END OF ERROR')  
  
\# Main program  
if \_\_name\_\_ == "\_\_main\_\_":  
 start\_time = time.time()  
 my\_logger.info('----- START: ' + str(time.time()))  
 result = main()  
 my\_logger.info('----- END: ' + str(time.time()))  
 my\_logger.info('Elapsed Seconds: ' + str(time.time() - start\_time))  
 handler.close()  
 sys.exit(result)  
  
### 3\. Configure your user to pass the mail to your script

So that you can pass rules you want to execute when receiving an email, you need to create a file (hidden) called **.procmailrc** that will take place in the home directory of the user you want to use.

For example, for executing rules when receiving email to *mailuser@mycompany.com*, you will have to put that file into the home dir like **/home/mailuser/.procmailrc**

LOGFILE=/var/log/procmail.log  
VERBOSE=YES  
  
:0:  
\* ^Subject:.\*procmail.\*  
\* ^TO\_mailuser@mycompany.(com|net)  
{  
 :0c  
 procmail-dir/  
  
 :0 fw  
 | /usr/bin/python /home/mailuser/procmail\_script.py  
  
 :0 e  
 procmail-failed-dir/  
}

This will perform multiple steps:

1. Check that the mail recipient is **mailuser@mycompany.com** or **mailuser@mycompany.net**
2. Put a copy of the email into the **procmail-dir** directory
3. Pass the message to our python script **procmail\_script.py**
4. Discard the message if the script succeeds (remove from queue) or copy it to **procmail-failed-dir** if failed (so you can process it later)

### 4\. Prepare an email and perform a testing locally

First, create a sample mail that you will use for testing in a file called *procmail\_test.txt*:From: me@mycompany.com  
To: mailuser@mycompany.com  
Subject: This is a procmail testing  

Hey there,  
  
I hope this message will be parsed and logged properly as expected.  
  
This is my first test for procmail deployment!  
  
Then, you can test it by executing procmail manually:

procmail VERBOSE=on /home/mailuser/.procmailrc < /home/mailuser/procmail\_test.txt  

Now, open the file **/opt/mailAnalysis.log** and you should have something like:

2018-03-31 08:08:45 - INFO - ----- START: 1522570125.06  
2018-03-31 08:08:45 - INFO - From: me@mycompany.com  
2018-03-31 08:08:45 - INFO - To: mailuser@mycompany.com  
2018-03-31 08:08:45 - INFO - Subject: This is a procmail testing  
2018-03-31 08:08:45 - INFO - Body: Hey there,  
I hope this message will be parsed and logged properly as expected.  
This is my first test for procmail deployment!  
2018-03-31 08:08:45 - INFO - ----- END: 1522570125.12  
2018-03-31 08:08:45 - INFO - Elapsed Seconds: 0.0624470710754