0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

copilot くん に訊いてみた

Last updated at Posted at 2025-04-03

ちょっとしたスクリプトを書いてもらう

以下は 対話



postfix のメールログから、特定のメールアドレス宛のメールの受信結果(許諾・拒否)をサマリーにするpythonスクリプトを書けますか
Waiting for reply…

はい、もちろんです。以下は、Postfixのメールログから特定のメールアドレス宛のメール受信結果をサマリーするPythonスクリプトの例です。このスクリプトは、Postfixのログファイルを解析し、指定されたメールアドレスに対するメールの受信結果(許諾・拒否)を集計します。
Python

import re
from collections import defaultdict

def parse_postfix_log(log_file_path, target_email):
    with open(log_file_path, 'r') as file:
        logs = file.readlines()

    email_status = defaultdict(lambda: {'accepted': 0, 'rejected': 0})

    for log in logs:
        if target_email in log:
            # Check for accepted email
            if re.search(r'status=sent', log):
                email_status[target_email]['accepted'] += 1
            # Check for rejected email
            elif re.search(r'status=bounced', log) or re.search(r'status=deferred', log):
                email_status[target_email]['rejected'] += 1

    return email_status

def print_summary(email_status):
    for email, status in email_status.items():
        print(f"Summary for {email}:")
        print(f"  Accepted: {status['accepted']}")
        print(f"  Rejected: {status['rejected']}")

if __name__ == "__main__":
    log_file_path = '/var/log/mail.log'  # Path to Postfix log file
    target_email = 'example@example.com'  # Target email address

    email_status = parse_postfix_log(log_file_path, target_email)
    print_summary(email_status)

このスクリプトは、Postfixのログファイルを読み込み、指定されたメールアドレスに対するメールの受信結果を解析します。status=sentというログエントリはメールが許諾されたことを示し、status=bouncedやstatus=deferredはメールが拒否されたことを示します。結果は集計され、サマリーとして出力されます。

スクリプトを実行する前に、以下の点を確認してください:

    log_file_path に正しいPostfixログファイルのパスを指定します。
    target_email に解析したいメールアドレスを指定します。

必要に応じて、条件や正規表現を調整して、Postfixのログ形式に合わせてください。
このスクリプトでは、 拒否されたかは不明です。受信した数はあっているようです。理由は reject 処理した場合、bounce 、 deffered というstatus とはならないからです。
Waiting for reply…

ご指摘ありがとうございます。Postfixのメールログから受信結果をより正確に解析するために、rejectのステータスも考慮に入れたスクリプトを提供します。以下のスクリプトでは、rejectやreject_warningのログエントリもチェックし、メールの受信結果を集計します。
Python

import re
from collections import defaultdict

def parse_postfix_log(log_file_path, target_email):
    with open(log_file_path, 'r') as file:
        logs = file.readlines()

    email_status = defaultdict(lambda: {'accepted': 0, 'rejected': 0})

    for log in logs:
        if target_email in log:
            # Check for accepted email
            if re.search(r'status=sent', log):
                email_status[target_email]['accepted'] += 1
            # Check for rejected email
            elif re.search(r'reject', log) or re.search(r'reject_warning', log):
                email_status[target_email]['rejected'] += 1

    return email_status

def print_summary(email_status):
    for email, status in email_status.items():
        print(f"Summary for {email}:")
        print(f"  Accepted: {status['accepted']}")
        print(f"  Rejected: {status['rejected']}")

if __name__ == "__main__":
    log_file_path = '/var/log/mail.log'  # Path to Postfix log file
    target_email = 'example@example.com'  # Target email address

    email_status = parse_postfix_log(log_file_path, target_email)
    print_summary(email_status)

このスクリプトでは、status=sent、reject、およびreject_warningの各ログエントリをチェックし、メールの受信結果を集計します。これにより、メールが拒否された場合も正確にカウントされるようになります。

スクリプトを実行する前に、以下の点を確認してください:

    log_file_path に正しいPostfixログファイルのパスを指定します。
    target_email に解析したいメールアドレスを指定します。

必要に応じて、条件や正規表現を調整して、Postfixのログ形式に合わせてください。
  • 感想: 質問が曖昧だった可能性は否めない。ただし、posifix の "拒否=reject" というのは RFC5321でも複数言及されているので備えていてほしい知識ではある。
  • テストして判ったこともでもある。テストコードを書かせて実行まで依頼できたら自己完結の可能性はある。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?