#!/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 = <<HTML;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<link rel="StyleSheet" href="
http://www.perl.it/css/general.css" type="text/css">
<link rel="StyleSheet" href="
http://www.perl.it/css/widgets.css" type="text/css">
<meta HTTP-EQUIV="Content-Type" content="text/html;charset=iso-8859-1">
<meta name="description" content="Perl.it - Il punto di riferimento di Perl in Italia">
<meta name="keywords" content="Perl programmazione">
<meta name="rating" content="general">
<meta name="robot" content="index,follow">
</head>
<body>
<div id="header">
<a href="
http://www.perl.it" target="_self"><img src="
http://www.perl.it/img/gui/perlitLogo.gif" width="149" height="39" alt="Perl.it Web Site"></a><a name="top"></a>
</div>
<div id="centercontent">
<h3>Novità dal canale <a href="$$chn{link}">$$chn{title}</a>, $$chn{description}</h3>
<ol>
HTML
# For each item, add a <LI> tag
for my $item (@$news)
{
$text .= '<li> [' . $item->{'dc:subject'} . '] <a href="' . $item->{link} . '">' . $item->{title} . '</a> di <b>' . $item->{'dc:creator'} . '</b> del ' . localtime(HTTP::Date::str2time($item->{'dc:date'})) . "<br/>\n" .
'<blockquote>' . $item->{description} . "<br/>\n" .
'<p>URL: <a href="' . $item->{link} . '">' . $item->{title} . '</a> - <code><a href="' . $item->{link} . '">' . $item->{link} . '</a></code></p>' .
'</blockquote></li>' . "\n";
}
# Close html document
$text .= '</div></ol></body></html>';
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 <
[email protected]>';
$rcpt ||= 'Cosimo Streppone <
[email protected]>';
$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 <
[email protected]>'
--subject 'My news'
--rcpt 'My Friends <
[email protected]>'
=head1 AUTHOR
Yes, the author of this awesome script is Cosimo :-)
=cut