LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   send mail with inline images along with html in perl (http://www.linuxquestions.org/questions/linux-general-1/send-mail-with-inline-images-along-with-html-in-perl-600536/)

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.

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. http://www.gerd-tentler.de/tools/mim...erl/index.html

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

Code:

From user@test.com  Tue Dec 11 12:47:52 2007
 Subject: Redhat
  Folder: /var/spool/mail/user
2746
Can't locate MIMEMAIL.pm 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/mailtest.pl line 3.

Any help on this please.
FYI,

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 09:34 PM.