Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Sending Inline Images in e-mail with Mail::Sender (or getting them to print in Outlook)

by vroom (Pope)
on Mar 03, 2003 at 22:02 UTC ( #240171=perlquestion: print w/ replies, xml ) Need Help??
vroom has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm currently working on sending an HTML e-mail with inline images using Mail::Sender and running into problems. I can get the images to show up fine in Outlook but when I print the messages the images show up as the "not found" x's.

The code below is mostly shamelessly cut from the Mail::Sender docs. Also different clients seem to handle the results very differently. I'm looking for any insight into this particular problem, or best practices for getting this to work in *most* clients.

Thanks,
vroom

use Mail::Sender; my $sender = new Mail::Sender {smtp => 'smtp@blah.com', from => 'emailforyou@blah.com'}; if (ref $sender->OpenMultipart({ from => 'webmaster@ci.grand-rapids.mi.us', to => $to, subject => 'mail message', boundary => 'boundary-test-1', multipart => 'related'})) { $sender->Attach( {description => 'html body', ctype => 'text/html; charset=us-ascii', encoding => '7bit', disposition => 'NONE', file => 'file.html' }); $sender->Attach({ description => 'file1.jpg', ctype => 'image/gif', encoding => 'base64', disposition => "inline; filename=\"file1.jpg\";\r\nContent-ID: <img1>" +, file => 'file1.jpg' }); $sender->Attach({ description => 'logo.gif', ctype => 'image/gif', encoding => 'base64', disposition => "inline; filename=\"logo.gif\";\r\nContent-ID: <img2>", file => 'logo.gif' }); $sender->Close() or die "Close failed! $Mail::Sender::Error\n"; } else { die "Cannot send mail: $Mail::Sender::Error\n"; }

Update:It does in fact appear to be the Outlook Issue referenced in the KB article in bbfu's post below. I ended up using Mime::Lite as jlongino suggested since the code was somewhat more succinct.

Comment on Sending Inline Images in e-mail with Mail::Sender (or getting them to print in Outlook)
Download Code
Replies are listed 'Best First'.
Re: Sending Inline Images in e-mail with Mail::Sender (or getting them to print in Outlook)
by jlongino (Parson) on Mar 04, 2003 at 04:04 UTC
    You might want to use MIME::Lite instead. Here is some code (that I also shamelessly lifted from the docs). It viewed and printed correctly using Outlook Express. I liked the module installation and 'feel' of the code better too:
    use strict; use warnings; use MIME::Lite; my $msg = MIME::Lite->new( To =>'someone@somewhere.net', Subject =>'HTML with in-line images!', Type =>'multipart/related' ); $msg->attach(Type => 'text/html', Data => qq{ <body> Here's <i>my</i> image: <img src="cid:myimage.gif"> </body> } ); $msg->attach(Type => 'image/gif', Id => 'myimage.gif', Path => '/some/path/myimage.gif', ); $msg->send();
    Update: It also views/prints correctly under Mozilla 1.2.1

    --Jim

Re: Sending Inline Images in e-mail with Mail::Sender (or getting them to print in Outlook)
by bbfu (Curate) on Mar 04, 2003 at 03:11 UTC

    Well, a couple minor things. Firstly, you didn't include the HTML file you use (file.html), which can make a difference. The problem might lie there. Specifically, with the syntax of the src="cid:img" references.

    Also, you're manually embedding one header (Content-ID) within another. It should work the way you have it, but it is "more correct" to use the content_id parameter to Attach (see code below).

    And though I'm sure it doesn't make any difference to the problem, you're giving the wrong content-type for the jpeg ('image/gif' instead of 'image/jpeg').

    Here is your code, slightly modified and formatted, and the html file I used. It seems to work for me, though I don't actually have Outlook to test it with. As far as I can tell, this is "correct", though, so it should work the same with Outlook as with my email client (PocoMail). If this doesn't work with Outlook, then I can only assume the problem is with Outlook and printing inline images. Perhaps you could try it in another mail client to see if it's an Outlook-specific issue.

    Update: There is a Knowledge Base Article (#287768) about this issue. You should check that to see if it's the same problem.

    The code:

    #!/usr/bin/perl use warnings; use strict; use Mail::Sender; my $sender = new Mail::Sender {smtp => 'your-smtp-server', from => 'testing@example.com'}; if (ref $sender->OpenMultipart({ from => 'testing@example.com', to => $to, subject => 'mail message', boundary => 'boundary-test-1', multipart => 'related', })) { print "Attaching body...\n"; $sender->Attach({ description => 'html body', ctype => 'text/html; charset=us-ascii', encoding => '7bit', disposition => 'none', file => 'file.html', }); print "Attaching jpeg...\n"; $sender->Attach({ description => 'file1.jpg', ctype => 'image/jpeg', encoding => 'base64', disposition => 'inline; filename="file1.jpg";', content_id => 'img1', file => 'file1.jpg' }); print "Attaching gif...\n"; $sender->Attach({ description => 'logo.gif', ctype => 'image/gif', encoding => 'base64', disposition => 'inline; filename="logo.gif";', content_id => 'img2', file => 'logo.gif' }); print "Closing / Sending\n"; $sender->Close() or die "Close failed! $Mail::Sender::Error\n"; } else { die "Cannot send mail: $Mail::Sender::Error\n"; }

    The file.html:

    <html> <body> <p>Inline image number 1 (jpeg): <img src="cid:img1"> <p>Inline image number 2 (gif): <img src="cid:img2"> </body> </html>

    bbfu
    Black flowers blossum
    Fearless on my breath

      i was trying the same thing, and typed your code in and i can't get any of the images in the outlook email. they seem to be files, in the body of the email. in ms express they show up fine. is there a setting in Outlook that i should set? thanks in advance!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://240171]
Approved by mjeaton
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2015-07-31 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls