X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=Email.pm;h=7945e66b4ce9a46d18b8d4b925d2932623ea481d;hb=23938968d8d7f8b6761460e8aec8064a1055f139;hp=d376becc5835d30ecb2e4143ac4130d416562157;hpb=1ab6815bfaa94e7d5c60c8cedf1576f93b9691e9;p=catagits%2FCatalyst-Plugin-Email.git diff --git a/Email.pm b/Email.pm index d376bec..7945e66 100644 --- a/Email.pm +++ b/Email.pm @@ -4,8 +4,9 @@ use strict; use Email::Send; use Email::MIME; use Email::MIME::Creator; +use Carp qw/croak/; -our $VERSION = '0.03'; +our $VERSION = '0.06'; =head1 NAME @@ -30,15 +31,79 @@ Catalyst::Plugin::Email - Send emails with Catalyst Send emails with Catalyst and L and L. -=head2 METHODS +=head1 CONFIGURATION -=head3 email +C accepts the same options as L. + +To send using the system's C program, set C like so: + + __PACKAGE__->config->{email} = ['Sendmail']; + +To send using authenticated SMTP: + + __PACKAGE__->config->{email} = [ + 'SMTP', + 'smtp.myhost.com', + username => $USERNAME, + password => $PASSWORD, + ]; + +For different methods of sending emails, and appropriate C options, +see L, L, L and +L. + +=head1 METHODS + +=head2 email + +C accepts the same arguments as L's +C. + + $c->email( + header => [ + To => 'me@localhost', + Subject => 'A TT Email', + ], + body => $c->subreq( '/render_email' ), + ); + +To send a multipart message, include a C argument containing an +arrayref of Email::MIME objects. + + my @parts = ( + Email::MIME->create( + attributes => { + content_type => 'application/pdf', + encoding => 'quoted-printable', + name => 'report.pdf', + }, + body => $FILE_DATA, + ), + Email::MIME->create( + attributes => { + content_type => 'text/plain', + disposition => 'attachment', + charset => 'US-ASCII', + }, + body => $c->subreq( '/render_email' ), + ), + ); + + $c->email( + header => [ + To => 'me@localhost', + Subject => 'A TT Email', + ], + parts => \@parts, + ); =cut sub email { my $c = shift; my $email = $_[1] ? {@_} : $_[0]; + croak "Can't send mail without recipient" + unless length($email->{To}); $email = Email::MIME->create(%$email); my $args = $c->config->{email} || []; my @args = @{$args}; @@ -50,14 +115,74 @@ sub email { send $class => $email, @args; } +=head1 USING WITH A VIEW + +A common practice is to handle emails using the same template language used +for HTML pages. This can be accomplished by pairing this plugin with +L. + +Here is a short example of rendering an email from a Template Toolkit source +file. The call to $c->subreq makes an internal call to the render_email +method just like an external call from a browser. The request will pass +through the end method to be processed by your View class. + + sub send_email : Local { + my ( $self, $c ) = @_; + + $c->email( + header => [ + To => 'me@localhost', + Subject => 'A TT Email', + ], + body => $c->subreq( '/render_email' ), + ); + # redirect or display a message + } + + sub render_email : Local { + my ( $self, $c ) = @_; + + $c->stash( + names => [ qw/andyg sri mst/ ], + template => 'email.tt', + ); + } + +And the template: + + [%- FOREACH name IN names -%] + Hi, [% name %]! + [%- END -%] + + -- + Regards, + Us + +Output: + + Hi, andyg! + Hi, sri! + Hi, mst! + + -- + Regards, + Us + =head1 SEE ALSO -L. +L, L, L, +L =head1 AUTHOR Sebastian Riedel, C +=head1 THANKS + +Andy Grundman - Additional documentation + +Carl Franks - Additional documentation + =head1 COPYRIGHT This program is free software, you can redistribute it and/or modify it