Hi Monks!
I am trying to attach multiple images to my HTML template file, but i need the values for the images to be out side of my email sub, and that is where it is not working. It works having the values inside of the sub email, but I would like to have these values in a dynamic way, to be easier to manipulate the HTML template. Any help will be great, thanks for looking!
I hope the code explains as well what I am trying to do here:
#!/usr/bin/perl use strict; use warnings; use MIME::Lite::TT::HTML; my %params; $params{first_name} = 'Joe'; $params{last_name} = 'Test'; $params{date} = 'April 11, 2011'; my %options; $options {INCLUDE_PATH} = '/templates'; my $logo = '/images/logo.gif'; my $footer = '/images/footer.gif'; # I'd like to have a system that would allow me # to add as many images as possible to my template file here # instead of multiple hashes, is that possible? my %img_in; $img_in{Type} = 'image/gif'; $img_in{Id} = 'logo'; $img_in{Path} = $logo; $img_in{Filename} = 'logo.gif'; $img_in{Disposition} = 'attachment'; my %img_sec; $img_sec{Type} = 'image/gif'; $img_sec{Id} = 'footer'; $img_sec{Path} = $footer; $img_sec{Filename} = 'footer.gif'; $img_sec{Disposition} = 'attachment'; # set up email my $to = 'test@test.com'; my $from = 'test@test.com'; my $subject = "Email Sent Test"; my $message = "Email Test."; # send email email($to, $from, $subject, $message); # email function sub email { # get incoming parameters my ($to, $from, $subject, $message) = @_; # create a new message my $msg = MIME::Lite::TT::HTML->new( From => $from, To => $to, Subject => $subject, Template => { #text => 'test_txt.tt', html => 'test_html.tt', }, TmplOptions => \%options, TmplParams => \%params, Data => $message ); # add the attachments # this works but I need these values to be dynamic not hard coded in +here =code my $logo = '/images/logo.gif'; my $footer = '/images/footer.gif'; $msg->attach( Type => 'image/gif', Id => 'logo', Path => $logo, Filename => 'logo.gif', Disposition => 'attachment' )or die "Error adding $logo: $!\n"; $msg->attach( Type => 'image/gif', Id => 'footer', Path => $footer, Filename => 'footer.gif', Disposition => 'attachment' )or die "Error adding $footer: $!\n"; =cut # here is where I am having problems for ( keys %img_in ) { $msg->attach( Type => $img_in{Type}, Id => $id=$img_in{ID}, Path => $path=$img_in{Path}, Filename => $img_in{Filename}, Disposition => $img_in{Disposition} )or die "Error adding $img_in{$_}: $!\n"; } # send the email $msg->send(); }
Here is the test template:
<html> <body> TEST HTML<br> Logo: <img src="cid:logo" height="36" border="0"> <br> <strong>Hi [% first_name %]</strong>, <p> Last Name [% last_name %]. </p> <p> [% date %] </p> <p> Thank you! </p> Logo Footer: <img src="cid:footer" height="36" border="0"> <br> </body> </html>

Thanks!

In reply to Attaching images to email html template help! by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.