|
NAMEEmail::MIME::CreateHTML - Multipart HTML Email builderSYNOPSISuse Email::MIME::CreateHTML; 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 ); use Email::Send; my $sender = Email::Send->new({mailer => 'SMTP'}); $sender->mailer_args([Host => 'smtp.example.com']); $sender->send($email); DESCRIPTIONThis module allows you to build HTML emails, optionally with a text-only alternative and embedded media objects. For example, an HTML email with an alternative version in plain text and with all the required images contained in the mail.The HTML content is parsed looking for embeddable media objects. A resource loading routine is used to fetch content from those URIs and replace the URIs in the HTML with CIDs. The default resource loading routine is deliberately conservative, only allowing resources to be fetched from the local filesystem. It's possible and relatively straightforward to plug in a custom resource loading routine that can resolve URIs using a broader range of protocols. An example of one using LWP is given later in the "COOKBOOK". The MIME structure is then assembled, embedding the content of the resources where appropriate. Note that this module does not send any mail, it merely does the work of building the appropriate MIME message. The message can be sent with Email::Send or any other mailer that can be fed a string representation of an email message. Mail ConstructionThe mail construction is compliant with rfc2557.HTML, no embedded objects (images, flash, etc), no text alternative text/html HTML, no embedded objects, with text alternative multipart/alternative text/plain text/html HTML with embedded objects, no text alternative multipart/related text/html embedded object one embedded object two ... HTML with embedded objects, with text alternative multipart/alternative text/plain multipart/related text/html embedded object one embedded object two ... METHODSThere is only one method, which is installed into the Email::MIME package:
LOW-LEVEL APIEmail::MIME::CreateHTML also defines a lower-level interface of 3 building-block routines that you can use for finer-grain construction of HTML mails. These may be optionally imported:use Email::MIME::CreateHTML qw(embed_objects parts_for_objects build_html_mail);
PARAMETERS
GLOBAL VARIABLES
COOKBOOKThe basicsThis 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 "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 "embed" option: my $email = Email::MIME->create_html( header => [ From => 'my@address', To => 'your@address', Subject => 'My speedy HTML', ], body => $html, embed => 0 #<-- ); Optimising out HTML parsingBy 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 and then pass in a set of objects to associate with those Content IDs:my $html = qq{ <html><head><title>My Document</title></head><body> <p>Here is a picture:</p><img src="cid:some_image_jpg@bbc.co.uk"> </body></html> }; You then need to create a mapping of the Content IDs to object filenames: my %objects = ( "some_image_jpg@bbc.co.uk" => "/var/html/some_image.jpg" ); Finally you need to disable both the "embed" and "inline_css" options to turn off HTML parsing, and pass in your mapping: my $quick_to_assemble_mime = Email::MIME->create_html( header => [ From => 'my@address', To => 'your@address', Subject => 'My speedy HTML', ], body => $html, embed => 0, #<-- inline_css => 0, #<-- objects => \%objects #<-- ); Preprocessing templates If you have for example a personalised newsletter where your HTML will vary slightly from one email to the next, but you don't want to re-parse the HTML each time to re-fetch and attach objects, you can use the "embed_objects" function to pre-process the template, converting URIs into CIDs: use Email::MIME::CreateHTML qw(embed_objects); my ($preproc_tmpl_content, $cid_mapping) = embed_objects($tmpl_content); You can then reuse this and the CID mapping: my $template = compile_template($preproc_tmpl_content); foreach $newsletter (@newsletters) { #Do templating my $html = $template->process($newsletter); #Build MIME structure my $mime = Email::MIME->create_html( header => [ From => $reply_address, To => $newsletter->address, Subject => 'Weekly newsletter', ], body => $html, embed => 0, #Already done inline_css => 0, #Already done objects => $cid_mapping #Here's one we prepared earlier ); #Send email send_email($mime); } Note that one caveat with this approach is that all possible images that might be used in the template will be attached to the email. Depending on your template logic, it may be that some are never actually referenced from within the email (e.g. if an image is conditionally displayed) so this may create unnecessarily large emails. Plugging in a custom resource resolverA custom resource resolver can be specified by passing your own object to resolver:my $mime = Email::MIME->create_html( header => [ From => 'my@address', To => 'your@address', Subject => 'Here is the information you requested', ], body => $html, base => 'http://internal.foo.co.uk/images/', resolver => new MyResolver, #<-- ); The object needs to have the following API: package MyResolver; sub new { my ($self, $options) = @_; my $base_uri = $options->{base}; #... YOUR CODE HERE ... (probably want to stash $base_uri in $self) } sub get_resource { my ($self, $uri) = @_; my ($content,$filename,$mimetype,$xfer_encoding); #... YOUR CODE HERE ... return ($content,$filename,$mimetype,$xfer_encoding); } where: $uri is the URI of the object we are embedding (taken from the markup or passed in via the CID mapping) $base_uri is base URI used to resolve relative URIs $content is a scalar containing the contents of the file $filename is used to set the name attribute of the Email::MIME object $mimetype is used to set the content_type attribute of the Email::MIME object $xfer_encoding is used to set the encoding attribute of the Email::MIME object (note this is the suitable transfer encoding NOT a character encoding) Plugging in different types of object cacheYou can use a cache from the Cache::Cache distribution:use Cache::MemoryCache; my $mime = Email::MIME->create_html( header => \@headers, body => $html, object_cache => new Cache::MemoryCache( { 'namespace' => 'MyNamespace', 'default_expires_in' => 600 } ) ); Or a cache from the Cache distribution: use Cache::File; my $mime = Email::MIME->create_html( header => \@headers, body => $html, object_cache => Cache::File->new( cache_root => '/tmp/mycache', default_expires => '600 sec' ) ); Alternatively you can roll your own. You just need to define an object with get and set methods: my $mime = Email::MIME->create_html( header => \@headers, body => $html, object_cache => new MyCache() ); package MyCache; our %Cache; sub new {return bless({}, shift())} sub get {return $Cache{shift()}} sub set {$Cache{shift()} = shift()} 1; SEE ALSOPerl Email Project <http://pep.pobox.com>Email::Simple, Email::MIME, Email::Send, Email::MIME::Creator TODOMaybe add option to control the order that the text + html parts appear in the MIME message.AUTHORTony Hennessy and Simon Flack with cookbook + some refactoring by John Alden <cpan _at_ bbc _dot_ co _dot_ uk> with additional contributions by Ricardo Signes <rjbs@cpan.org> and Henry Van Styn <vanstyn@cpan.org>COPYRIGHT(c) BBC 2005,2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL.See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
Visit the GSP FreeBSD Man Page Interface. |