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

I've a problem with the Mail::Sender module. Basically I want to send an HTML E-mail with an embedded image to Outlook. The following code is based on the mod's documentation but isn't working, the html file and image file arrive as attachment's not as the message.

Can anyone please help?
#!/usr/local/bin/perl -w use strict; use Mail::Sender; my $sender = new Mail::Sender { smtp => 'myserver.com' }; $sender->OpenMultipart( { from => 'Jonathan@myserver.com', to => 'Jonathan@anotherbox.com', subject => 'Embedded image test', subtype => 'related', boundary => 'boundary-test-1', type => 'multipart/related'} ); $sender->SendFile ( { description => 'html body', ctype => 'text/html; charset=us-ascii', encoding => '7bit', disposition => 'NONE', file => 'test.html'} ); $sender->SendFile ( { description => 'Company logo', ctype => 'image/gif', encoding => 'base64', disposition => "inline; filename=\"picture.gif\";\r\nC +ontent-ID, file => 'picture.gif'} ); $sender->Close(); __END__ And the html file test.html <HTML> <BODY BGCOLOR=#87CEFA TEXT=#000000> <TITLE>Test message</TITLE> <IMG SRC="cid:ed1"><BR> <BIG><BIG><B><U>This is a test message</U></B></BIG></BIG> </BODY> </HTML>


"I have read your book, and it is both good and original. But what is good is not original, and what is original is not good." - Samuel Johnson

Replies are listed 'Best First'.
Re: Mail Sending me mad
by $code or die (Deacon) on Dec 14, 2000 at 23:27 UTC
    Hi,

    Try MIME::Lite::HTML. I used it last night and it's really nice and you only need a couple of lines of code. You point to the html file you want to send and it automatically adds all the graphics to the MIME email. Your HTML can be a template and you can substitute variables.

    The author told me that he just added support for background images in tables, etc.

    Sweet.
Re: Mail Sending me mad
by kilinrax (Deacon) on Dec 14, 2000 at 22:38 UTC
    You appear to be missing some stuff off the end of the 'disposition => " ... "' line, is this an artifact of cut & paste, or a possible cause of the error?
    $sender->SendFile ( { description => 'Company logo', ctype => 'image/gif', encoding => 'base64', disposition => "inline; filename=\"picture.gif\";\r\nC +ontent-ID: <logo>", file => 'picture.gif'} );
Re: Mail Sending me mad
by turnstep (Parson) on Dec 15, 2000 at 01:04 UTC

    As kilinrax has stated, the Disposition line is very important for this to work right. I would also recommend adding in some error checking. It's not your usual error checking, but simple enough:

    my $result = $sender->SendFile( { blah, blah } ); if (!ref $result) { die "Sendfile failed: $Mail::Sender::Error\n"; }
    The most common error you'll catch this way is "file not found" but who knows? maybe it will catch the wrong (or missing) Disposition problem as well.

Re: Mail Sending me mad
by jorg (Friar) on Dec 15, 2000 at 20:48 UTC
    Hi, I looked into sending xls files as attachment and stumbled across the beforementioned MIME::Lite module. It took me about 10 minutes to get the whole thing working. Here's a code snippet from the docs to show you the simplicity of the module
    use strict; use MIME::Lite; use Getopt::Std; #------------------------------ # main #------------------------------ sub main { my %opts; my $gif = $ARGV[0] || die "usage error: missing GIF path\n"; ### Create a new multipart message: my $msg = MIME::Lite->new(From => 'me@myhost.com', To => 'you@yourhost.com', Subject =>'Test the "attach to singlepart" hack', Type => 'TEXT', Data => ["This is a simple text message... ", "can we attach a file to it?\n"]); ### Attach a part: $msg->attach(Type => 'image/gif', Path => $gif); ### Output! $msg->print; } exit (&main ? 0 : -1);
    hope this helps ...
Re: Mail Sending me mad
by merlyn (Sage) on Dec 15, 2000 at 20:10 UTC
    the html file and image file arrive as attachment's not as the message.
    The problem is that you picked the wrong example from the documentation. Here's the one to model it after:
    $sender->OpenMultipart({to => 'Perl-Win32-Users@activeware.foo +', subject => 'Mail::Sender.pm - new modu +le'}); $sender->Body; $sender->Send(<<'*END*'); Here is a new module Mail::Sender. It provides an object based interface to sending SMTP mails. It uses a direct socket connection, so it doesn't need any additional program. Enjoy, Jenda *END* $sender->SendFile( {description => 'Perl module Mail::Sender.pm', ctype => 'application/x-zip-encoded', encoding => 'Base64', disposition => 'attachment; filename="Sender.zip"; type="ZIP + archive"', file => 'sender.zip' }); $sender->Close;
    You want your HTML in the body, not as an attachment.

    -- Randal L. Schwartz, Perl hacker

Re: Mail Sending me mad
by Jonathan (Curate) on Dec 15, 2000 at 14:04 UTC
    Apologies, I was more stupid than usual yesterday
    I failed to copy the disposition line for the last SendFile correctly. It actually reads

    disposition => "inline; filename=\"picture.gif\";\r\nContent-ID:<ed1> +",
    I took out the error checking to keep the problem as small as possible. I'd already checked that there were no errors when the script ran. Sorry, I should of said. Still can't get it working. :-(