Topic: pop email script for conky

Hello everyone,

here is a little email script for conky, installation instrutions are in comments at the top of the script. The script is written in perl and is a little less efficient that it could be because I decided to store emails in a hash in case I wanted to operate on them later.

I know there are a thousand scripts out there to do this exact thing (including a conky module), the main benefit of this script is if you want to learn perl, or just get your hands dirty instead of downloading a binary.

#!/usr/bin/perl
# File: conkyemail.pl
# Author: bezlbum

#TODO Change $user and $pass to email username and password
#TODO change host to email host
#TODO You can also change $numEmailsToPrint

#TODO You must install perl mail module,
#from command line run:

#    "sudo cpan"

# Inside the cpan shell run:

#    install Mail::POP3Client

use Mail::POP3Client;
use strict;

##########
###INIT###
##########

my $user = "username";
my $pass = "password";
my $host = "pop.gmail.com";
my $numEmailsToPrint = 3;

#connect to steds, for checking mail
my $popEmail = new Mail::POP3Client(
            USER    => $user,
            PASSWORD => $pass,
            HOST     => $host,
               USESSL   => "true" ) or die "Could not connect to $host email $!\n";

#store new mail from stedwards
my %recv_mail;

###################
###SUBROUTINES#####
###################

sub getemail() {
    #get number of emails to check
    my $num_emails_to_print = pop();

    #setup email vars
    my $numEmails = $popEmail->Count();
    my $from;
    my $subject;
    my $body;

    my $count = 0;
    for (my $i = $numEmails; $i >= ($numEmails - $num_emails_to_print); $i--) {
        foreach ( $popEmail->Head( $i+1 ) ) {
            $count++;
            #get who the email is from
            if( $_ =~ /^(From:)\s+(.*)/i ) {
                $from = $2;
            }
            #get the subject email line
            elsif( $_ =~ /^(Subject:)\s+(.*)/i ) {
                $subject = $2;
            }
            #when we have the subject,from, and body; then we ship out the info
            if( ($from && $subject) || ($from && ($count == 2)) || ($subject && ($count == 2)) ) {
                #this whole part could just be printing the email, but I wanted to store it instead
                #into a hash incase I create future methods to mess with the emails
                $recv_mail{$i}{'from'} = $from;
                $recv_mail{$i}{'subject'} = $subject;
                $recv_mail{$i}{'body'} = substr($popEmail->Body($i + 1), 0, 100);
                #clear out subject and from making them false
                $from = 0;
                $subject = 0;
                $body = "";
                $count = 0;
            }
        }
    }
    #close the email object
    $popEmail->close();
}

##########
###MAIN###
##########

&getemail($numEmailsToPrint);

#print for conky
for my $key (keys %recv_mail) {
    print $recv_mail{$key}{'from'} ."\n";
    print $recv_mail{$key}{'subject'} ."\n\n";
}

Re: pop email script for conky

this is great! I've been looking for something like this!

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

@crunchy, thanks! we always seem to get along online smile. If you have any questions or are interested in learning perl, let me know!

Re: pop email script for conky

I've actually been working on learning perl for the last 2 months or so! lol

now that I start learning perl I realize how much it can do and how much people use it. I had never seen a perl script before 2 months ago. smile

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

What made you choose perl over python or ruby?

I chose perl because I'm interested in becoming a sysadmin, and I love the regex support in perl (e.g. the =~ operator makes me feel like I can do anything!).

But, I do have a bit of a problem with oo perl, although I have not messed around with Moose. Which is why I have thought about picking up python or ruby. Also, have you come across Higher Order Perl yet?

Re: pop email script for conky

I guess that I chose perl because I've also worked a little bit in c and the syntax was similar enough but I could also incorporate bash commands. hmm

I've looked at python and found it confusing so. . .

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

Yeah, running shell commands in perl with backticks are awesome!

[edit] btw, @crunchy, please let me know if you make any improvements to the script. I love collaboration.

Last edited by bezlbum (2011-04-06 05:36:34)

Re: pop email script for conky

