How to create a script that sends you rejected emails

The following describes how to create a script and cron job that sends you rejected emails from the server.


When an email is sent from your web server and it then gets blocked or rejected, it is stored in the following directory:


To view the blocked or rejected emails, you must manually log into your server and check this directory. A simpler option is to instead create a cron job that triggers a shell script. The script then tells the server to email you any blocked or rejected emails when it occurs.

For more information about blocked or rejected emails, see the following article:

Creating the shell script

  1. Log into your server via SSH.
  2. Make sure you're in your user's home directory:
    [server]$ pwd
  3. Create a new file via SSH, and then name it something like
  4. Add the following code to this file:
    • You only need to change the fields in bold.
    • You can change the message to anything you like.
    #Check ~/Maildir/new for any files created within the last 15 min
    files=$(find ~/Maildir/new -type f -mmin -15)
    for emailFileName in $files; do
      #open each email individually and assign to new variable
      emailContent=$(cat $emailFileName)
    printf '%s\n' "${emailContent[*]}" | mail -s "These emails were rejected" done
  5. Make sure to change the email to the address you'd like to receive these notifications from.

Creating the cron job

After you create the script, you must create a new cron job to run every 15 minutes. The cron job then runs the file you just created.

  1. Create a new cron job in your panel:
  2. Make sure to adjust the username to the correct Shell user for which you created the script.
  3. Select 'Every 15 minutes' for the time frame.

    You can change the time frame to anything you like, but just make sure to update the same time frame in your script to match.

  4. Click the Add button to save.

The cron job runs your script every 15 minutes. If an email is rejected, a copy of it is then sent to the address you specified in the script.

Did this article answer your questions?

Article last updated .