Solved

Monitoring new mail in Cyrus-IMAP accounts

Posted on 2006-07-12
7
343 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 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 25

Author Comment

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

Thanks.

Regards,
   Tomas Helgi
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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