How Do You Print the Email Addresses That Bounced after They Were Sent from Your Postfix Server?

Problem scenario
You have installed Postfix (e.g., version 3.2.0) on your Linux server. You know several emails that were sent (or attempted to be delivered) bounced. You want to have a list of those unique email addresses by themselves. What do you do?

Solution
Run this command:

sudo cat /var/log/mail | grep bounce | awk ' $0 ~ /@/ {print $5}' | awk '{FS="to="; print $2}' | tr "," " " | sort | uniq
# The first AWK finds only the email addresses that bounced (without the rest of the line)
# The second AWK removes the "to=" in the beginning of the string
# The tr command removes the commas

Please note that this does not take into account what is in the mail queue. You may want to use the mailq command.

Leave a comment

Your email address will not be published. Required fields are marked *