# -*-Perl-*- ####################################################################### ### $Id: sendmail.txt,v 1.6 2006/06/03 13:01:43 mate Exp $ ### Required bloglines-0.80 later package sendmail; use strict; use Net::SMTP; use Jcode; use Time::Local; ####################################################################### # CONFIGURATION # メール件名 my $MAIL_SUBJECT = '[bloglines] 新着記事 ($total)'; # メール送信先 my $MAIL_TO = ''; # メール FROM my $MAIL_FROM = ''; # メールサーバ my $MAIL_SERVER = 'localhost'; ####################################################################### $MAIL_SUBJECT = $Bloglines::MAIL_SUBJECT if defined $Bloglines::MAIL_SUBJECT; $MAIL_TO = $Bloglines::MAIL_TO if defined $Bloglines::MAIL_TO; $MAIL_FROM = $Bloglines::MAIL_FROM if defined $Bloglines::MAIL_FROM; $MAIL_SERVER = $Bloglines::MAIL_SERVER if defined $Bloglines::MAIL_SERVER; my ($MAIL_LOOP, %TEMPLATE, %HEADER, %FOOTER); my %MONTH = (Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11); sub setup { push @Bloglines::OPTIONS, "sendmail"; $HEADER{total} = 0; 1; } sub filter { return unless exists $Bloglines::OPTCTL{sendmail}; my ($pkg, $file, $rss) = @_; load_template(\%TEMPLATE); foreach my $site (@{$rss||[]}) { my $i = 0; my $loop = ""; $site->{siteid} = $site->{'bloglines:siteid'}; foreach my $item (@{$site->{items}}) { $item->{i} = ++$i; $item->{itemid} = $item->{'bloglines:itemid'}; $item->{date} = gmt2jst($item->{pubDate}) if exists $item->{pubDate}; $item->{creator} = $item->{'dc:creator'} if exists $item->{'dc:creator'}; $loop .= parse_template($TEMPLATE{ITEMS}, $item); } $site->{total} = $i; $HEADER{total} += $i; $MAIL_LOOP .= parse_template($TEMPLATE{CHANNEL}, $site).$loop; } if (defined $MAIL_LOOP && open(F, ">$file.mail")) { print F $MAIL_LOOP; close(F); } } sub teardown { return if $HEADER{total} < 1; $MAIL_SUBJECT = parse_template($MAIL_SUBJECT, \%HEADER); $HEADER{to} = Jcode->new($MAIL_TO)->mime_encode; $HEADER{from} = Jcode->new($MAIL_FROM)->mime_encode; $HEADER{subject} = Jcode->new($MAIL_SUBJECT)->mime_encode; $HEADER{version} = $Bloglines::VERSION; my $head = parse_template($TEMPLATE{HEADER}, \%HEADER); my $foot = parse_template($TEMPLATE{FOOTER}, \%FOOTER); my $smtp = Net::SMTP->new($MAIL_SERVER) or die "Can't open server: $MAIL_SERVER"; $smtp->mail($HEADER{from}) or die "From: ".$smtp->message; $smtp->to($HEADER{to}) or die "To: ".$smtp->message; $smtp->data() or die "->data(): ".$smtp->message; $smtp->datasend($head.$MAIL_LOOP.$foot) or die "->datasend(): ".$smtp->message; $smtp->dataend() or die "->dataend(): ".$smtp->message; $smtp->quit; } # Fri, 2 Jun 2006 01:02:03 GMT -> Fri Jun 2 10:02:03 2006 JST sub gmt2jst { my ($wday, $day, $mon, $year, $hour, $min, $sec, $gmt) = split(/[,:\s]+/, $_[0]); return $_[0] unless defined $gmt && $gmt eq 'GMT'; return scalar localtime(timelocal($sec, $min, $hour, $day, $MONTH{$mon}, $year)+32400).' JST'; } sub load_template { my ($tmplref) = @_; my ($group); while () { last if (/__END__/); if (/^#+\s+(\w+)/) { $group = $1; next; } $tmplref->{$group} .= $_; } } sub parse_template { my ($tmpl, $datref) = @_; if (ref($datref) eq 'HASH') { if ($tmpl =~ /\$\w+/) { $tmpl =~ s/\$(\w+)/defined($$datref{$1}) ? $$datref{$1} : ""/eg; } } elsif (ref($datref) eq 'ARRAY') { my $t = ""; foreach (@$datref) { $t .= parse_template($tmpl, $_); } $tmpl = $t; } $tmpl =~ s|\n[\s\t]+\n|\n\n|g if ($tmpl =~ /\n[\s\t]+\n/); $tmpl =~ s|\n\n\n+|\n\n|g if ($tmpl =~ /\n\n\n/); return $tmpl; } 1; __DATA__ ## HEADER To: $to From: $from Subject: $subject X-Mailer: bloglines/$version ## CHANNEL <$title> ($total) $link http://www.bloglines.com/userdir?siteid=$siteid ========================================================================= ## ITEMS [$i] $title $link $date $creator $description ------------------------------------------------------------------------- ## FOOTER ========================================================================= __END__ ### Local Variables: ### mode: perl ### End: =head1 NAME sendmail.pl =head1 SYNOPSIS =head1 DESCRIPTION =head1 AUTHOR Yutaka Kojima Eyutaka@asmate.netE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The updated version is available from http://www.asmate.net/software/perl/bloglines/plugins/ =cut