?
Solved

Monitoring new mail in Cyrus-IMAP accounts

Posted on 2006-07-12
7
Medium Priority
?
345 Views
Last Modified: 2008-02-01
  Hi!

I need a Perl (cron job) script to monitor new mail on several email accounts on my Postfix/Cyrus-IMAP server.
This job should count new messages (unread messages) and send the result to specific email address.
How do I do this ?

Regards,
  Tomas Helgi
0
Comment
[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
  • 3
  • 2
7 Comments
 
LVL 24

Expert Comment

by:slyong
ID: 17089556
Hi Tomas,

I am not sure how you want the total solution to be.  I would use fetchmail for this task:

1) create ~/.fetchmailrc with something like:
poll imap.your.mail.server with proto IMAP user youruserid password yourpassword is youruseridonlocal

2) create a Perl script like:
#!/usr/bin/perl
my $result   = `fetchmail -c`;
chop $result;

my @mailinfo = split(/ /, $result);
my $unread   = $mailinfo[0] - substr($mailinfo[2],1);

open (MAIL, "|/usr/sbin/sendmail -t ");
  print MAIL "From: noreply\@yourdomain.com\n";
  print MAIL "To: receiver\@receiverdomain.com\n";
  print MAIL "Content-Type: text/plain\n";
  print MAIL "Subject: New Mail Notice\n";
  print MAIL "You have $unread new mail(s) in $mailinfo[7]\n";
close (MAIL);
0
 
LVL 40

Expert Comment

by:noci
ID: 17095816
Or access the IMAP store directly
http://www.cpan.org/modules/by-module/Mail/Mail-IMAPClient-2.2.9.readme

The imapsync tool uses it to synchronise imap stores.
(ie transplant messages as the are from one to another store.)
0
 
LVL 24

Expert Comment

by:slyong
ID: 17196443
I think that the fetchmail + Perl Script that I provided pretty much covered what is needed (i.e. check how many mail in the mailbox and send an email).  The solution is provided so the points should be rewarded accordingly.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Author Comment

by:Tomas Helgi Johannsson
ID: 17210497
This is not what I was looking for.
This is just for one single account.

Regards,
  Tomas Helgi
0
 
LVL 24

Accepted Solution

by:
slyong earned 300 total points
ID: 17210506
1) create multiple fetchmail configuration files with something like:

account1.fetchmailrc
poll imap.your.mail.server with proto IMAP user youruserid1 password yourpassword1 is youruseridonlocal1

account2.fetchmailrc
poll imap.your.mail.server with proto IMAP user youruserid2 password yourpassword2 is youruseridonlocal2


2) create a Perl script like:
#!/usr/bin/perl
my $result   = `fetchmail -c -f account1.fetchmailrc`;
chop $result;

my @mailinfo = split(/ /, $result);
my $unread   = $mailinfo[0] - substr($mailinfo[2],1);

open (MAIL, "|/usr/sbin/sendmail -t ");
  print MAIL "From: noreply\@yourdomain.com\n";
  print MAIL "To: receiver1\@receiverdomain.com\n";
  print MAIL "Content-Type: text/plain\n";
  print MAIL "Subject: New Mail Notice\n";
  print MAIL "You have $unread new mail(s) in $mailinfo[7]\n";
close (MAIL);

my $result   = `fetchmail -c -f account2.fetchmailrc`;
chop $result;

my @mailinfo = split(/ /, $result);
my $unread   = $mailinfo[0] - substr($mailinfo[2],1);

open (MAIL, "|/usr/sbin/sendmail -t ");
  print MAIL "From: noreply\@yourdomain.com\n";
  print MAIL "To: receiver2\@receiverdomain.com\n";
  print MAIL "Content-Type: text/plain\n";
  print MAIL "Subject: New Mail Notice\n";
  print MAIL "You have $unread new mail(s) in $mailinfo[7]\n";
close (MAIL);
0
 
LVL 25

Author Comment

by:Tomas Helgi Johannsson
ID: 17211243
Hmm, ok this would go with slight modifications.

Thanks.

Regards,
   Tomas Helgi
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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