Ubuntu Postfix: How to check if my email headers are RFC5321/RFC5322 compliant

To check if your email headers are RFC 5321 (SMTP) and RFC 5322 (Internet Message Format) compliant on an Ubuntu system using Postfix, you can follow these steps:

  1. Install Required Tools:

    Ensure you have the necessary tools installed to analyze email headers and check their compliance. You’ll need mutt and python3-email-validator. You can install them using the following command:

    sudo apt-get install mutt python3-email-validator
    
  2. Retrieve the Email Header:

    First, you need to obtain the email header that you want to check. You can do this by saving the email as a text file, and then extracting the header using the mutt command-line email client. Assuming your email is in a file called email.txt, you can extract the header using:

    mutt -f email.txt -e "unset record"
    

    This command will display the header in your terminal.

  3. Use Python Script for Validation:

    You can use a Python script to check the extracted email header for RFC compliance. Create a Python script, for example, check_email_headers.py, with the following content:

    import email
    from email_validator import validate_email, EmailNotValidError
    
    def is_valid_email_header(header_text):
        try:
            email_message = email.message_from_string(header_text)
    
            # Check RFC 5321 compliance (SMTP)
            if 'Received' not in email_message:
                return False
    
            # Check RFC 5322 compliance (Internet Message Format)
            if 'To' not in email_message or 'From' not in email_message:
                return False
    
            # Additional checks can be added as needed
    
            return True
        except Exception as e:
            return False
    
    with open('email_header.txt', 'r') as header_file:
        header_text = header_file.read()
    
    if is_valid_email_header(header_text):
        print("Email headers are compliant.")
    else:
        print("Email headers are not compliant.")
    

    Save this script and make it executable:

    chmod +x check_email_headers.py
    
  4. Run the Validation Script:

    Run the validation script and provide the path to the email header file you extracted earlier:

    ./check_email_headers.py
    

    The script will analyze the email header and provide feedback on whether it’s compliant with RFC 5321 and RFC 5322.

Please note that this is a basic validation script, and you may need to perform additional checks or customize it according to your specific requirements. Ensuring complete RFC compliance can be complex, and this script serves as a starting point for basic validation.