Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add Kamui::Mail

  • Loading branch information...
commit 0c4d40c8869f44c7582677c35f932bc50f260b91 1 parent 9771787
@nekokak authored
View
122 lib/Kamui/Mail.pm
@@ -0,0 +1,122 @@
+package Kamui::Mail;
+use strict;
+use warnings;
+use MIME::Lite::TT::Japanese;
+use File::Slurp;
+use Template;
+use Encode qw//;
+use Template::Provider::Encoding;
+
+sub new {
+ my ($class, $tmpl_name, $args) = @_;
+
+ my $params;
+
+ $params->{conf} = delete $args->{conf};
+ unless ($params->{conf}) {
+ die "no conf error!";
+ }
+
+ $args->{LineWidth} ||= 0;
+ $params->{from} = $args->{From};
+ $params->{return_path} = $args->{'Return-Path'};
+
+ $params->{mail} = MIME::Lite::TT::Japanese->new(
+ %$args,
+ %{$class->get_mail_template($params->{conf}, $tmpl_name, $args->{TmplParams})},
+ TmplOptions => {
+ ABSOLUTE => 1,
+ RELATIVE => 1,
+ ENCODING => 'utf-8',
+ FILTERS => $params->{conf}->{view}->{tt}->{filters},
+ COMPILE_DIR => '/tmp/' . $ENV{USER} . "/",
+ LOAD_TEMPLATES => [ Template::Provider::Encoding->new ],
+ },
+ 'X-User-Agent' => $ENV{HTTP_USER_AGENT} || 'unknown',
+ );
+ bless $params, $class;
+}
+
+sub attach {
+ my ($self, $attach_data) = @_;
+ $self->{mail}->attach( %$attach_data );
+}
+
+sub send {
+ my $self = shift;
+
+ if ($self->{conf}->{debug}) {
+ my $output = $self->{mail}->as_string;
+ Encode::from_to($output,'jis','utf8');
+ print STDERR $output;
+ } else {
+ $self->{mail}->send(
+ 'sendmail',
+ SetSender => 1,
+ FromSender => ($self->{return_path} ? $self->{return_path} : $self->{from}),
+ );
+ }
+
+ $self->{send}=1;
+}
+
+sub get_mail_template {
+ my ($class, $conf, $file_name, $args) = @_;
+
+ my $src = read_file($conf->{view}->{tt}->{path}.'/mail/'.$file_name);
+ if ($src =~ /\A([^\n]+)\n\n(.*)\Z/ms) {
+ my $subject = $class->_get_subject($conf, $1, $args);
+ my $body = $2;
+ return {
+ Subject => $subject,
+ Template => \$body,
+ };
+ } else {
+ die "Invalid mail template. No subject!";
+ }
+}
+
+sub _get_subject {
+ my ($class, $conf, $subject, $args) = @_;
+
+ my $tt = Template->new(
+ ABSOLUTE => 1,
+ RELATIVE => 1,
+ ENCODING => 'utf-8',
+ FILTERS => $conf->{view}->{tt}->{filters},
+ COMPILE_DIR => '/tmp/' . $ENV{USER} . "/",
+ LOAD_TEMPLATES => [ Template::Provider::Encoding->new ],
+ );
+ $tt->process(\$subject, $args ,\my $output) or die $@;
+ $output;
+}
+
+sub DESTROY {
+ my $self = shift;
+ unless ($self->{send}) {
+ my $class = ref $self;
+ die "$class $self destroyed without send mail.";
+ }
+}
+
+1;
+
+=head1 NAME
+
+Kamui::Mail - MIME::Lite::TT::Japanese with Kamui.
+
+=head1 SYNOPSIS
+
+ use Kamui::Mail;
+ Kamui::Mail->new(
+ 'test.eml', => {
+ From => 'nekokak@gmail.com',
+ To => 'nekokak@nekokak.org',
+ TmplParams => +{
+ subject_param => 'nekokak からだよ',
+ body_param => 'nekokak にだよ',
+ },
+ conf => container('conf'),
+ }
+ )->send;
+
View
3  t/assets/tmpl/mail/test.eml
@@ -0,0 +1,3 @@
+[test] [% subject_param %] テストメールですよ
+
+[% body_param %]あいうえお
View
2  t/config.pl
@@ -42,5 +42,7 @@
base_dir => 't/assets/',
};
+$conf->{debug} = 1;
+
return $conf;
View
74 t/mail.t
@@ -0,0 +1,74 @@
+use t::Utils;
+use Test::More;
+use Test::Exception;
+use IO::Scalar;
+use Email::MIME::JP::Mobile;
+use Kamui::Mail;
+use Mock::Container;
+
+subtest 'no conf case' => sub {
+ dies_ok(sub{
+ Kamui::Mail->new(
+ 'test.eml', => {
+ From => 'nekokak@gmail.com',
+ To => 'kobayashi@con-course.com',
+ TmplParams => +{
+ subject_param => 'nekokak からだよ',
+ body_param => 'nekokak にだよ',
+ },
+ }
+ );
+ });
+ done_testing;
+};
+
+subtest 'mail send' => sub {
+ use Email::MIME::JP::Mobile;
+
+ tie *STDERR, 'IO::Scalar', \my $out;
+
+ Kamui::Mail->new(
+ 'test.eml', => {
+ From => 'nekokak@gmail.com',
+ To => 'kobayashi@con-course.com',
+ TmplParams => +{
+ subject_param => 'nekokak からだよ',
+ body_param => 'nekokak にだよ',
+ },
+ conf => container('conf'),
+ }
+ )->send;
+
+ untie *STDERR;
+
+ my $mail = Email::MIME::JP::Mobile->new($out);
+ is $mail->subject, '[test] nekokak からだよ テストメールですよ';
+ is $mail->body, "nekokak にだよあいうえお\n";
+
+ done_testing;
+};
+
+subtest 'mail no send' => sub {
+
+ dies_ok(
+ sub {
+ my $mail = Kamui::Mail->new(
+ 'test.eml', => {
+ From => 'nekokak@gmail.com',
+ To => 'kobayashi@con-course.com',
+ TmplParams => +{
+ subject_param => 'nekokak からだよ',
+ body_param => 'nekokak にだよ',
+ },
+ conf => container('conf'),
+ }
+ );
+ $mail = undef;
+ }
+ );
+
+ done_testing;
+};
+
+done_testing;
+
Please sign in to comment.
Something went wrong with that request. Please try again.