root/lang/perl/Ark-View-Email/trunk/lib/Ark/View/Email.pm @ 36003

Revision 36003, 5.1 kB (checked in by haoyayoi, 3 years ago)

add sender

Line 
1package Ark::View::Email;
2
3use Ark 'View';
4our $VERSION = '0.01';
5
6__PACKAGE__->config(
7    stash_key   => 'email',
8    default     => {
9        content_type    => 'text/plain',
10    },
11);
12
13has sender_def => (
14    is   => 'rw',
15    isa  => 'ArrayRef',
16    lazy => 1,
17    default => sub {
18        my $self = shift;
19        my @sender = ();
20        if (my $sender = $self->{sender}->{mailer}) {
21            push (@sender, $sender);
22        }
23        my @def_sender = qw/SMTP Sendmail Qmail/;
24        push (@sender, @def_sender);
25    }
26);
27
28has mailer => (
29    is   => 'rw',
30    isa  => 'Object',
31    lazy => 1,
32    default => sub{
33        my ($self, $mailer) = @_;
34       
35        $self->ensure_class_loaded("Email::Send");
36        Email::Send->import;
37        my $sender = Email::Send->new;
38        my $def;
39        for $def ( @{$self->sender_def} ) {
40            last if $sender->mailer_available($def);
41        }
42        $sender->mailer($def);
43    }
44);
45
46has mime => (
47    is      => 'rw',
48    isa     => 'HashRef',
49    lazy    => 1,
50    default => [],
51);
52
53has email => (
54    is      => 'rw',
55    isa     => 'HashRef',
56    lazy    => 1,
57    default => [],
58)
59
60sub Build {
61    my $self = shift;
62
63    if ( my $args = $self->{sender}->{mailer_args} ) {
64        if ( ref $args eq 'HASH' ) {
65            $self->mailer->mailer_args([ %$args ]);
66        }
67        elsif ( ref $args eq 'ARRAY' ) {
68            $self->mailer->mailer_args($args);
69        } else {
70            croak "Invalid mailer_args specified, check pod for Email::Send!";
71        }
72    }
73    $self->mailer($sender);
74
75    return $self;
76}
77
78sub process {
79    my ( $self, $c ) = @_;
80
81    croak "Unable to send mail, bad mail configuration" unless $self->mailer;
82    my $email = $c->stash->{ $self->{stash_key} };
83    croak "Can't send email without a valid email structure" unless $email;
84
85    # Default content type
86    if ( exists $self->{content_type} and not $email->{content_type} ) {
87        $email->{content_type} = $self->{content_type};
88    }
89
90    my $header = $email->{header} || [];
91    push( @$header, ( 'To'   => delete $email->{to} ) )   if $email->{to};
92    push( @$header, ( 'Cc'   => delete $email->{cc} ) )   if $email->{cc};
93    push( @$header, ( 'Bcc'  => delete $email->{bcc} ) )  if $email->{bcc};
94    push( @$header, ( 'From' => delete $email->{from} ) ) if $email->{from};
95    my $subject = Encode::encode( 'MIME-Header', delete $email->{subject} );
96    push( @$header, ( 'Subject' => $subject ) ) if $email->{subject};
97    push( @$header, ( 'Content-type' => $email->{content_type} ) )
98      if $email->{content_type};
99
100    my $parts = $email->{parts};
101    my $body  = $email->{body};
102
103    unless ( $parts or $body ) {
104        croak "Can't send email without parts or body, check stash";
105    }
106
107    my %mime = ( header => $header, attributes => {} );
108    if ( $parts and ref $parts eq 'ARRAY' ) {
109        $mime{parts} = $parts;
110    }
111    else {
112        $mime{body} = $body;
113    }
114
115    if ( $email->{content_type} ) {
116        $mime{attributes}->{content_type} = $email->{content_type};
117    }
118    if (   $mime{attributes}
119        && not $mime{attributes}->{charset}
120        && $self->{default}->{charset} )
121    {
122        $mime{attributes}->{charset} = $self->{default}->{charset};
123    }
124
125    my $message = $self->generate_message( $c, \%mime );
126    if ($message) {
127        my $return = $self->mailer->send($message);
128        # return is a Return::Value object, so this will stringify as the error
129        # in the case of a failure.
130        croak "$return" unless $return;
131    }
132    else {
133        croak "Unable to create message";
134    }
135}
136
137sub setup_attributes {
138    my ( $self, $c, $attrs ) = @_;
139   
140    my $def_content_type    = $self->{default}->{content_type};
141    my $def_charset         = $self->{default}->{charset};
142
143    my $e_m_attrs = {};
144
145    if (exists $attrs->{content_type} && defined $attrs->{content_type} && $attrs->{content_type} ne '') {
146        $c->log->debug('Ark::View::Email uses specified content_type ' . $attrs->{content_type} . '.') if $c->debug;
147        $e_m_attrs->{content_type} = $attrs->{content_type};
148    }
149    elsif (defined $def_content_type && $def_content_type ne '') {
150        $c->log->debug("Ark::View::Email uses default content_type $def_content_type.") if $c->debug;
151        $e_m_attrs->{content_type} = $def_content_type;
152    }
153   
154    if (exists $attrs->{charset} && defined $attrs->{charset} && $attrs->{charset} ne '') {
155        $e_m_attrs->{charset} = $attrs->{charset};
156    }
157    elsif (defined $def_charset && $def_charset ne '') {
158        $e_m_attrs->{charset} = $def_charset;
159    }
160
161    return $e_m_attrs;
162}
163
164sub generate_message {
165    my ( $self, $c, $attr ) = @_;
166
167    # setup the attributes (merge with defaults)
168    $attr->{attributes} = $self->setup_attributes( $c, $attr->{attributes} );
169    return Email::MIME->create(%$attr);
170}
171
1721;
173__END__
174
175=head1 NAME
176
177Ark::View::Email - Email view class
178
179=head1 SYNOPSIS
180
181  use Ark::View::Email;
182
183
184=head1 DESCRIPTION
185
186Ark::View::Email is
187
188=head1 AUTHOR
189
190haoyayoi E<lt>st.hao.yayoi@gmail.comE<gt>
191
192=head1 SEE ALSO
193
194=head1 LICENSE
195
196This library is free software; you can redistribute it and/or modify
197it under the same terms as Perl itself.
198
199=cut
Note: See TracBrowser for help on using the browser.