Created
February 18, 2010 01:36
-
-
Save kan/307231 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MoFedge::Mail; | |
use strict; | |
use warnings; | |
use base qw/MoFedge Class::Accessor::Fast Class::Data::Inheritable/; | |
use MIME::Lite::TT::Japanese; | |
use File::Slurp; | |
use Template; | |
use Carp; | |
use Template::Provider::Encoding; | |
__PACKAGE__->mk_accessors(qw/mail from return_path/); | |
__PACKAGE__->mk_classdata('force_utf8' => 0); | |
sub new { | |
my ($class, $tmpl_name, $args) = @_; | |
my $params; | |
$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($tmpl_name,$args->{TmplParams})}, | |
($class->force_utf8 ? ('TmplOptions' => { LOAD_TEMPLATES => [ Template::Provider::Encoding->new ]}) : ()), | |
'X-User-Agent' => $ENV{HTTP_USER_AGENT} || 'unknown', | |
); | |
bless $params, $class; | |
} | |
sub attach { | |
my $self = shift; | |
my $attach_data = shift; | |
$self->mail->attach( %$attach_data ); | |
} | |
sub send { | |
my $self = shift; | |
if ($self->debug_level) { | |
my $output = $self->mail->as_string; | |
if ( $self->force_utf8 ) { | |
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 $self = shift; | |
my $file_name = shift; | |
my $args = shift; | |
my $src = read_file($self->config->tmpl_path . '/' . $self->get_dirname_by_classname . "/$file_name"); | |
if ($src =~ /\A([^\n]+)\n\n(.*)\Z/ms) { | |
my $subject = $self->_get_subject($1,$args); | |
my $body = $2; | |
return { | |
Subject => $subject, | |
Template => \$body, | |
}; | |
} else { | |
die "Invalid mail template. No subject!"; | |
} | |
} | |
sub _get_subject { | |
my ($self, $subject, $args) = @_; | |
my $tt = Template->new($self->force_utf8 ? (LOAD_TEMPLATES => [ Template::Provider::Encoding->new ]) : ()); | |
$tt->process(\$subject, $args ,\my $output) or die $@; | |
return $output; | |
} | |
sub DESTROY { | |
my $self = shift; | |
unless ($self->{send}) { | |
my $class = ref $self; | |
Carp::carp("$class $self destroyed without send mail."); | |
} | |
} | |
1; | |
=head1 NAME | |
MoFedge::Mail - MIME::Lite::TT::Japanese with MoFedge. | |
=head1 SYNOPSIS | |
package Your::Mail; | |
use base 'MoFedge::Mail'; | |
package Your::Pages; | |
use Your::Mail; | |
sub dispatch_hoge { | |
my $self = shift; | |
my $mail = Your::Mail->new('template_name',$tmpl_params); | |
$mail->send; | |
# or | |
my $mail = Your::Mail->new('template_name',$tmpl_params)->send; | |
} | |
=head1 AUTHOR | |
Atsushi Kobayashi, C<< <nekokak at gmail> >> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment