Email-MIME-CreateHTML
view release on metacpan or search on metacpan
lib/Email/MIME/CreateHTML.pm view on Meta::CPAN
###############################################################################
# Purpose : Build HTML emails
# Author : Tony Hennessy
# Created : Aug 2006
###############################################################################
package Email::MIME::CreateHTML;
use strict;
use Carp;
use Exporter;
use Email::MIME;
use HTML::TokeParser::Simple;
use HTML::Tagset;
our $VERSION = '1.042';
use Email::MIME::CreateHTML::Resolver;
#Globals
use vars qw(%EMBED @EXPORT_OK @ISA);
%EMBED = (
'bgsound' => {'src'=>1},
'body' => {'background'=>1},
'img' => {'src'=>1},
'input' => {'src'=>1},
'table' => {'background'=>1},
'td' => {'background'=>1},
'th' => {'background'=>1},
'tr' => {'background'=>1},
);
@EXPORT_OK = qw(embed_objects parts_for_objects build_html_email);
@ISA = qw(Exporter);
#
# Public routines used by create_html and also exportable
#
sub embed_objects {
my ($html, $args) = @_;
my $embed = ( defined $args->{embed} && $args->{embed} eq '0' ) ? 0 : 1;
my $inline_css = ( defined $args->{inline_css} && $args->{inline_css} eq '0' ) ? 0 : 1;
my $resolver = new Email::MIME::CreateHTML::Resolver($args);
my $embed_tags = $args->{'embed_elements'} || \%EMBED;
return ($html, {}) unless ( $embed || $inline_css ); #No-op unless one of these is set
my ($html_modified, %embedded_cids);
my $parser = HTML::TokeParser::Simple->new( \$html );
my $regex = '^(' . join('|',keys %HTML::Tagset::linkElements) . ')';
$regex = qr/$regex/;
while ( my $token = $parser->get_token ) {
unless ( $token->is_start_tag( $regex ) ) {
$html_modified .= $token->as_is;
next;
}
my $token_tag = $token->get_tag();
my $token_attrs = $token->get_attr();
# inline_css
if ( $token_tag eq 'link' && $token_attrs->{type} eq 'text/css' ) {
unless ( $inline_css ) {
$html_modified .= $token->as_is;
next;
}
my $link = $token_attrs->{'href'};
my ($content,$filename,$mimetype,$encoding) = $resolver->get_resource( $link );
$html_modified .= "\n".'<style type="text/css">'."\n".'<!--'."\n".
$content.
"\n-->\n</style>\n";
next;
}
# rewrite and embed
for my $attr ( @{ $HTML::Tagset::linkElements{$token_tag} } ) {
if ( defined $token_attrs->{$attr} ) {
my $link = $token_attrs->{$attr};
next if ($link =~ m/^cid:/i);
# embed
if ( $embed && $embed_tags->{$token_tag}->{$attr} ) {
unless ( defined $embedded_cids{$link} ) {
# make a unique cid
my $newcid = time().$$.int(rand(1e6));
$embedded_cids{$link} = $newcid;
}
my $link_rewrite = "cid:".$embedded_cids{$link};
$token->set_attr( $attr => $link_rewrite );
}
}
}
$html_modified .= $token->as_is;
}
my %objects = reverse %embedded_cids; #invert mapping
return ($html_modified, \%objects);
}
sub parts_for_objects {
my ($objects, $args) = @_;
my $resolver = new Email::MIME::CreateHTML::Resolver($args);
my @html_mime_parts;
lib/Email/MIME/CreateHTML.pm view on Meta::CPAN
=item ($modified_html, $cid_mapping) = embed_objects($html, \%options)
This parses the HTML and replaces URIs in the embed list with a CID.
The modified HTML and CID to URI mapping is returned.
Relevant parameters are:
embed
inline_css
base
object_cache
resolver
The meanings and defaults of these parameters are explained below.
=item @mime_parts = parts_for_objects($cid_mapping, \%options)
This creates a list of Email::MIME parts for each of the objects in the supplied CID mapping.
Relevant options are:
base
object_cache
resolver
The meanings and defaults of these parameters are explained below.
=item $email = build_html_email(\@headers, $html, \%body_attributes, \@html_mime_parts, $plain_text_mime)
The assembles a ready-to-send Email::MIME object (that can be sent with Email::Send).
=back
=head1 PARAMETERS
=over 4
=item header =E<gt> I<list>
A list reference containing a set of headers to be created.
If no Date header is specified, one will be provided for you based on the
gmtime() of the local machine.
=item body =E<gt> I<scalar>
A scalar value holding the HTML message body.
=item body_attributes =E<gt> I<hash reference>
This is passed as the attributes parameter to the C<create> method (supplied by C<Email::MIME::Creator>) that creates the html part of the mail.
The body content-type will be set to C<text/html> unless it is overidden here.
=item embed =E<gt> I<boolean>
Attach relative images and other media to the message. This is enabled by default.
The module will attempt to embed objects defined by C<embed_elements>.
Note that this option only affects the parsing of the HTML and will not affect the C<objects> option.
The object's URI will be rewritten as a Content ID.
=item embed_elements =E<gt> I<reference to hash of hashes with boolean values>
The set of elements that you want to be embedded. Defaults to the C<%Email::MIME::CreateHTML::EMBED> package global.
This should be a data structure of the form:
embed_elements => {
$elementname_1 => {$attrname_1 => $boolean_1},
$elementname_2 => {$attrname_2 => $boolean_2},
...
}
i.e. resource will be embedded if C<$embed_elements-E<gt>{$elementname}-E<gt>{$attrname}> is true.
=item resolver =E<gt> I<object>
If a resolver is supplied this will be used to fetch the resources that are embedded as MIME objects in the email. If no resolver is given the default behaviour is to choose the best available resolver to read C<$uri> with any C<$base> value prefixe...
Resources fetched using the resolver will be cached if an C<object_cache> is supplied.
=item base =E<gt> I<scalar>
This must be a filepath or a URI.
If C<embed> is true (the default) then C<base> will be used when fetching the objects.
Examples of good bases:
./local/images
/home/somewhere/images
http://mywebserver/images
=item inline_css =E<gt> I<boolean>
Inline any CSS external CSS files referenced through link elements. Enabled by default.
Some mail clients will only interpret css if it is inlined.
=item objects =E<gt> I<hash reference>
A reference to a hash of external objects. Keys are Content Ids
and the values are filepaths or URIs used to fetch the resource with the resolver. We use C<MIME::Types> to derive the type from the
file extension. For example in an HTML mail you would use the file keyed on '12345678@bbc.co.uk' like C<E<lt>img src="cid:12345678@bbc.co.uk" alt="a test" width="20" height="20" /E<gt>>
=item object_cache =E<gt> I<cache object>
A cache object can be supplied to cache external resources such as images.
This must support the following interface:
$o = new ...
$o->set($key, $value)
$value = $o->get($key)
Both the Cache and Cache::Cache distributions on CPAN conform to this.
=item text_body =E<gt> I<scalar>
A scalar value holding the contents of an additional I<plain text> message body.
=item text_body_attributes =E<gt> I<hash reference>
This is passed as the attributes parameter to the C<create> method (supplied by C<Email::MIME::Creator>) that creates the plain text part of the mail.
The body Content-Type will be set to C<text/plain> unless it is overidden here.
=back
=head1 GLOBAL VARIABLES
=over 4
=item %Email::MIME::CreateHTML::EMBED
This is the default set of elements (and the relevant attributes that point at a resource) that will be embedded.
The for this is:
'bgsound' => {'src'=>1},
'body' => {'background'=>1},
'img' => {'src'=>1},
'input' => {'src'=>1},
'table' => {'background'=>1},
'td' => {'background'=>1},
'th' => {'background'=>1},
'tr' => {'background'=>1}
You can override this using the C<embed_elements> parameter.
=back
=head1 COOKBOOK
=head2 The basics
This builds an HTML email:
my $email = Email::MIME->create_html(
header => [
From => 'my@address',
To => 'your@address',
Subject => 'My speedy HTML',
],
body => $html
);
If you want a plaintext alternative, include the C<text_body> option:
my $email = Email::MIME->create_html(
header => [
From => 'my@address',
To => 'your@address',
Subject => 'Here is the information you requested',
],
body => $html,
text_body => $plain_text #<--
);
If you want your images to remain as links (rather than be embedded in the email) disable the C<embed> option:
my $email = Email::MIME->create_html(
header => [
From => 'my@address',
To => 'your@address',
Subject => 'My speedy HTML',
],
body => $html,
embed => 0 #<--
);
=head2 Optimising out HTML parsing
By default, the HTML is parsed to look for objects and stylesheets that need embedding.
If you are controlling the construction of the HTML yourself, you can use Content Ids as the URIs within your HTML
( run in 1.519 second using v1.01-cache-2.11-cpan-71847e10f99 )