How to Analyze SMTP Logs and Extract Email Traffic (PHP Script)
Working with mail servers? Then you already know one thing: š SMTP logs are messy. When a client asks: āCan you send me only my email logs?ā Youāre stuck with a huge log file containing thousands ...

Source: DEV Community
Working with mail servers? Then you already know one thing: š SMTP logs are messy. When a client asks: āCan you send me only my email logs?ā Youāre stuck with a huge log file containing thousands of mixed records. In this post, Iāll show you a simple but powerful way to extract a specific emailās traffic from SMTP logs using PHP. šØ The Problem SMTP logs are not structured per email. Instead, they look like this: SMTP-IN 63EBA13D... 20.57..79 EHLO SMTP-IN 63EBA13D... 20.57..79 MAIL FROM SMTP-IN 63EBA13D... 20.57..79 RCPT TO:[email protected] SMTP-IN 63EBA13D... 20.57..79 DATA š Different emails are mixed together š Same IP continues the flow š Logs are split across multiple lines So filtering by email alone is not enough. š” The Solution Hereās the trick: Find the line containing the target email Extract the IP address from that line Collect nearby lines with the same IP This reconstructs the full SMTP flow. āļø PHP Script <?php $logFile = __DIR__ . "/log/SMTP-Activity.log"; $outp