okay. I give up. I guess that I need help with cpan. my own user account runs fine but I can't install install Mail::POP3Client through cpan because it keeps asking me questions that I have no clue how to answer. sad

is there a way for it to auto install (I accidently made it manual sad ) again?

EDIT never mind figured it out. big_smile unfortunately it still doesn't show anything on my conky. sad

Last edited by crunchy (2011-04-06 06:23:24)

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

DId you update the username, password, and mail host. Also (if you are using gmail) you may have to enable pop.

Try to run the script in the command line and see if you get any output.

[Edit] what mail host are you using?

Last edited by bezlbum (2011-04-06 06:31:18)

Re: pop email script for conky

I'm using gmail I ran it from the command line and it showed nothing. sad

EDIT: I also enabled pop

Last edited by crunchy (2011-04-06 06:33:38)

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

all I can say is make sure that your username and password are correct, the script is executable, and the number of emails is set to above 0.

Also if your gmail username or password has any perl special characters (like @,#,$) you will want to change the

$user = "$user"
$pass = "p@ss"

to single quotes around the username and password , or perl might have some problems

[EDIT] is perl reporting any errors?

Last edited by bezlbum (2011-04-06 06:38:22)

Re: pop email script for conky

my username and password doesn't have any except for my username has a "." and my pass has some spaces.

when I type in ./conkyemail.pl it gives me:

         

big_smile

oh, and by the way the problem might be this: in my conky I'm using: "${execi 5 ~/conkyemail.pl}"

I know that the update interval is insane.

Last edited by crunchy (2011-04-06 06:42:36)

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

well I would change the double quotes to single quotes anyway.

Also add "use warnings;" to the beginning of the script and run it. This might help us debug the problem.

Re: pop email script for conky

no warnings sadly sad I changed it to single quotes too.

so just to make absolutely sure. host should be "pop.gmail.com"

I'm really stumped about this.

Last edited by crunchy (2011-04-06 06:45:18)

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

Haha, well this is getting ridiculous, because I have the script working running right now.

The only other thing I can think is, does your email account have any emails? and are you sure that pop is enabled?

Also, try to make sure the script is trying to connect by adding a println after this part:
my $popEmail = new Mail::POP3Client(
            USER    => $user,
            PASSWORD => $pass,
            HOST     => $host,
               USESSL   => "true" ) or die "Could not connect to $host email $!\n";

e.g. print "got here\n";

Re: pop email script for conky

yeah, it should be pop.gmail.com, I have a gmail account and it works. I have also used this script for my college email account, which is pop3.

Re: pop email script for conky

Honestly, double check the username and password. When I change my password to something incorrect, no errors are printed out. I wish it would print out errors, but Mail::POP3Client does not give me anything to print.

Re: pop email script for conky

pop is enabled, my inbox has about 100 emails in it one of which are unread (it's messy I know)

I put the print "got here\n"; after that and it printed it out in the terminal and on conky.

this is getting ridiculous! since you have your script running I don't think that it's the script. I have pop enabled so it isn't that. what line are you using in your conky?

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

well if it does not print when you run the script in the command line, then it won't print in conky...

but here is the line I'm using:
${execi 60 "/home/bezlbum/bin/stedsemail.pl"}

You may want to use the full path of the script instead of ~

Re: pop email script for conky

my email and password are correct just copied and pasted into gmail sign-in from the perl script

another thing that could be causing it. my email account is "blah here"."more blah"@gmail.com

I obviously don't want to give out my gmail account publically.

in the perl script should I be putting the @gmail.com part in?

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

no, you dont need to put the @gmail part.

if your username is blah.blah then the script should read

$user='blah.blah';

Re: pop email script for conky

okay, I'm going to give up for a while were I am it's 11:00 at night.

I will try again tomorrow with a fresher brain!

thank you so much for you sharing the script and for your help! smile

If I get it working then I'll post and tell you how I did it.

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Re: pop email script for conky

before i go to far and get this script working, will this delete messages from my server?

i'm in the same boat as crunchy, i did default install with CPAN (was that incorrect?) and have the script in home/bin folder.  do i need some other perl stuff installed or anything else?