in reply to Attaching images to email html template help!

# 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; ... my %img_sec; ...

If I'm understanding you correctly, you want an array of hashes holding the image info:

my @imgs = ( { Type => 'image/gif', ID => 'logo', Path => '/images/logo.gif', Filename => 'logo.gif', Disposition => 'attachment', }, { Type => 'image/gif', ID => 'footer', Path => '/images/footer.gif', Filename => 'footer.gif', Disposition => 'attachment', }, # ... );

(of course, you could also fill the hashes dynamically...)

You should then be able to iterate over / attach them as follows

for my $img (@imgs ) { $msg->attach( %$img ) or die "Error adding $img->{Filename}: $!\n" +; }

Replies are listed 'Best First'.
Re^2: Attaching images to email html template help!
by Anonymous Monk on Mar 17, 2011 at 17:42 UTC
    That cleared some issues for me for sure, thank you, but the images is still only showing when the $->attach(); is inside of sub email for some reason.
      but the images is still only showing when the $->attach(); is inside of sub email

      To attach the images you need the $msg object, and as you have it, that object is lexically scoped to your email function.

      Anyhow, why do you want to attach the images outside of the function?

        Eventually I will change the email sub into a module so I can use it with another scripts I have. But that will be another battle!
      I got it to work, the issue was with ID => 'logo',".
      It should be;
      Id => 'logo',

      I am curious about how I could fill the hashes dynamically, any example on that?
        I am curious about how I could fill the hashes dynamically

        That largely depends on where you get the info from.  Let's say you read a list of image paths from a file  (__DATA__ here):

        #!/usr/bin/perl -w use strict; my @imgs; while (my $path = <DATA>) { chomp $path; my ($fullname, $name, $ext) = $path =~ m#(([^/]+)\.(\w+))$#; my $type = "image/$ext"; $type =~ s/jpg$/jpeg/; push @imgs, { Type => $type, ID => $name, Path => $path, Filename => $fullname, Disposition => 'attachment', }; } use Data::Dumper; print Dumper \@imgs; __DATA__ /images/logo.gif /images/footer.gif /images/whatever.gif

        produces:

        $VAR1 = [ { 'ID' => 'logo', 'Disposition' => 'attachment', 'Type' => 'image/gif', 'Filename' => 'logo.gif', 'Path' => '/images/logo.gif' }, { 'ID' => 'footer', 'Disposition' => 'attachment', 'Type' => 'image/gif', 'Filename' => 'footer.gif', 'Path' => '/images/footer.gif' }, { 'ID' => 'whatever', 'Disposition' => 'attachment', 'Type' => 'image/jpeg', 'Filename' => 'whatever.jpg', 'Path' => '/images/whatever.jpg' } ];
Re^2: Attaching images to email html template help!
by Anonymous Monk on Mar 18, 2011 at 13:30 UTC
    Thanks for your response, it was a great help.
    If I change my sub routine "email" from my sample code here into a module and lets say I would be passing all the values to this new module now called mod_email like this;
    mod_email( {to=>$to, from => $email, subject=>$subject, body=>$body, ttparams => $templ_data} );
    There is no problem passing the values into the module like that, but how could I pass this array of hashes "@imgs" to this module? Here is some lines of code of my trying for your opinion:
    #from the .pl file call the module ... mod_email( {to=>$to, from => $email, subject=>$subject, body=>$body, t +tparams => $templ_data},imgs=>@imgs ); #now this part would be code inside of the module mod_email ... sub process_email_stuff { my ($vals) = @_; my $to = $vals->{ to } || {}; my $from = $vals->{ from } || {}; my $subject = $vals->{ subject } || {}; my $body = $vals->{ body } || {}; my $ttparams = $vals->{ ttparams } || {}; #questioning this: my @imgs = $vals->{ imgs } || {}; my $msg = MIME::Lite::TT::HTML->new( From => $from, ... #all the other stuff its done, now get to the @imgs processing, hopefu +lly. for my $img (@imgs ) { $msg->attach( %$img ) or die "Error adding $img- {Filename}: $!\n" +; } ... 1;

    I hope this is clear about want I am trying to accomplish, and thanks once again!
      how could I pass this array of hashes "@imgs" to this module?

      Normally, you'd pass a reference to it, i.e. \@imgs

      email( {to=>$to, from => $email, ..., imgs => \@imgs} ); ... sub email { my ($vals) = @_; ... my $imgs = $vals->{ imgs } || []; ...

      Further on, you then just dereference it the usual way: @$imgs for the entire array, or $imgs->[0] for a single element (which would be a hashref in your particular case).

      See also perlreftut and perlref.

      P.S. you probably don't want to supply an empty hashref ({}) as defaults for things which are normally just scalars/strings, such as your

      my $to = $vals->{ to } || {}; my $from = $vals->{ from } || {}; my $subject = $vals->{ subject } || {}; ...