When your server IP shows up on a spam blocklist, the first reaction is usually "we don't send newsletters, this must be a mistake." Sometimes the listing is stale or left over from a previous user of the IP. More often, the server really did send the mail, and the job is to find which part of it did. Blocklist operators list an IP based on mail they saw arrive from it, so the mail existed somewhere, even if no one on the team wrote it.
Finding the source gets much easier once you know the few ways a message can enter a Linux mail system and what each one leaves behind.
The entry points a mail server actually has
A mail transfer agent (MTA) such as Exim or Postfix accepts mail in a small number of ways. Every message in its queue came through one of them.
The first is SMTP from the network. A client opens a TCP connection, usually to port 25, 465, or 587, and goes through the SMTP conversation: EHLO, optionally AUTH with a username and password, then MAIL FROM, RCPT TO, and DATA. The MTA decides at the RCPT TO stage whether to accept each recipient. A local recipient is accepted. A remote recipient should only be accepted when the client has authenticated or comes from a trusted network. That decision is called relay control.
The second is local submission. A program on the server runs the sendmail binary and writes the message to its standard input. No network connection is involved. PHP's mail() function works this way on Linux. It runs whatever sendmail_path points to, by default sendmail -t -i, as the user the PHP process runs under. With Exim, sendmail is Exim itself, which reads the message and queues it. With Postfix, the sendmail compatibility program hands the message to postdrop, which writes it into the maildrop directory, and the pickup daemon then moves it into the queue.
The third path skips the MTA completely. Any process that can open an outbound TCP connection to port 25 on a remote mail server can speak SMTP directly. Malware often does this, because your MTA logs nothing and nothing sits in your queue.
These three paths match the usual culprits. Authenticated SMTP points to a stolen mailbox password. Local submission points to a malicious or abused script. Unauthenticated network traffic points to an open relay or a process sending directly. Each one is recorded in a different place.
Reading what the MTA recorded at arrival
Every accepted message gets a queue ID and an arrival log line. The arrival line records how the message came in, and it usually tells you which path to investigate.
In Exim, the arrival line contains <= followed by the envelope sender, and the fields after it matter:
P=esmtpaorP=esmtpsameans the message arrived over SMTP with authentication. TheA=field shows which authenticator and which login were used. On cPanel servers this often looks likeA=dovecot_login:user@example.com.P=localmeans local submission, andU=names the system user who ran the binary.- If Exim logs arguments (the
+argumentslog selector, which many cPanel setups enable), you also get acwd=entry. It shows the directory the submitting process was running in. For a PHP script, that is often the folder the script lives in.
Postfix splits the same information across its daemons. Mail from the network shows up in postfix/smtpd lines, and authenticated sessions include sasl_username=. Local submissions show up in postfix/pickup lines with uid= and the sender. The UID tells you which account ran sendmail.
So the first useful question is what the arrival line says. Summarizing the logs by login, user, and directory usually makes the source obvious, because spam is high volume and stands out against normal traffic.
# Exim (cPanel path; Debian/Ubuntu use /var/log/exim4/mainlog)
exim -bpc
grep -o 'A=dovecot_[a-z]*:[^ ]*' /var/log/exim_mainlog | sort | uniq -c | sort -rn | head
grep -o 'cwd=[^ ]*' /var/log/exim_mainlog | sort | uniq -c | sort -rn | head
exim -Mvh MESSAGE_ID
# Postfix (RHEL-family uses /var/log/maillog)
postqueue -p | tail -n 1
grep -o 'sasl_username=[^ ,]*' /var/log/mail.log | sort | uniq -c | sort -rn | head
grep 'postfix/pickup' /var/log/mail.log | grep -o 'uid=[0-9]*' | sort | uniq -c | sort -rn
postcat -q QUEUE_ID
All of these commands only read. exim -bpc counts queued messages. The last line of postqueue -p gives the Postfix queue total. exim -Mvh and postcat -q print a single queued message so you can see its headers and recipients. Log paths and authenticator names vary by distribution and control panel, so adjust them to your server.
Stolen SMTP account
If one login dominates the authenticated count, someone has that mailbox password. It may have come from phishing, from reuse after another site's breach, or from a weak password guessed through repeated SMTP AUTH attempts. The MTA treats these messages as legitimate because the client proved it knew the password.
Look at the client IPs that logged in with that account. A normal user connects from a handful of addresses. A compromised account typically shows logins from many unrelated IPs, often in a short window. Credential stuffing produces the same pattern on other login forms, and the same log-reading approach applies. This article on spotting credential stuffing in auth logs covers that approach.
To fix it, change the password, end any active sessions if your mail server supports that, and then clear the spam from the queue. Clearing the queue permanently deletes messages. Before you do it, confirm you are not removing the account owner's legitimate mail along with the spam.
A PHP script sending through sendmail
If the arrival lines show local submission, the mail came from a process on the server. On shared hosting and WordPress sites, that process is very often a PHP file. Sometimes it is a mailer an attacker uploaded after exploiting a vulnerable plugin or upload form. Sometimes it is a legitimate contact form that doesn't validate its input, so spammers inject extra recipients through the header fields.
The cwd= value in Exim or the UID in Postfix narrows the source to one account and often one directory. PHP can also label each message with the script that sent it. With mail.add_x_header enabled, mail() adds an X-PHP-Originating-Script header containing the UID and the script filename. You can see it when you print a queued message's headers. If the header is missing, the setting may be off, or the script may skip mail() and talk SMTP itself.
Once you have the file, search the web server access log for POST requests to it. That shows when the sending started and from which IPs, and it often points back to how the attacker got in. A dropped mailer means the attacker had a way to write files, which usually means code execution on the site. Removing that one file rarely closes the hole. If you find one, read this guide on deciding whether to clean the server or rebuild it before you treat the incident as finished.
Open relay and mail that never touches the queue
If neither authenticated logins nor local submissions explain the volume, look at unauthenticated SMTP. An open relay accepts mail for remote recipients from anyone. In Exim, the arrival line would show P=esmtp or P=esmtps with no A= field, and the recipient would be a domain you don't host.
In Postfix, smtpd_relay_restrictions controls relaying. Older setups use smtpd_recipient_restrictions instead. The usual safe pattern is permit_mynetworks, then permit_sasl_authenticated, then reject_unauth_destination. A mistake in mynetworks, such as including a whole public range, silently turns the server into a relay for that range. Current distribution defaults are not open relays, so an open relay usually comes from a manual change.
To confirm, test from a host outside your network:
- Connect to your server on port 25.
- Send a
MAIL FROMwith an outside domain. - Send a
RCPT TOfor a different outside domain. - Check the reply to
RCPT TO. A correctly configured server rejects it with a relay-denied error. A 2xx reply means the server is relaying.
The last case is the most confusing. The queue is empty, the logs show nothing unusual, and the IP is still listed. In this case a process is probably sending directly to remote mail servers on port 25. The MTA never saw the mail, so it has no record of it. You have to look at the network instead:
ss -tnp '( dport = :25 )'
Run it as root so ss can show which process owns each connection. Your MTA will normally have some outbound port 25 connections while it delivers mail. Any other process holding one is suspect, such as php, perl, or a binary with a random-looking name. Check its binary path through /proc/PID/exe and its owner. Blocklists that track botnet activity often list IPs for exactly this kind of traffic. Some listing details include the HELO name the sender used. If that name doesn't match your server's hostname, the mail probably didn't come from your MTA.
You can prevent this whole category by blocking outbound port 25 for every user except the MTA. WHM has an option for this called SMTP Restrictions. On servers without a panel, firewall owner matching can do the same job. Check your panel or firewall documentation for the exact setting.
What to do with this
Start with the blocklist's own lookup page, because it usually says why the IP was listed and roughly when. Then read the arrival lines from that time. An authenticated login points to a stolen password. A local submission points to a script. An empty queue with a listed IP points to direct outbound connections. Fix the source before you request delisting, because an IP that keeps sending usually gets listed again. For support engineers, the most useful habit is to collect the listing reason and the arrival lines before touching the queue. Deleting the spam first also deletes the evidence of where it came from.