LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-08-2005, 03:53 PM   #1
fiservguy
Member
 
Registered: Nov 2004
Distribution: RHEL 2.1, RHEL 3.0, SUSE 9.2
Posts: 31

Rep: Reputation: 16
Scripting email handling


(Sorry if this is the wrong forum...)

I have a server that is sending out automated emails for a website. This is working fine. However, the powers-that-be want any undeliverable email to be flagged, and pertinent information pulled out of the message and put in a database for report generation.

My question is: is there a way to parse through received mail, search for strings, perform actions based on whether or not those strings exist, and delete all parsed messages after they've been processed?

I know I can grep through the appropriate mail spool in /var/spool/mail, but I'm nervous about trying to edit that file. I'd be less nervous editing a user's mbox file, but I don't know how to move mail from the spool to this file without actively reading it.

Any suggestions would be greatly appreciated.

matt
(fiservguy)
 
Old 02-08-2005, 04:04 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Set up some sort of mail delivery system on your local box. Preferrably to an mbox. Then you can use grep and sed to extract your needed information. Or, if you use qmail or something similar, you can use the .qmail file feature of redirection to a program. I'm pretty sure other MTAs offer similar functionality.
 
Old 02-08-2005, 04:05 PM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
This is a tough one. What I would probably do is create a script that does the desired actions you want and run it through cron however often you want to check for these undelivered emails.

As for the scripting language to go with I would use either Perl or Python. Both can do the desired actions you want. You'll have to get good at regular expressions to parse through the contents of the mail file. I'm not sure what strings you would be looking for but pretty much anything can be done with regular expressions. Also, both languages have the ability to write to a database(i.e. mysql) and the ability to edit text files.

Am I way off base here?
 
Old 02-08-2005, 04:21 PM   #4
fiservguy
Member
 
Registered: Nov 2004
Distribution: RHEL 2.1, RHEL 3.0, SUSE 9.2
Posts: 31

Original Poster
Rep: Reputation: 16
Matir,

Thanks for the quick reply. Sendmail is loaded on the box in question; how can I (can I?) send just the mail for the user in question to a file? Would this be a sendmail.cf/mc setting or a .mailrc setting?

Crashed_Again,

You've described what I plan on doing. I just need some suggestions in how to get started. I've been boning up on my regular expressions, and I'm building a test file to play with to make sure I'm extracting what I need. Unfortunately, I'm a relatively new programmer, so this will be an exciting experience for me.

Thanks again for the quick responses.
 
Old 03-08-2006, 01:42 AM   #5
mickeyren
LQ Newbie
 
Registered: Feb 2005
Posts: 14

Rep: Reputation: 0
hi guys,

i may have a similar problem,

i need my qmail mail server to call my php script file whenever it receives a bounced email.

how do i configure it to receives bounce email and then call my php file?

thanks
 
Old 03-08-2006, 09:24 AM   #6
fiservguy
Member
 
Registered: Nov 2004
Distribution: RHEL 2.1, RHEL 3.0, SUSE 9.2
Posts: 31

Original Poster
Rep: Reputation: 16
I ulitimately went with a cron job that searches the mail file for certain keywords (specifically SMTP error codes), then extracts the pertinant information.

Set your job to run often enough and it can be "near real time" I suppose.

fiservguy
 
Old 03-08-2006, 11:46 AM   #7
scottmorris
Novell CoolSolutions Editor
 
Registered: Jan 2006
Distribution: SUSE 9.3 - 10.0
Posts: 30

Rep: Reputation: 16
Alternative solution

One thing, if I may suggest it: You might consider using a small app called 'getmail' to retrieve the mail from the server to a different location than where your 'live' email folder is. You just simply instruct getmail to not delete the messages from the server, so that sendmail or whatever can still get them. This way, you can have your own copy of the messages, and not have to mess with the live messages. You can then set up a cronjob and a script (PHP, Perl, Python, etc) to parse your messages, looking for bounces, recording them to whatever database you are using.
 
Old 03-08-2006, 03:06 PM   #8
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337

Rep: Reputation: 358Reputation: 358Reputation: 358Reputation: 358
Quote:
Originally Posted by fiservguy
I know I can grep through the appropriate mail spool in /var/spool/mail, but I'm nervous about trying to edit that file. I'd be less nervous editing a user's mbox file, but I don't know how to move mail from the spool to this file without actively reading it.
You could create a .forward file in the user's home directory and grab the incoming email that way.

Example:
Code:
$ cat $HOME/.forward
\username, "| /path/to/a/parser/program"
$
Replace "username" with the actual username that will be receiving the email.

The above .forward file does two things, [1] it sends incoming mail to the default destination (that's the "\username" directive before the comma) and [2] if sends a copy of the incoming mail to the STDIN of "/path/to/a/parser/program" The parser program is invoked for every new incoming email.

I have used the above technique with sendmail on Solaris, I assume it would work with other mail transport agents on Linux (you'd have to try it).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What email program can work with email header on pop3 server TruongAn Linux - Software 0 01-13-2005 12:48 PM
How do I configure postfix master to forward all email to an email server ? hello321_1999 Linux - Software 1 11-18-2004 05:43 AM
How to Import Microsft outlook Email in any Red hat 9 Email mobassir Linux - Software 2 07-28-2003 11:42 AM
How to Import Microsft outlook Email in any Red hat 9 Email Software mobassir Linux - Software 0 07-26-2003 05:08 AM
How do I: convert my fetchmail email file to separate email files? ancientt Linux - Software 1 02-03-2003 03:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration