Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Silly idea, need help (verbal announcement of inbound emails)

  1. #11
    Dirk's Avatar
    Dirk is offline Moderator
    Join Date
    May 2006
    Location
    England.
    Posts
    927
    Rep Power
    10

    Default First version done!!

    Ok, well I've pulled together some sample code from the net, played around with it a little and I have a version that works!

    I'm using pop3 and the always bcc function, deleting the mail from the copy account once it's been announced. The script is running now and is shown for your amusement below. This only really took about an hour, I'm new to perl and there are some parts of this code that I dont actually understand. Perhaps once you have all finished laughing, you can offer a reworked version?

    I'll come back to the code tomorrow and try to make something more concise, but for now, rest

    PHP Code:
    #!/usr/bin/perl -w

    use Net::POP3;

    while () {
    print 
    "Checking POP3 Server.... \n";

    # Connect to pop3 server
    my $pop3 Net::POP3->new("10.100.100.2") || die "Error : Couldn't log on to server";

    # Login to pop3 server
    my $Num_Message $pop3->login("spokenemail""spokenemail");
    my $Messages $pop3->list();
    my ($MsgId$MsgDate$MsgFrom$MsgTo$MsgCc$MsgSub);
    my ($MsgAttach$MsgSize$MsgHeader$MsgHeadFlg$MsgBody);

    foreach 
    $MsgNo (keys %$Messages)
        {
        
    my $MsgContent $pop3->get($MsgNo);
        
    my $count 0;
        
    $MsgHeadFlg 0;
        
    $MsgBody "";
     
        
    # Process message data
        
    while()
        {

        
    # Exit if last line of mail
        
    if ($count >= scalar(@$MsgContent))
            {
            
    last;
            }

            
    # Check if end of mail header
            
    if (@$MsgContent[$count] =~ /^\n/)
            {
            
    $MsgHeadFlg 1;
            }

        
    # Proceed if message header not processed
        
    if (not $MsgHeadFlg)
            {

            
    # Split the line 
            
    my @LineContent split /: /, @$MsgContent[$count];

            
    # Check Header Info
            
    SWITCH:
            {
            
    # Get message from
            
    $LineContent[0] =~ /From/&& do
                {
                
    $MsgFrom $LineContent[1];
                
    last SWITCH;
            };
        
            
    # Get message to
            
    $LineContent[0] =~ /To/&& do
            {
            
    $MsgTo $LineContent[1];
    print 
    $MsgTo;
            
    last SWITCH;
            };
            
            
    # Get message subject
            
    $LineContent[0] =~ /Subject/&& do
            {
            
    $MsgSub $LineContent[1];
            
    last SWITCH;
            };
        }
        }
        else
        {
        
    # Process message body
        
    $MsgBody .= @$MsgContent[$count];
        }
        
    $count++;
      }
      
    system "swift 'New message from: $MsgFrom'";
      
    system "swift 'For: $MsgTo'";
      
    system "swift 'Subject is: $MsgSub'";
      
    $pop3->delete($MsgNo);
    }

    # Disconnect from pop3 server
    $pop3->quit();

    sleep (30);



  2. #12
    Dirk's Avatar
    Dirk is offline Moderator
    Join Date
    May 2006
    Location
    England.
    Posts
    927
    Rep Power
    10

    Default And version 2

    Right, I've managed a newter version of the code, it has some screen feedback and its shorter.

    I'm done with this now, task achieved, I know the only zimbra centric part of this was a single line adding to the postfix conf, but still, I'd have struggled without that but of advice, thanks all.

    PHP Code:
    #!/usr/bin/perl -w

    use Net::POP3;
    # ----- Define email server and account -----
    my $pop3server "192.169.0.1";
    my $username "user";
    my $password "pass";
    # -------------------------------------------

    my $speechstring;

    while () {
        
    my $pop3 Net::POP3->new($pop3serverTimeout=>10Debug=>0) || die "Error : Couldn't log on to server";
        print 
    "Checking POP3 server.... ";

        
    my $pop3_msgcount $pop3->login($username$password);
        print 
    "Messages found: $pop3_msgcount \n";

        
    unless ($pop3_msgcount eq "0e0") {
            foreach 
    my $pop3_id (.. $pop3_msgcount) {
                
    $pop3_header $pop3 -> top($pop3_id0);
                
    my ($pop3_to$pop3_from$pop3_subject) = read_header($pop3_header);
                
    $speechstring "Message from: $pop3_from, for: $pop3_to, Subject is: $pop3_subject.\n";
                
    system "swift \"$speechstring\"";
                print 
    $speechstring;
                
    $pop3 -> delete ($pop3_id);
            }
        }
        
    $pop3 -> quit();
        print 
    "Will poll server in 30 seconds... \n";
        
    sleep (10);
        print 
    "20... \n";
        
    sleep (10);
        print 
    "10... \n";
        
    sleep (10);
    }

    sub read_header {
        
    my $header_array_ref shift;
        
    my $header join "", @$header_array_ref;

        
    my ($pop3_subject) = $header =~ /Subject: (.*)/m;

        
    my ($pop3_from) = $header =~ /From: \"(.*)\" </m;
        if (!
    $pop3_from) {
            (
    $pop3_from) = $header =~ /From: (.*) </m;
        }
        if (!
    $pop3_from) {
            (
    $pop3_from) = $header =~ /From: (.*)@/m;
        }
        if (!
    $pop3_from) {
            (
    $pop3_from) = "Unknown";
        }

        my (
    $pop3_to) = $header =~ /To: \"(.*)\" </m;
        if (!
    $pop3_to) {
            (
    $pop3_to) = $header =~ /To: (.*) </m;
        }
        if (!
    $pop3_to) {
            (
    $pop3_to) = $header =~ /To: <(.*)@/m;
        }
        if (!
    $pop3_to) {
            (
    $pop3_to) = $header =~ /To: (.*)@/m;
        }
        if (!
    $pop3_to) {
            (
    $pop3_to) = "Unknown";
        }

        return (
    $pop3_to$pop3_from$pop3_subject);

    Last edited by Dirk; 05-16-2007 at 03:42 AM.

  3. #13
    raj's Avatar
    raj
    raj is offline Moderator
    Join Date
    Oct 2005
    Location
    USA, Canada and India
    Posts
    777
    Rep Power
    10

    Default

    Cool..it was nice to talk about things which are FUN than usual admin stuff.

    Remember when you upgrade your postfix config will be overwitten..so u should add the always_bcc line again..

    Cheers..
    Raj

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •