package Feed::HelperRole::Mail;
use Moose::Role;
use 5.012;
use namespace::autoclean;
use Encode;
use Encode::MIME::Header;
use MIME::Lite;
use DateTime::Format::Mail;
use Digest::SHA1 qw/ sha1_hex /;
use Template;
use Template::Provider::Encoding;
use Template::Stash::ForceUTF8;
use Moose::Util::TypeConstraints;
use Try::Tiny;
subtype 'ArrayOfStr', as 'ArrayRef[Str]';
coerce 'ArrayOfStr', from 'Str', via { [ $_ ] };
has _mail_folders => (
is => 'ro',
isa => 'ArrayOfStr',
coerce => 1,
default => sub { [] },
traits => [ 'Array' ],
init_arg => 'mail_folders',
handles => {
mail_folders => 'elements',
}
);
has tt => (
is => 'ro',
isa => 'Template',
lazy_build => 1,
);
has template => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
has date_formatter => (
is => 'ro',
lazy_build => 1,
);
sub _build_tt {
my ($self) = @_;
return Template->new(
LOAD_TEMPLATES => [ Template::Provider::Encoding->new ],
STASH => Template::Stash::ForceUTF8->new,
);
}
sub _build_template {
my ($self) = @_;
my $curpos = tell(DATA);
binmode(DATA);
my $template = do {local $/;<DATA>};
seek(DATA,$curpos,0);
return $template;
}
sub _build_date_formatter {
return DateTime::Format::Mail->new();
}
sub entry_to_mime {
my ($self,$entry) = @_;
my $from = 'feeder@localhost';
my $date = $entry->modified // $entry->issued
// DateTime->now(time_zone=>'UTC');
my $from_name = $entry->author // $self->title;
try { $from_name = decode('utf-8',$from_name) };
$from_name =~ tr/,//d;
my $subject = $entry->title//'(no title)';
try { $subject = decode('utf-8',$subject) };
my $id = sha1_hex($entry->id // $entry->link);
my $body = $self->prepare_body($entry);
my $msg = MIME::Lite->new(
Date => $self->date_formatter->format_datetime($date),
From => encode('MIME-Header',qq{"$from_name" <$from>}),
To => $from,
Subject => encode('MIME-Header',$subject),
Type => 'multipart/related',
);
$msg->attach(
Type => 'text/html; charset=utf-8',
Data => encode('utf-8',$body),
Encoding => 'quoted-printable',
);
$msg->add('Message-Id', "<$id.feeder\@localhost>");
my @tags = $entry->tags;
try { @tags = map { decode('utf-8',$_) } @tags };
$msg->add('X-Tags', encode('MIME-Header', join(' ', @tags )));
return $msg;
}
sub prepare_body {
my ($self,$entry) = @_;
my $template=$self->template;
my $out;
$self->tt->process(
\$template,
{
feeder => $self,
feed => $self->feed,
entry => $entry,
content => $entry->content,
},
\$out,
)
or die $self->tt->error;
return $out;
}
1;
__DATA__
[% USE encoding 'utf-8' -%]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style TYPE=text/css>
body { padding:0; margin:20px }
strong { font-weight:bold; font-size:1.2em }
div
div
div
</style>
</head>
<body>
<div id="msgheader">
[% SET link = entry.link || entry.id -%]
<a href="[% link | html %]"><strong>[% entry.title %]</strong></a><br />
[% feed.title %]<br />
[% IF entry.author %]
by [% entry.author | html %]
[% END %]
[% IF entry.tags.size %]
on [% entry.tags.join(',') %]
[% END %]
</div>
<div id="msgbody">
[% IF content.body -%]
[% IF content.body.match('(?i)^<p[ >]') %]
[% content.body %]
[% ELSE %]
<div id="msgbody">[% content.body %]</div>
[% END %]
[% ELSE %]
<br />
[% END %]
</div>
<div id="msgfooter">
[% IF entry.issued %]
Posted on [% feeder.date_formatter.format_datetime(entry.issued) %]
[% END %]
[% IF entry.modified %]
Modified on [% feeder.date_formatter.format_datetime(entry.modified) %]
[% END %]
|
<a href="[% entry.link | html %]">permalink</a>
|
<a href="[% feed.link | html %]">[% feed.title | html %]</a>
<br clear="all" />
</div>
</body>
</html>