| 1 | package Catalyst::Plugin::Email::Japanese; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Catalyst::Exception; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use UNIVERSAL::require | ||
| 7 | |||
| 8 | 0 | ▶ | our $VERSION = '0.07'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Catalyst::Plugin::Email::Japanese - Send Japanese emails with Catalyst | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Catalyst qw/Email::Japanese/; | ||
| 17 | |||
| 18 | # config base parameters | ||
| 19 | __PACKAGE__->config( | ||
| 20 | email => { | ||
| 21 | Template => 'email.tt', | ||
| 22 | From => 'typester@cpan.org', | ||
| 23 | } | ||
| 24 | ); | ||
| 25 | |||
| 26 | # and later in your controller | ||
| 27 | $c->email( | ||
| 28 | To => 'example@example.com', | ||
| 29 | Subject => 'Hi!', | ||
| 30 | ); | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Send emails with Catalyst and L<MIME::Lite::TT::Japanese>. | ||
| 35 | |||
| 36 | =head1 ForceUTF8 MODE | ||
| 37 | |||
| 38 | If $c->config->{ForceUTF8} or $c->config->{email}->{ForceUTF8} is true value, | ||
| 39 | this module use L<Template::Provider::Encoding> and L<Template::Stash::ForceUTF8> for correct utf-8 handling. | ||
| 40 | |||
| 41 | Please see these module's docs for detail. | ||
| 42 | |||
| 43 | =head1 HTML MAIL SUPPORT | ||
| 44 | |||
| 45 | If Template parameter is hash ref like below: | ||
| 46 | |||
| 47 | $c->config->{email} = { | ||
| 48 | Template => { | ||
| 49 | html => 'html.tt', | ||
| 50 | text => 'text.tt', | ||
| 51 | }, | ||
| 52 | }; | ||
| 53 | |||
| 54 | then this module use L<MIME::Lite::TT::HTML::Japanese> instead of L<MIME::Lite::TT::Japanese>. | ||
| 55 | |||
| 56 | This is useful for sending html mails. | ||
| 57 | |||
| 58 | =head1 METHODS | ||
| 59 | |||
| 60 | =head2 email( %args ) | ||
| 61 | |||
| 62 | Send email with MIME::Lite::TT::(HTML::)Japanese. | ||
| 63 | |||
| 64 | %args and $c->config->{emal} is MIME::Lite::TT::(HTML::)Japanese's parameters, and %args override latter. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub email { | ||
| 69 | my $c = shift; | ||
| 70 | my $args = $_[1] ? {@_} : $_[0]; | ||
| 71 | |||
| 72 | my $template = $args->{Template} || $c->stash->{email}->{template} || $c->config->{email}->{Template}; | ||
| 73 | |||
| 74 | my $module = | ||
| 75 | ref $template eq 'HASH' | ||
| 76 | ? 'MIME::Lite::TT::HTML::Japanese' | ||
| 77 | : 'MIME::Lite::TT::Japanese'; | ||
| 78 | $module->require | ||
| 79 | or Catalyst::Exception->throw( | ||
| 80 | message => qq/Couldn't load $module, "$!"/ ); | ||
| 81 | |||
| 82 | my $options = { | ||
| 83 | EVAL_PERL => 0, | ||
| 84 | %{ $c->config->{email}->{TmplOptions} || {} }, | ||
| 85 | %{ $args->{TmplOptions} || {} }, | ||
| 86 | }; | ||
| 87 | |||
| 88 | my $include_path | ||
| 89 | = delete $options->{INCLUDE_PATH} | ||
| 90 | || $c->view->config->{INCLUDE_PATH} | ||
| 91 | || [ $c->config->{root}, $c->config->{root} . '/base' ]; | ||
| 92 | |||
| 93 | if ( $c->config->{ForceUTF8} or $c->config->{email}{ForceUTF8} || $args->{ForceUTF8} ) { | ||
| 94 | $_->require | ||
| 95 | || Catalyst::Exception->throw( message => $! ) | ||
| 96 | for qw/Template::Provider::Encoding Template::Stash::ForceUTF8/; | ||
| 97 | $options->{LOAD_TEMPLATES} = [ Template::Provider::Encoding->new( INCLUDE_PATH => $include_path ) ]; | ||
| 98 | $options->{STASH} = Template::Stash::ForceUTF8->new; | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | $options->{INCLUDE_PATH} = $include_path; | ||
| 102 | } | ||
| 103 | |||
| 104 | my $params = { | ||
| 105 | base => $c->req->base, | ||
| 106 | c => $c, | ||
| 107 | name => $c->config->{name}, | ||
| 108 | %{ $c->stash }, | ||
| 109 | %{ $args->{TmplParams} || {} }, | ||
| 110 | }; | ||
| 111 | |||
| 112 | my $msg = $module->new( | ||
| 113 | %{$c->config->{email} || {} }, | ||
| 114 | %{$args || {} }, | ||
| 115 | Template => $template, | ||
| 116 | TmplParams => $params, | ||
| 117 | TmplOptions => $options, | ||
| 118 | Icode => $args->{Icode} || $c->config->{email}->{Icode} || 'utf8', | ||
| 119 | LineWidth => $args->{LineWidth} || $c->config->{email}->{LineWidth} || 0, | ||
| 120 | ); | ||
| 121 | |||
| 122 | my $route = $c->config->{email}->{mailroute} || { via => 'smtp', host => 'localhost' }; | ||
| 123 | $route->{via} ||= 'smtp'; | ||
| 124 | |||
| 125 | eval { | ||
| 126 | if ( $route->{via} eq 'smtp_tls' ) { | ||
| 127 | $msg->send_by_smtp_tls( | ||
| 128 | $route->{host}, | ||
| 129 | User => $route->{username}, | ||
| 130 | Password => $route->{password}, | ||
| 131 | Port => $route->{port} || 587, | ||
| 132 | ); | ||
| 133 | } | ||
| 134 | elsif ( $route->{via} eq 'sendmail' ) { | ||
| 135 | my %param; | ||
| 136 | $param{FromSender} = '<' . $c->config->{email}->{mailfrom} . '>' if $c->config->{email}->{mailfrom}; | ||
| 137 | $param{Sendmail} = $route->{command} if defined $route->{command}; | ||
| 138 | $msg->send( 'sendmail', %param ); | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | my @args = $route->{host} ? ( $route->{host} ) : (); | ||
| 142 | $msg->send( $route->{via}, @args ); | ||
| 143 | } | ||
| 144 | }; | ||
| 145 | |||
| 146 | if ($@) { | ||
| 147 | Catalyst::Exception->throw( message => "Error while sending emails: $@" ) | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | =head1 SEE ALSO | ||
| 154 | |||
| 155 | L<Catalyst>, L<Catalyst::Plugin::Email>, L<MIME::Lite::TT::Japanese>, L<MIME::Lite::TT::HTML::Japanese>. | ||
| 156 | |||
| 157 | =head1 AUTHOR | ||
| 158 | |||
| 159 | Daisuke Murase <typester@cpan.org> | ||
| 160 | |||
| 161 | =head1 COPYRIGHT | ||
| 162 | |||
| 163 | This program is free software; you can redistribute | ||
| 164 | it and/or modify it under the same terms as Perl itself. | ||
| 165 | |||
| 166 | The full text of the license can be found in the | ||
| 167 | LICENSE file included with this module. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | 1; | ||