[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Script for mail check

Posted on 2011-03-01
7
Medium Priority
?
881 Views
Last Modified: 2013-12-26
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?
0
Comment
Question by:tunes4life
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:kashvinvj
ID: 35007591
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}).
0
 

Author Comment

by:tunes4life
ID: 35007765
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
0
 
LVL 40

Expert Comment

by:noci
ID: 35075503
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.

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 35081073
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.
0
 
LVL 20

Accepted Solution

by:
simon3270 earned 2000 total points
ID: 35081108
It would be cleaner to use a mail program to read and delete the mail, but if that is not possible, the folowing script will delete the mail with the specified text in it.

It assume that $MAIL is the file containing that user's mail, and that the mail follows standard mailbox rules; each mesage starts with a From line, and any lines starting "From " within the mail are escaped (which is why you sometimes see ">From " within an email).  The space after the From is important - there may be one or more "From: " lines, with a colon in them, but we aren't looking for those.  The "checkline" variable contains the text which will uniquely identify the test email.

Before you test this, copy the user's $MAIL file somewhere safe!

Note that there is no locking here - if another mail arrives while the check mail is being deleted, that new mail may be lost.  To make this less of a problem,it might be worht adopting part of noci's solution, and sending these check emails to a dummy user.  Then there is only one email a day for that user,and that email will be the one deleted.

I do make use of a quirk of csplit here - since the first line of $MAIL starts "From ", csplit will create an empty first file, and the second file it creates will contain the first email.  This means that even if all emails (or the only email) in $MAIL is deleted, the "cat" below the "rm" line still has at least one file (i.e. the empty first file) to write to $MAIL.

checkline="This is the test email"

if grep "$checkline" $MAIL >/dev/null
then
  # test email has arrived - delete it
  mkdir /tmp/check.$$
  oldpwd=$(pwd)
  cd /tmp/check.$$
  csplit $MAIL -n 4 -k -s '/^From /' '{*}'
  rm $(grep -l "$checkline" *)
  cat * > $MAIL
  cd "$oldpwd"
  rm -r /tmp/check.$$
else
  # No check email found - tell someone
  echo No Check Email on $(date) | mail -s "No check email today" adminuser@wherever.com
fi

Open in new window

 
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question