[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Monitoring new mail in Cyrus-IMAP accounts

  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
Tomas Helgi Johannsson
Asked:
Tomas Helgi Johannsson
  • 3
  • 2
1 Solution
 
slyongCommented:
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
 
nociSoftware EngineerCommented:
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
 
slyongCommented:
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
Tomas Helgi JohannssonAuthor Commented:
This is not what I was looking for.
This is just for one single account.

Regards,
  Tomas Helgi
0
 
slyongCommented:
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
 
Tomas Helgi JohannssonAuthor Commented:
Hmm, ok this would go with slight modifications.

Thanks.

Regards,
   Tomas Helgi
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now