Solved

Monitoring new mail in Cyrus-IMAP accounts

Posted on 2006-07-12
7
335 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
  • 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 39

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 24

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 100 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 24

Author Comment

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

Thanks.

Regards,
   Tomas Helgi
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now