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";
}