Remove Bad-Reputation IPs from Proxmox Mail Gateway and Retry Bounced Emails

Remove Bad-Reputation IPs from Proxmox Mail Gateway and Retry Bounced Emails

Proxmox Mail Gateway is an open-source email security solution that is designed to protect your mail server from spam, viruses, phishing attempts, and other email-based threats.


To automate the process of retrying bounced emails through different IPs in Proxmox Mail Gateway, you can use this Python script that checks for bounced emails and removes the specific IP address from the Postfix configuration, and reloads Postfix.

import subprocess
import re

def remove_ip_from_postfix(ip):
    # Remove the IP from Postfix configuration
    subprocess.run(['postconf', '-e', f'smtp_bind_address={ip}'])
    subprocess.run(['service', 'postfix', 'reload'])

def retry_bounced_email(email_id):
    # Retry the bounced email using Postfix sendmail command
    subprocess.run(['sendmail', '-q', '-i', email_id])

def monitor_bounced_emails():
    # Monitor the email server logs for bounced emails
    log_file = '/var/log/mail.log'  # Path to the mail server log file

    with open(log_file, 'r') as log:
        for line in log:
            # Check if the line indicates a bounced email
            if 'status=bounced' in line:
                # Extract the email ID and IP address from the log line
                match = re.search(r'mail_id=(\w+).*\[([0-9.]+)\]', line)
                if match:
                    email_id = match.group(1)
                    ip_address = match.group(2)

                    # Remove the IP from Postfix configuration and reload
                    remove_ip_from_postfix(ip_address)

                    # Retry the bounced email
                    retry_bounced_email(email_id)

# Start monitoring for bounced emails
monitor_bounced_emails()

Run the script in the background to automatically switch to different IP addresses and retry bounced emails until the next bounce occurs.

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.