My email server was hammered by spam where Proxmox Mail Gatway cant cope with effectively. I had to find a solution to give feedback to a PMG directly from my mailbox Spam folder so PMG can update blocklisted IP.
This script is designed to connect to an IMAP (Internet Message Access Protocol) server to parse emails in the spam/junk folder and extract the origin IP addresses. It then connects to a Proxmox Mail Gateway (PMG) and adds these extracted IP addresses to the blacklist. Additionally, it ensures that only unique IP addresses are added to the blacklist and removes any duplicates.
Before using this script, ensure you have the following prerequisites:
-
Python Environment: This script is written in Python. Make sure you have Python installed on your system.
-
Dependencies: Install the required dependencies using the following command:
pip install imaplib email requests
-
Credentials: You need to have credentials for both the IMAP server and the Proxmox Mail Gateway. Create a file named
credentials.py
in the same directory as the script with the following content:IMAP_SERVER = 'your_imap_server_address' IMAP_USERNAME = 'your_imap_username' IMAP_PASSWORD = 'your_imap_password' PMG_API_URL = 'your_pmg_api_url:8006/api2/json' PMG_USERNAME = 'your_pmg_username' PMG_PASSWORD = 'your_pmg_password'
-
Whitelist IPs: If there are any IP addresses that should not be added to the blacklist, list them in a file named
whitelist_ips.txt
in the same directory as the script, with each IP address on a new line. -
Script will look for Spam or Junk folder. If spam is in different folder name you need to update it accordingly.
To use the script, follow these steps:
-
Run the Script: Execute the script using the following command:
python IMAP-and-PMG-Blacklist-Integration.py
-
View Outputs:
- The script will print status messages to the console indicating the progress of fetching and processing emails, adding IPs to the blacklist, and removing duplicates.
- The oldest non-whitelisted unique source IPs will be saved to a file named
source_ips.txt
in the same directory as the script.
- Security: Ensure that the credentials file (
credentials.py
) and any sensitive information are kept secure and not shared publicly. - Whitelist IPs: Review the
whitelist_ips.txt
file to ensure that any IPs listed there should not be blacklisted. - Error Handling: The script includes error handling for various scenarios, but ensure proper monitoring and logging in a production environment.
- Piotr Esse
This project is licensed under the MIT License.