knexus has asked for the wisdom of the Perl Monks concerning the following question:

All,

I am trying to use the MIME::Lite::HTML package to email web pages and include e-mail attachments. I have no problems using MIME::Lite to send mail with attachments and no problem using MIME::Lite::HTML to send a web page.

I figured that MIME::Lite::HTML inherits from MIME::Lite so I thought I'd be able to use MIME Lite methods to add the attachments... but apparently I do not understand the mechanism well enough. Sorry if this is a dumb mistake, it's just been a long "short" week. ;-(

Most of the code is straight from the Module's DOCs.
Could someone tell me if what I want to do is "do-able" and if so what's wrong with the code.

Thanks

The Errors

Name "mailHTML::MIME::Lite" used only once; possible typo at mailhtml. +pl line 15. Can't call method "attach" on an undefined value at mailhtml.pl line 1 +5.

The Code

#!/usr/bin/perl -w use strict; use MIME::Lite; use MIME::Lite::HTML; my $mailHTML = new MIME::Lite::HTML From => 'Someone@anyplace.com', To => 'someoneelse@theirplace.com', Subject => 'Search here if you want!', 'X-Priority'=> 1, 'X-MSMail-Priority' => 'High', ; $mailHTML::MIME::Lite->attach( Type =>'text/html; charset="iso-8859-1" +', Data => '<b>Brought to you now!</b>', ); my $URL = 'http://google.com'; my $MIMEmail = $mailHTML->parse("$URL"); $MIMEmail->send_by_smtp('smtp-server.cfl.rr.com');

Replies are listed 'Best First'.
Re: MIME Lite HTML with Attachments
by snax (Hermit) on Sep 05, 2003 at 23:22 UTC
    Your errors tell part of the story, and it looks like you know better, too :)

    You have

    $mailHTML::MIME::Lite->attach( Type =>'text/html; charset="iso-8859-1" +', Data => '<b>Brought to you now!</b>', );
    which should be
    $mailHTML->attach( Type =>'text/html; charset="iso-8859-1"', Data => '<b>Brought to you now!</b>', );
    precisely for the same reasons you used it as
    my $MIMEmail = $mailHTML->parse("$URL");
    towards the end of your script. The token (is that the right word?) $mailHTML has all the methods "embedded" -- you don't need to try to specify which parts of which inherited interfaces you want to play with; it has them "natively" so to speak.

    This presupposes that the ::Lite extension does properly inherit, of course.

      I agree the errors usually tell the story but for some reason the message hasn't been getting thru to me :)
      Also, If I appear to know better, please don't let it fool you, I am new to perl (couple of weeks or so) but have worked in several other langs... sometimes I think that may actually be baggage for me ;-)

      Anyhow, I had already tried the approach you mentioned, seemed natual. However, I got the following error:

      $mailHTML->attach( Type =>'text/html; charset="iso-8859-1"', Data => '<b>Brought to you now!</b>', ); Can't locate object method "attach" via package "MIME::Lite::HTML" at +mailhml.pl line 15.

      So that's when I tried "scoping" the reference (terminology???) to get at the method I want.

      Perhaps, your statement

      This presupposes that the ::Lite extension does properly inherit, of course.

      I guess I will go peruse the source. Any other thoughts?

      Thanks

Re: MIME Lite HTML with Attachments
by benn (Vicar) on Sep 06, 2003 at 10:36 UTC
    The docs for MIME::Lite::HTML don't make this clear at all, and I had to look at the source to suss what was going on...

    M::L::H *doesn't* inherit from MIME::Lite - it just uses it....but the good news is that it returns a MIME::Lite object when you call the parse method, so what you actually want is this...

    my $mailHTML = new MIME::Lite::HTML From => 'Someone@anyplace.com', # etc. ; my $MIMEmail = $mailHTML->parse('http://google.com'); $MIMEmail->attach( Type =>'text/html; etc. etc); $MIMEmail->send_by_smtp('smtp-server.cfl.rr.com');
    Cheers,
    Ben.
      Thanks benn, you just beat me to it. ;-)

      After getting some sleep, I started looking at the package source and it became clear that M::L::H contians a M::L and does NOT inherit from it.

      However, parse() returns a M::L for the caller to use for sending the email. That's when it HIT ME (doh!). I should have realized what was going on by how the send() was done, ie. via $MIMEmail and not $mailHTML.
      So, things are much better now... perhaps it was the sleep? ;-}

      Anyhow, that's just another one to add to the lessons learned list.
      Plus being new to perl, it's nice get confirmation from someone else, so many thanks for the reply.