Jane Humbrey said:
Hi James
I changed the type as you said, but the paper clip didn't appear to show the
attachments in Outlook/Outlook Express : (.
Jo
Okay ... I wanted to do something similar ... and normally, I don't do
this, but .... here is some code for you to try.
First, change the directory to what you are using, and fill in valid
'To' and 'From' address, and change the SMTP server from 'localhost'
to what you normally use/need to use.
Second, I used Novell GroupWise and Sylpheed as the email clients. As
stated in the MIME::Lite and MIME::Lite::HTML documentation and proven
during my tests, each client will handle email attachments
differently. Plus, you will notice I tried to add text to the
begining of the email - which was the only thing that showed properly
in the email clients. So, I commented the lines out. The code will
produce inline HTML AND attachments. However, I was not able to test
using Outlook.
I personally want to get this working properly, because I would like
to use this in work I do. I'll post anything else I know here -and- I
might just email the author to see if the usage is correct -and-
double check the RFC's to see if the output produced and what the
RFC's jive. So far, they do.
I didn't use the MIME::Lite::HTML module because that only produces
inline HTML. From the OP, it appears that you wanted an attachment.
I also used LWP and HTML::TokeParse to gather the original document.
The code is commented for you (and any one else's) review.
HTH
Jim
==TESTED, BUT NEEDS VARIABLES UPDATED==
#!/usr/bin/perl -w
#use strict pragma
use strict;
#use the following modules
use LWP::Simple;
use HTML::TokeParser;
use MIME::Lite;
#declare lexical variables (I do it right Tad?

)
my ($directory,$text,$html,$parser,%images,$mimeLite);
#what directory are file files located
$directory = '<fill in directory here>';
#set the text (aka body) of message
$text = 'Here is the latest add for ....';
#get the HTML document (as if you were using a browser) using LWP
#notice the 'file://' - this is the same as if you typed in
#file:///<some directory>/<some file>
$html = get("file://$directory/index.htm");
#declare a new HTML::TokeParser object - use the HTML file
#you just got as the document to parse
$parser = HTML::TokeParser->new(\$html);
#while parsing through the document, get each token
#(aka tag w/ attributes)
while (my $token = $parser->get_token) {
#if the tag is a start tag AND is the 'img' tag ....
if ($token->[0] eq 'S' && $token->[1] eq 'img') {
#store the 'src' attribute (aka the image file name)
#in the hash %images
$images{ $token->[2]{'src'} }++;
}
}
#this will allow MIME::Lite to verify the address you're sending to
$MIME::Lite::AUTO_VERIFY=1;
#create a new MIME::Lite object with
#the following attributes
#The 'Reply-To' is NOT required, but nice in case this script
#runs as a daemon/service - if you set this, when a user
#clicks the 'Reply', the 'Reply-To' address is used instead of
#the 'From' address
$mimeLite = new MIME::Lite(
From=>'FROM',
To=>'TO',
Subject=>'TEST',
Type=>'multipart/related',
'Reply-To'=>'RELPY-TO',
);
#THIS PART DOES NOT WORK PROPERLY
#Now, start building your MIME encoded attachments
#First, attach the body ...
#$mimeLite->attach(
# Type=>'text/plain',
# Data=>"$text",
# Encoding=>'quoted-printable'
#);
#now, the images ...
foreach my $img(keys %images){
#now, put $img into $new_img
my $new_img = $img;
#this regex removes the leading '/' from the filename
#it may NOT be required - it was for my test

$new_img =~ s/^\///;
#get rid of any path information - just get the filename
$new_img =~ s/^.*[\/](\w+\.\w+)$/$1/;
#according to the MIME::Lite documentation, in order to get
#any images to show up, we need to insert 'cid:' into the
'src'
#tag - we'll to that here for each image with a regex
$html =~ s/\"$img\"/\"cid:$new_img\"/g;
#now create the attachment ...
$mimeLite->attach(
Type=>'image/gif',
Path=>"$directory/$img",
Id=>"$new_img",
Encoding=>'base64',
Disposition=>'attachment',
Filename=>"$new_img"
);
}
#now, attach the HTML file ...
#for your purposes, you may want to change the 'Type' to
#what was suggested in the previous post and the 'Encoding'
#could be removed if you do that or changed to
#'base64'
$mimeLite->attach(
Type=>'text/html',
Data=>"$html",
Encoding=>'quoted-printable',
Disposition=>'attachment',
Filename=>'myhtml.htm'
);
#send the message
$mimeLite->send('smtp', 'localhost');