Link to home
Start Free TrialLog in
Avatar of tunes4life
tunes4life

asked on

Script for mail check

This is my my first question and a weird one. I have a website on a linux server. This website has a company email system which is where we generate a lot of sales. Its basically a PHP contact page that emails the sales department. Every morning, just to make sure that the system is working correctly (no issues with server outgoing or our mail servers incoming) I set up a cron job on the server that hits a php script that tests the email system. Basically the job just hits the contact php page and populates the fields with 'test' values and sends the email just like if a customer had sent it.

I know, very convoluted, sorry.

This is what I'm trying to do:

I want to set up another cron job that runs 5 minutes after the test job runs. This job will check the mail folder, see if the test email has been received. If it has, delete the email and do nothing. If it hasn't received the test email, I want it to send me email at a different address with an alert.

In essence, right now I get a test email every day from the server. I never really read them or even see them anymore because I get them everyday. I'd like to stop getting the daily email and just get an email when the system fails.

I know the mail on the server is stored in a single file for every user. I know I could run a GREP command and search for the text in the email title that says "This is a TEST email". But how would I delete the email if it does?

Am I thinking about this the wrong way?
Avatar of kashvinvj
kashvinvj

You can access the /var/mail directory, you may be able to remove the file. Look for a file with your userID.

You could also create a script to read mail and delete it with a for or while loop (if you have an idea of how many messages { you can tell that via the header option in mail I believe}).
Avatar of tunes4life

ASKER

If I deleted the file in /var/mail, wouldn't that delete all mail for the particular user?

Are you talking a bout a bash script with the loop? Any chance of getting a example of the basic commands? I know the basics of Linux programming but I would assume I would need to incorporation some 'Mail' commands in the script? Maybe example of a site that gives some good tutorials?

Thanks for the quick reply
Avatar of noci
If you have a pop/imap server available, you can use fetchmail to check for mail and optionaly delete it.
OTOH, is it possible for you to have the test mail delivered to another "user" account? In that case you can empty that users mailbox.

If you had Maildir delivery, where every message is in a separate file, your problem would become relatively simple.
Otherwise, you might be able to engineer a solution using a text-mode (not GUI) email client (aka MUA) and expect.
From the point of view of writing an expect script, the simpler the MUA is, the better. So mail or mailx might be preferable to mutt for instance. Your script would look for your distinctive test mail header and produce a report if it doesn't find one. Then it would get the MUA to delete the message.
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial