File Coverage

blib/lib/Email/Assets.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Email::Assets;
2 1     1   25693 use Moose;
  0            
  0            
3              
4             =head1 NAME
5              
6             Email::Assets - Manage assets for Email
7              
8             =head1 VERSION
9              
10             Version 0.10
11              
12             =cut
13              
14             our $VERSION = '0.10';
15              
16             use MIME::Types;
17             use Email::Assets::File;
18              
19             has base => (
20             is => 'ro',
21             );
22              
23             has mime_types => (
24             is => 'ro',
25             lazy => 1,
26             default => sub { return MIME::Types->new(); },
27             );
28              
29             has _assets => (
30             traits => ['Hash'],
31             is => 'ro',
32             isa => 'HashRef[Email::Assets::File]',
33             default => sub { {} },
34             handles => {
35             _asset_exists => 'exists',
36             names => 'keys',
37             _set_asset => 'set',
38             _get_asset => 'get',
39             _all_assets => 'values',
40             }
41             );
42              
43             sub include {
44             my ($self, $filename, $options) = @_;
45             $options ||= { inline_only => 0 };
46              
47             if ($self->_asset_exists($filename)) {
48             return $self->_get_asset($filename);
49             }
50              
51             my $asset = Email::Assets::File->new({
52             mime_types => $self->mime_types,
53             base_paths => $self->base,
54             relative_filename => $filename,
55             inline_only => $options->{inline_only}
56             });
57             $self->_set_asset($filename => $asset);
58             return $asset;
59             }
60              
61             sub include_base64 {
62             my ($self, $base64_string, $filename, $options) = @_;
63             $options ||= { inline_only => 0 };
64             my $asset = Email::Assets::File->new({
65             mime_types => $self->mime_types,
66             base_paths => $self->base,
67             relative_filename => $filename,
68             base64_data => $base64_string,
69             inline_only => $options->{inline_only},
70             url_encoding => $options->{url_encoding},
71             });
72             $self->_set_asset($filename => $asset);
73             return $asset;
74             }
75              
76              
77             sub exports {
78             return shift->_all_assets;
79             }
80              
81             sub get {
82             my ($self, $filename) = @_;
83             return $self->_get_asset($filename);
84             }
85              
86             sub attachments {
87             return [ grep { $_->not_inline_only } shift()->_all_assets ];
88             }
89              
90             sub to_mime_parts {
91             my $self = shift;
92             return [ map { $_->as_mime_part } @{$self->attachments} ];
93             }
94              
95             =head1 DESCRIPION
96              
97             HTML Email is a world of pain, this makes life a bit simpler. This is a Simple to use perl class for handling file assets in email,
98             allowing you to link using cid or embed inline as data uri, providing hopefully something close to what File::Assets does for web.
99              
100             Also supports exporting as MIME::Lite message parts.
101              
102             =head1 SYNOPSIS
103              
104             use Email::Assets;
105              
106             my $assets = Email::Assets->new( base => [ $uri_root, $dir_root ] );
107              
108             # Email::Assets will automatically detect the type based on the extension
109             my $asset = $assets->include("/static/foo.gif");
110              
111             # or
112              
113             my $asset = $assets->include_base64( $image_base64 );
114              
115             # This asset won't get attached twice, as Email::Assets will ignore repeats of a path
116             my $cid = $assets->include("/static/foo.gif")->cid;
117              
118             # Or you can iterate (in order)
119             for my $asset ($assets->exports) {
120             print $asset->cid, "\n";
121             my $mime_part = $asset->as_mime_part;
122             }
123              
124             ..or in a template :
125              
126             <img src="cid://[% assets.include('static/foo.gif').cid %]">
127             [% # or %]
128             <img src="data:[% assets.include('static/foo.gif', {inline_only => 1}).inline_data -%]">
129             <p>
130             <img src="cid:[% assets.include_base64(image_base64, filename, { url_encoded => 1 }).cid %]">
131             </p>
132             <p>
133              
134              
135             =head1 ATTRIBUTES
136              
137             =head2 mime_types
138              
139             MIME::Types object
140              
141             =head2 base
142              
143             arrayref of paths to find files in
144              
145             =head1 METHODS
146              
147             =head2 include
148              
149             Add an asset, takes filename (required), then optional hashref of options (inline_only currently only one supported), returns Email::Assets::File object
150              
151             =head2 include_base64
152              
153             Object method. Adds an asset already encoded in base64, returns that asset.
154              
155             Takes string holding base64 encoded file, filename, then optional hashref of options:
156              
157             =over 4
158              
159             =item inline_only - exclude asset from to_mime_parts, to avoid adding un-necessary size & attachments
160              
161             =item url_encoded - base64 encoding is "url safe" so use base64url decoder, and re-encode for MIME::Lite, etc as not suited for most email use.
162              
163             =back
164              
165             Returns Email::Assets::File object.
166              
167             =head2 exports
168              
169             Get all assets, returns list of Email::Assets::File objects
170              
171             =head2 get
172              
173             Get an asset by name, takes relative path, returns Email::Assets::File object
174              
175             =head2 attachments
176              
177             Get assets that aren't inline_only, returns arrayref of Email::Assets::File objects
178              
179             =head2 to_mime_parts
180              
181             Returns assets that aren't inline_only as arrayref of MIME::Lite objects
182              
183             =head1 SEE ALSO
184              
185             =over 4
186              
187             =item L<Email::Assets::File>
188              
189             =item L<MIME::Lite>
190              
191             =item L<File::Assets>
192              
193             =back
194              
195             =head1 AUTHOR
196              
197             Aaron J Trevena, C<< <teejay at cpan.org> >>
198              
199             =head1 BUGS
200              
201             Please report any bugs or feature requests to C<bug-email-assets at rt.cpan.org>, or through
202             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Email-Assets>. I will be notified, and then you'll
203             automatically be notified of progress on your bug as I make changes.
204              
205             =head1 SUPPORT
206              
207             You can find documentation for this module with the perldoc command.
208              
209             perldoc Email::Assets
210              
211              
212             You can also look for information at:
213              
214             =over 4
215              
216             =item * RT: CPAN's request tracker (report bugs here)
217              
218             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Email-Assets>
219              
220             =item * AnnoCPAN: Annotated CPAN documentation
221              
222             L<http://annocpan.org/dist/Email-Assets>
223              
224             =item * CPAN Ratings
225              
226             L<http://cpanratings.perl.org/d/Email-Assets>
227              
228             =item * Search CPAN
229              
230             L<http://search.cpan.org/dist/Email-Assets/>
231              
232             =back
233              
234              
235             =head1 ACKNOWLEDGEMENTS
236              
237              
238             =head1 LICENSE AND COPYRIGHT
239              
240             Copyright 2014 Aaron J Trevena.
241              
242             This program is free software; you can redistribute it and/or modify it
243             under the terms of the the Artistic License (2.0). You may obtain a
244             copy of the full license at:
245              
246             L<http://www.perlfoundation.org/artistic_license_2_0>
247              
248             Any use, modification, and distribution of the Standard or Modified
249             Versions is governed by this Artistic License. By using, modifying or
250             distributing the Package, you accept this license. Do not use, modify,
251             or distribute the Package, if you do not accept this license.
252              
253             If your Modified Version has been derived from a Modified Version made
254             by someone other than you, you are nevertheless required to ensure that
255             your Modified Version complies with the requirements of this license.
256              
257             This license does not grant you the right to use any trademark, service
258             mark, tradename, or logo of the Copyright Holder.
259              
260             This license includes the non-exclusive, worldwide, free-of-charge
261             patent license to make, have made, use, offer to sell, sell, import and
262             otherwise transfer the Package with respect to any patent claims
263             licensable by the Copyright Holder that are necessarily infringed by the
264             Package. If you institute patent litigation (including a cross-claim or
265             counterclaim) against any party alleging that the Package constitutes
266             direct or contributory patent infringement, then this Artistic License
267             to you shall terminate on the date that such litigation is filed.
268              
269             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
270             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
271             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
272             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
273             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
274             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
275             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
276             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277              
278              
279             =cut
280              
281             1; # End of Email::Assets