tanveer |
11-18-2007 04:00 AM |
send mail with inline images along with html in perl
Hi,
I am willing to send an auto reply with Perl and procmail for auto reply.
My procmail file is as follows:
Code:
PMDIR=$HOME/procmail
#The log of this procmail rc will store
#in user'home directory/procmail/log file
LOGFILE=$PMDIR/log
VERBOSE=no
LOG=" "
SHELL=/bin/sh # for other shells, this might need adjustment
:0 Whc: vacation.lock
* !^FROM_DAEMON
#This portion will not generate any email auto reply to DAEMON
* !^X-Loop: user@xxx.com
# This will not generate any email auto reply to
| formail -trD 8192 vacation.cache
:0 ehc # if the name was not in the cache
* ? test ! -f flag
#this portion will check if the flag file exist or not if not exist then it will work
| (formail -trI"Precedence: junk" \
-A"X-Loop: user@xxx.com"; \
) | /home/user/scripts/script.pl
If I use this script then it sends the mail with the htm file as body but at the place of image it shows a cross like missing file.
Code:
script.pl
#!/usr/bin/perl
use MIME::Lite;
use Net::SMTP;
my $toline="";
my $subject="";
while (<>)
{
$toline=$_ if /^To:/;
$subject=$_ if /^Subject:/;
}
my @ar=split(/ /,$toline);
my $ToAddress=$ar[1];
$subject =~ s/^Subject://;
my $to_address = $ToAddress;
my $mail_host = 'current machine IP';
### Create the multipart container
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
$my_file_html="/home/user/contents/test.htm" if $subject =~ /test/i;
$msg->attach (
Type => 'text/html',
Path => $my_file_html,
Filename => $my_file_html,
) or die "Error adding $my_file_html: $!\n";
### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
Now in test.htm there are one gif files which doesn't show so I added this:
Code:
$msg->attach(
Type => 'image/gif',
Id => 'myimage.gif',
Path => '/home/user/contents/test_files/image003.gif',
);
And also changed in the test.htm file as <img src="cid:myimage">. Now it sends both the htm file and image file as attachments. Nothing is then in the mail body.
The interesting part is if use something like this then it works:
Code:
...
......
$msg->attach (
Type => 'text/html',
Data => qq{
<body>
Here's <i>my</i> image:
<img src="cid:myimage.gif">
</body>
},
) or die "Error adding $my_file_html: $!\n";
$msg->attach(
Type => 'image/gif',
Id => 'myimage.gif',
Path => '/home/user/contents/EDGE_M_files/image003.gif',
);
....
......
But if gives the htm file location then it sends as attachments as I cant write a whole htm file in the script.
Any idea on this.
thanks for your time.
|