Security

WordPress Sending Spam Emails? Find the Script in 5 Minutes

By , Lead SEO & Co-founder 7 min read
A mail server rack at night spewing glowing envelope-shaped packets from one slot, a crimson light marking the culprit module, a laptop on a cart tracing it, the rest of the rack steady in cyan

WordPress sending spam emails is one of the easier hacks to trace, because the server's mail log almost always names the folder the spam is coming from. You don't need a scanner to find it. On a typical cPanel server, one command reads the log and ranks every folder by how much mail it sent, and the culprit lands at the bottom of the list.

That's the whole trick, and it takes about five minutes. This post shows the command, explains every piece of it, and covers what to do after you've found the script: clear the queue, check your mailboxes, and get your domain off blocklists.

If your host has already suspended email or the whole account, you can send me what they told you through a short questionnaire. For emergencies, I start within a few hours during business hours (Pacific and Eastern time zones).

Three ways a WordPress site sends spam

Before the command, a quick map. Spam leaving your server usually comes from one of these:

  1. A malicious PHP script (a small program uploaded by an attacker) that calls the mail function directly. This is the most common one after a hack, and the one this post finds.
  2. A hijacked email account. Someone guessed or stole a mailbox password and sends through your server like a normal user.
  3. An abused contact form. Bots submit your form with spam content, and your site dutifully emails it out, sometimes to addresses they choose.

The log tells you which one you have, so let's read it.

Find the spam script from the mail log

This needs SSH (secure command-line access) on a server running Exim, the mail server cPanel uses. On a VPS or dedicated server, run it as root. On shared hosting, you probably can't read the main log yourself. Skip to the next section for the shared-hosting route.

Terminal
# Read-only: rank every folder that sent mail, by number of messages. The biggest sender ends up last.
# Changes nothing. On a busy server it can take a minute to read the whole log.
grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F"cwd=" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n

Here's what each piece does, in plain English:

  • grep cwd /var/log/exim_mainlog pulls every log line that records cwd (the "current working directory," meaning the folder a sending script ran from).
  • grep -v /var/spool drops Exim's own internal lines, which aren't interesting.
  • The two awk commands cut each line down to just the folder path.
  • sort | uniq -c | sort -n counts how many times each folder appears, and sorts them smallest to largest.

GoDaddy's help center documents the same technique, in case you want a second reference.

What the output looks like

A healthy server shows a few home folders with modest counts: contact forms, WooCommerce order emails, password resets. A hacked one has an outlier at the bottom, something like:

Text
     12 /home/exampleuser/public_html
     31 /home/exampleuser/public_html/wp-admin
  14009 /home/exampleuser/public_html/wp-content/uploads/2013/04

That last line is from a real cleanup I did, with the username changed. Fourteen thousand and nine emails, from a folder that should only hold photos from April 2013. Inside it sat a file named object41.php. It looked innocent, but its date didn't match any other file in the folder. That mismatch is often the giveaway.

Go to the folder at the bottom of your list, and look for PHP files that don't belong. In wp-content/uploads, any .php file at all is suspect.

Shared hosting: the no-root route

On shared hosting, you usually can't read /var/log/exim_mainlog. Two options instead:

  • cPanel Track Delivery. Under Email > Track Delivery, cPanel shows a report of mail sent from your account, per cPanel's docs. It shows senders, recipients and results, and you can add a column for the SMTP authentication type. Lots of mail sent through a mailbox login points to a hijacked account. Mail with no login points to a script.
  • Ask your host. Send them the time range and ask for the cwd paths for mail sent from your account. Most hosts will run the command above for you. They often already know, which is why they emailed you.

Read the headers of mail still in the queue

If spam is still waiting to go out, each message carries a clue in its headers. On cPanel servers, PHP adds a header naming the script that sent it. The Exim command line has options to count and read queued messages:

Terminal
# Read-only: how many messages are waiting in the queue right now.
exim -bpc

# Read-only: print the X-PHP-Script header of the first 20 queued messages.
# The value is the website and script path that sent each one.
for id in $(exiqgrep -i | head -20); do exim -Mvh "$id" | grep -i "X-PHP-Script"; done

