<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/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-&gt;{channel};
    my $text = &lt;&lt;HTML;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;&lt;head&gt;
&lt;link rel="StyleSheet" href="http://www.perl.it/css/general.css" type="text/css"&gt;
&lt;link rel="StyleSheet" href="http://www.perl.it/css/widgets.css" type="text/css"&gt;
&lt;meta HTTP-EQUIV="Content-Type" content="text/html;charset=iso-8859-1"&gt;
&lt;meta name="description" content="Perl.it - Il punto di riferimento di Perl in Italia"&gt;
&lt;meta name="keywords" content="Perl programmazione"&gt;
&lt;meta name="rating" content="general"&gt;
&lt;meta name="robot" content="index,follow"&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div id="header"&gt;
&lt;a href="http://www.perl.it" target="_self"&gt;&lt;img src="http://www.perl.it/img/gui/perlitLogo.gif" width="149" height="39" alt="Perl.it Web Site"&gt;&lt;/a&gt;&lt;a name="top"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;div id="centercontent"&gt;
&lt;h3&gt;Novit&amp;agrave; dal canale &lt;a href="$$chn{link}"&gt;$$chn{title}&lt;/a&gt;, $$chn{description}&lt;/h3&gt;

&lt;ol&gt;
HTML

    # For each item, add a &lt;LI&gt; tag
    for my $item (@$news)
    {
        $text .= '&lt;li&gt; [' . $item-&gt;{'dc:subject'} . '] &lt;a href="' . $item-&gt;{link} . '"&gt;' . $item-&gt;{title} . '&lt;/a&gt; di &lt;b&gt;' . $item-&gt;{'dc:creator'} . '&lt;/b&gt; del ' . localtime(HTTP::Date::str2time($item-&gt;{'dc:date'})) . "&lt;br/&gt;\n" .
                 '&lt;blockquote&gt;' . $item-&gt;{description} . "&lt;br/&gt;\n" .
                 '&lt;p&gt;URL: &lt;a href="' . $item-&gt;{link} . '"&gt;' . $item-&gt;{title} . '&lt;/a&gt; - &lt;code&gt;&lt;a href="' . $item-&gt;{link} . '"&gt;' . $item-&gt;{link} . '&lt;/a&gt;&lt;/code&gt;&lt;/p&gt;' .
                 '&lt;/blockquote&gt;&lt;/li&gt;' . "\n";
    }

    # Close html document
    $text .= '&lt;/div&gt;&lt;/ol&gt;&lt;/body&gt;&lt;/html&gt;';

    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-&gt;send('smtp', 'mail.ebravo.it', Timeout=&gt;30);
    my $msg = MIME::Lite-&gt;new(
        From     =&gt; $from,
        To       =&gt; $rcpt,
        Subject  =&gt; $subj,
        Type     =&gt;'text/html',
        Data     =&gt; $text,
    );
    $msg-&gt;send();
}

Getopt::Long::GetOptions(
    'url:s'    =&gt; \my $url,
    'from:s'   =&gt; \my $from,
    'rcpt:s'   =&gt; \my $rcpt,
    'subject:s'=&gt; \my $subj,
) or exit(1);

$url  ||= 'http://www.perl.it/blog/digest.rdf';
$from ||= 'Cosimo Streppone &lt;cosimo@streppone.it&gt;';
$rcpt ||= 'Cosimo Streppone &lt;cosimo@streppone.it&gt;';
$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} &amp;&amp; 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 &lt;myself@mydomain.com&gt;'
              --subject 'My news'
              --rcpt 'My Friends &lt;friends@theirdomain.com&gt;'

=head1 AUTHOR

Yes, the author of this awesome script is Cosimo :-)

=cut

</pre></body></html>