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
- Log into your server via SSH.
- Make sure you're in your user's home directory:
[server]$ pwd /home/username
- Create a new file via SSH, and then name it something like mailrejections.sh.
- 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.
#!/bin/bash #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) echo $emailContent | mail -s "These emails were rejected" firstname.lastname@example.org done
- 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.
- Create a new cron job in your panel:
- Make sure to adjust the username to the correct Shell user for which you created the script.
- 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.
- 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.