Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Sending HTML email w/images + attachment

by vagnerr (Prior)
on Aug 21, 2002 at 01:06 UTC ( #191630=note: print w/ replies, xml ) Need Help??


in reply to Sending HTML email w/images + attachment

Have you considered MIME::Lite ?

It has the advantage that you can build up your own mime mail components. I was faced with a similar problem to yours in the past. I didn't need to use multiparts but I did do image inserts I used HTML::LinkExtor to grab all the images in the HTML page. I've included a code snippit if it will help:-

Notes:

  • my_print() is my "special" debug print statement (only prints if debug enabled)
  • $opts is my command line arguments hash $opts{'d'} was for debug :)
  • $template was an HTML::Template object
  • the script assumes that images to attach are local to its directory
Hope this helps
my $msg = MIME::Lite->new( From => $from_address, To => $email_address, Subject => $subject, Type => 'text/html', Data => $template->output() ); #we need to add any images to the mail if they exist my $parser = HTML::LinkExtor->new(); $parser->parse($template->output()); foreach my $link ($parser->links){ if ($link->[0] eq 'img'){ my_print("Image found [".$link->[2]."] "); if ($link->[2] =~ /^https?:\/\//){ my_print("Absolute! Ignored.\n"); }else{ my ($imgtype,$junk) = reverse split '\.',$link->[2]; $imgtype =~ s/^jpg$/jpeg/; #propper mime name? if(open(TEST,"<".$link->[2])){ my_print("Attaching\n"); close(TEST); $msg->attach(Type =>'image/'.$imgtype, Path =>$link->[2], Filename=>$link->[2], #Disposition => 'inline'); Disposition => 'attachment'); }else{ my_print("Not found, Skipped!!\n"); } } } } if($opts{'d'}){ #debug mode my_print($msg->as_string); }else{ $msg->send(); }


---If it doesn't fit use a bigger hammer


Comment on Re: Sending HTML email w/images + attachment
Download Code
Re: Re: Sending HTML email w/images + attachment
by Xaositect (Friar) on Aug 21, 2002 at 16:15 UTC
    Yes, we did look at MIME::Lite, although we didn't do anything as fancy as search the HTML for images. We just had a list of the images necessary, and did something like:
    foreach $image (@htmlimages) { $msg->attach( Type => 'image/gif', Id => $image, Path => "./$image" ); }
    We had no trouble makeing HTML email with images, or email with attachments, or email with a text alternative, I simply couldn't find a way to combine all three.

    The trick is the nesting a multipart/related inside a multipart/alternative inside a multipart/mixed message, while still having content parts outside the innermost section. If I had something like:
    $msg->close(Type => multipart/related);
    I could probably do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://191630]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-07-02 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls