-   Linux - General (
-   -   send mail with inline images along with html in perl (

tanveer 11-18-2007 04:00 AM

send mail with inline images along with html in perl

I am willing to send an auto reply with Perl and procmail for auto reply.

My procmail file is as follows:

#The log of this procmail rc will store
#in user'home directory/procmail/log file
 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:
# 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:";  \
                ) | /home/user/scripts/

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.

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);


Now in test.htm there are one gif files which doesn't show so I added this:

    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:


$msg->attach (

  Type => 'text/html',

  Data => qq{
                Here's <i>my</i> image:
                <img src="cid:myimage.gif">

) or die "Error adding $my_file_html: $!\n";

    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.

tanveer 12-11-2007 10:51 PM

I cant believe no one ever done this before.
I followed the script of this link & it sends html mail with inline images too.

But when I put this in my .procmailrc of users home folder for auto generated reply it gives an error log like this:


From  Tue Dec 11 12:47:52 2007
 Subject: Redhat
  Folder: /var/spool/mail/user
Can't locate in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at /home/user/scripts/ line 3.

Any help on this please.

I can run the same script if I execute it from command shell. But when try to run from .procmailrc file it gives the above error.

tanveer 12-14-2007 05:23 AM

:) finally sorted it out. Very simple thing but cost me lot pain. Should be very easy for people with little knowledge on perl as I don't have that so.... . Ne One interested, let me know.

All times are GMT -5. The time now is 12:16 PM.