root/trunk/plagger/lib/Plagger/Plugin/Publish/Gmail.pm

Revision 31 (checked in by miyagawa, 15 years ago)

* Publish::JavaScript? to create .js include file per feed.
* Better error handling in templates

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Publish::Gmail;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 our $VERSION = '0.10';
6
7 use DateTime;
8 use DateTime::Format::Mail;
9 use Encode;
10 use Encode::MIME::Header;
11 use MIME::Lite;
12
13 sub register {
14     my($self, $context) = @_;
15     $context->register_hook(
16         $self,
17         'publish.notify' => \&notify,
18     );
19 }
20
21 sub notify {
22     my($self, $context, $feed) = @_;
23
24     my @items = $feed->entries;
25     if ($self->conf->{group_items}) {
26         $self->send_email_feed($context, $feed, \@items);
27     } else {
28         for my $item (@items) {
29             $self->send_email_item($context, $feed, $item);
30         }
31     }
32 }
33
34 sub send_email_feed {
35     my($self, $context, $feed, $items) = @_;
36     my $subject = $feed->title || '(no-title)';
37     my $body = join '<hr />', map $self->templatize($context, $feed, $_), @$items;
38     $self->do_send_mail($context, $feed, $subject, $body);
39 }
40
41 sub send_email_item {
42     my($self, $context, $feed, $item) = @_;
43     my $subject = $item->title || '(no-title)';
44     my $body    = $self->templatize($context, $feed, $item);
45     $self->do_send_mail($context, $feed, $subject, $body);
46 }
47
48 sub do_send_mail {
49     my($self, $context, $feed, $subject, $body) = @_;
50
51     $body = $self->htmlize($body);
52
53     my $cfg = $self->conf;
54     $context->log(warn => "Sending $subject to $cfg->{mailto}");
55
56     my $feed_title = $feed->title;
57        $feed_title =~ tr/,//d;
58
59     my $now = Plagger::Date->now(timezone => $context->conf->{timezone});
60
61     my $msg = MIME::Lite->new(
62         Date => $now->format('Mail'),
63         From => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)),
64         To   => $cfg->{mailto},
65         Subject => encode('MIME-Header', $subject),
66         Type => 'multipart/related',
67     );
68     $msg->attach(
69         Type => 'text/html; charset=utf-8',
70         Data => encode("utf-8", $body),
71     );
72
73     my $route = $cfg->{mailroute} || { via => 'smtp', host => 'localhost' };
74     my @args  = $route->{host} ? ($route->{host}) : ();
75     $msg->send($route->{via}, @args);
76 }
77
78 sub htmlize {
79     my($self, $body) = @_;
80     return <<HTML;
81 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
82 <html>
83 <head>
84 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
85 </head>
86 <body>
87 $body
88 </body>
89 </html>
90 HTML
91 }
92
93 sub templatize {
94     my($self, $context, $feed, $item) = @_;
95     my $tt = $context->template();
96     $tt->process('gmail_notify.tt', {
97         feed => $feed,
98         item => $item,
99         cfg  => $self->conf,
100     }, \my $out) or $context->error($tt->error);
101     $out;
102 }
103
104 1;
Note: See TracBrowser for help on using the browser.