Not on cPanel? PHP itself can log every mail it sends. The PHP mail configuration docs describe mail.log, which records "the full path of the script, line number, To address and headers" for every mail() call. Ask your host to switch it on for your account, or set it in php.ini on your own server.

Stop it: remove the script, then clear the queue

Order matters here. Clear the queue before you remove the script, and the script refills it.

  1. Back up the account, including the infected files. Evidence helps.
  2. Remove or quarantine the script you found. Then search for its siblings. One mailer rarely arrives alone. My guide to reading obfuscated PHP like eval(base64_decode) helps if the file is scrambled.
  3. Clear the frozen spam from the queue. Frozen messages are ones Exim gave up on, usually bounces from fake addresses:
Terminal
# Deletes frozen messages from the mail queue. Root only. Cannot be undone.
# Could break: also deletes any legitimate frozen messages, so glance at a few first with exim -Mvh.
exiqgrep -z -i | xargs exim -Mrm

The -z flag matches only frozen messages and -i prints just the message IDs, per the Exim utilities docs. exim -Mrm removes them without sending bounces. In WHM, Mail Queue Manager does the same thing with buttons.

  1. Change passwords for every mailbox on the domain, plus hosting, SFTP, WordPress admins and the database.

If the log points to a mailbox, not a script

If the spam was sent with a mailbox login, there's no script to find. The password leaked. Change it, check that mailbox's forwarding rules and filters for anything the attacker added, and turn on two-factor login wherever your email provider offers it.

If it points to a contact form, add spam protection to the form, make sure it only ever sends to your own address, and update the form plugin.

Get your domain off blocklists

A spam run can land your server's IP address or domain on blocklists (lists mail providers use to reject spam). Check with MXToolbox's blacklist check, which tests your IP against more than 100 lists. Each list has its own delisting process, and most want proof the problem is fixed before they remove you.

Gmail has its own threshold. Google's sender guidelines ask all senders to keep spam rates reported in Postmaster Tools below 0.3%. If your business email shares a domain with the hacked site, watch for delivery problems over the next few weeks.

Why the spam comes back

The mailer is a symptom. Something uploaded it, and that something may upload another one tomorrow. Usually it's an outdated plugin, a backdoor elsewhere in the account, or a cron job that re-downloads the script. I cover those hiding places in my post on reinfections that start in the hosting account, and the full order is in my hacked WordPress recovery steps.

Final thoughts

When WordPress starts sending spam, the mail log has already done the detective work. Run the cwd command, go to the folder at the bottom of the list, find the PHP file that doesn't belong, then clean, clear the queue and change passwords, in that order. Then check blocklists and close the hole that let the script in.

If you'd rather hand the whole thing off, that's what my WordPress malware removal service is for. Tell me what the logs show. Standard cleanups start at $350, and emergency jobs at $500.

WordPress sending spam emails FAQs

How do I know if my WordPress site is sending spam?

Common signs are a warning or suspension notice from your host, bounce messages for emails you never sent, a sudden pile of messages in the mail queue, or your own emails landing in customers' spam folders. On a cPanel server, the cwd command in this post confirms it in a minute by ranking which folders sent the most mail.

Can a WordPress plugin send spam?

Yes, in two ways. A vulnerable plugin can let an attacker upload a mailer script, which is the most common cause. And a poorly protected contact or newsletter form can be abused by bots to send messages through your site. The mail log tells you which folder sent the mail, which usually points straight at the responsible plugin or the uploaded script.

Will my host suspend my account for sending spam?

Many hosts will, at least for email, because spam from one account can get the whole server's IP address blocklisted. A suspension is usually lifted once you show the source is removed, the queue is cleared and passwords are changed. Reply to your host's notice with what you found and fixed. A specific answer gets you back online faster.

How do I find a spam script on a server without root access?

Use cPanel's Track Delivery report to see what your account sent and whether it used a mailbox login. Then ask your host to pull the cwd paths from the Exim log for your account. If you can, also search wp-content/uploads for PHP files, which shouldn't exist there. Most uploaded mailers sit in uploads or in folders with random names.

Want a second pair of eyes on your site?

Tell me what's going on in the hacked website repair questionnaire. I reply with a plan and a fixed quote within one business day.

Starting at $350 Start the questionnaire