#!/usr/bin/env perl
#---------------------------------------------
# rss2mail.pl
# see pod documentation at the end
#
#---------------------------------------------
# $Id: $
use strict;
use warnings;
use Digest::MD5 ();
use Getopt::Long ();
use HTTP::Date ();
use LWP::Simple ();
use MIME::Lite ();
use XML::RSSLite ();
sub get_page
{
my $url = $_[0];
return LWP::Simple::get($url);
}
sub prepare_mail_body
{
my($feed, $news) = @_;
my $chn = $feed->{channel};
my $text = <
Novità dal canale $$chn{title}, $$chn{description}
';
return($text);
}
sub read_rss
{
my $feed = $_[0];
my %result;
XML::RSSLite::parseRSS(\%result, \$feed);
return(%result);
}
sub send_email
{
my($from, $rcpt, $subj, $text) = @_;
MIME::Lite->send('smtp', 'mail.ebravo.it', Timeout=>30);
my $msg = MIME::Lite->new(
From => $from,
To => $rcpt,
Subject => $subj,
Type =>'text/html',
Data => $text,
);
$msg->send();
}
Getopt::Long::GetOptions(
'url:s' => \my $url,
'from:s' => \my $from,
'rcpt:s' => \my $rcpt,
'subject:s'=> \my $subj,
) or exit(1);
$url ||= 'http://www.perl.it/blog/digest.rdf';
$from ||= 'Cosimo Streppone ';
$rcpt ||= 'Cosimo Streppone ';
$subj ||= '[TEST] Perl.it blog news';
my %feed;
my @news;
my $content;
my $text;
# Get url or exit with status 2
exit(2) unless $content = get_page($url);
# Parse RSS feed
%feed = read_rss($content);
# Check if we have posts to show
if( $feed{item} && ref($feed{item}) eq 'ARRAY')
{
@news = @{$feed{item}};
}
# No news, no party... :-)
if( ! @news )
{
print 'No news...', "\n";
exit(3);
}
# Prepare email to be sent
$text = prepare_mail_body(\%feed, \@news);
send_email( $from, $rcpt, $subj, $text );
#
# End of script
=pod
=head1 NAME
rss2mail.pl
=head1 DESCRIPTION
A simple script that downloads an RSS/RDF file, parses it and
sends an email message with the RSS feed summary.
The template of email is taken from http://www.perl.it site.
=head1 SYNOPSIS
./rss2mail.pl --url http://www.mysite.com/recent.rdf
--from 'Myself '
--subject 'My news'
--rcpt 'My Friends '
=head1 AUTHOR
Yes, the author of this awesome script is Cosimo :-)
=cut