For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Controlling email built with MIME::Lite?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Controlling email built with MIME::Lite?
Kevin Zembower

2006-11-17, 9:57 pm

I'm trying to send an HTML (boss' demand) using MIME::Lite. The program
I'm using is pasted in below my signature. When I comment out all the
'$msg->attach' lines, the body of the email message appears as HTML in
my Outlook email reader. As soon as I uncomment just one of the
'$msg->attach' lines, both the new attachment and the file 'index.htm'
appear as attachments, and the body of the email appears blank.

Is this behavior just an artifact of the way Outlook displays it? Can
this behavior be controlled, to put the file 'index.htm' in the body and
attach the images?

Thanks a lot for all your help and advice.

-Kevin

Kevin Zembower
Internet Services Group manager
Center for Communication Programs
Bloomberg School of Public Health
Johns Hopkins University
111 Market Place, Suite 310
Baltimore, Maryland 21202
410-659-6139=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
#! /usr/bin/perl -w
#
#This program takes a list of email addresses and emails them.
#NOTE: This program must be changed for each edition of the newsletter
#

use strict;

use MIME::Lite;

while (<> ) {
my $msg =3D MIME::Lite->new(
To =3D>"$_",
Subject =3D>'New phase of funding to support life-saving
vaccines',
Type =3D>'text/html',
Path =3D>'index.htm',
);
=20
#$msg->attach(Type =3D>'text/html', Path =3D>'index.htm');
$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/Hivheader1.jpg',
Disposition=3D>'attachment');
#$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/image001.jpg',
Disposition=3D>'attachment');
#$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/image002.jpg',
Disposition=3D>'attachment');
#$msg->attach(Type =3D>'image/gif', Path =
=3D>'index_files/image003.gif',
Disposition=3D>'attachment');
#$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/image004.jpg',
Disposition=3D>'attachment');
#$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/image005.jpg',
Disposition=3D>'attachment');
#$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/image006.jpg',
Disposition=3D>'attachment');
=20
$msg->send("sendmail","/usr/bin/qmail-inject");
}; #while there are more address on the command line
Kevin Zembower

2006-11-17, 9:57 pm

Dave, thanks for your suggestion. I've tried this, but without success.
I think I've almost duplicated the example given in the docs, with the
script pasted in at the end of this message, but I get the same result,
with both parts of the message attached. The only difference might be
the use of 'Data' rather than 'Path', which I'll check out next.

Thanks, again, for your suggestion.

-Kevin

-----Original Message-----
From: David Van Ginneken [mailto:davevg@gmail.com]=20
Sent: Friday, November 17, 2006 10:39 AM
To: Zembower, Kevin
Subject: Re: Controlling email built with MIME::Lite?

Check the MIME::Lite docs, specifically the section about Inline images.

http://search.cpan.org/~yves/MIME-L....pm#Send_an_HTM
L_document..._with_images_included!

On 11/17/06, Zembower, Kevin <kzembowe@jhuccp.org> wrote:
> I'm trying to send an HTML (boss' demand) using MIME::Lite. The

program
> I'm using is pasted in below my signature. When I comment out all the
> '$msg->attach' lines, the body of the email message appears as HTML in
> my Outlook email reader. As soon as I uncomment just one of the
> '$msg->attach' lines, both the new attachment and the file 'index.htm'
> appear as attachments, and the body of the email appears blank.
>
> -Kevin
>
> =

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> #! /usr/bin/perl -w
> #
> #This program takes a list of email addresses and emails them.
> #NOTE: This program must be changed for each edition of the newsletter
> #
>
> use strict;
>
> use MIME::Lite;
>
> while (<> ) {
> my $msg =3D MIME::Lite->new(
> To =3D>"$_",
> Subject =3D>'New phase of funding to support life-saving
> vaccines',
> Type =3D>'text/html',
> Path =3D>'index.htm',
> );
>
> #$msg->attach(Type =3D>'text/html', Path =3D>'index.htm');
> $msg->attach(Type =3D>'image/jpg', Path

=3D>'index_files/Hivheader1.jpg',
> Disposition=3D>'attachment');
> #$msg->attach(Type =3D>'image/jpg', Path

=3D>'index_files/image001.jpg',
> Disposition=3D>'attachment');
> #$msg->attach(Type =3D>'image/jpg', Path

=3D>'index_files/image002.jpg',
> Disposition=3D>'attachment');
> #$msg->attach(Type =3D>'image/gif', Path

=3D>'index_files/image003.gif',
> Disposition=3D>'attachment');
> #$msg->attach(Type =3D>'image/jpg', Path

=3D>'index_files/image004.jpg',
> Disposition=3D>'attachment');
> #$msg->attach(Type =3D>'image/jpg', Path

=3D>'index_files/image005.jpg',
> Disposition=3D>'attachment');
> #$msg->attach(Type =3D>'image/jpg', Path

=3D>'index_files/image006.jpg',
> Disposition=3D>'attachment');
>
> $msg->send("sendmail","/usr/bin/qmail-inject");
> }; #while there are more address on the command line
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


#New, changed program:
#! /usr/bin/perl -w

use strict;

use MIME::Lite;

while (<> ) {
my $msg =3D MIME::Lite->new(
To =3D>"$_",
Subject =3D>'New phase of funding to support life-saving
vaccines',
Type =3D>'multipart/related',
);

$msg->attach(Type =3D>'text/html', Path =3D>'index.htm',
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/Hivheader1.jpg',
Disposition=3D>'attachment');

$msg->send("sendmail","/usr/bin/qmail-inject");
}; #while there are more address on the command line
Kevin Zembower

2006-11-17, 9:57 pm

Dave, you're the best. For some reason, MIME::Lite behaves differently =
if the data is in a Data or Path class. For archive reference, I've =
pasted in my final working program at the end of this note.

Especially useful was your suggestion regarding the <img src=3D...> tag =
contents. I found that I didn't need the "cid:" tag, but did need to =
change the link so that they were relative and that the images were at =
the same directory level (the current directory) as the message itself. =
In other words, the tag had to be similar to "<img =
src=3D"Hivheader1.jpg" alt=3D"Hib Focus Alert">" rather than "<img =
src=3D"images/Hivheader1.jpg" alt=3D"Hib Focus Alert">".

Dave, thanks, again, for your excellent suggestions. I owe you a beer.

-Kevin

________________________________________

From: David Van Ginneken [mailto:davevg@gmail.com]=20
Sent: Friday, November 17, 2006 11:52 AM
To: Zembower, Kevin
Subject: Re: Controlling email built with MIME::Lite?

I just did this using the docs, and it worked in my outlook (2003 =
sp2).=A0 Hope it helps.

#!/usr/bin/perl
use strict;
use MIME::Lite;



my=A0=A0 $msg =3D MIME::Lite->new(
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 From=A0=A0=A0=A0 =3D>' =
test@myemail.com',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 To=A0=A0=A0=A0=A0 =
=3D>'test@myemail.com',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 Subject =3D>'HTML with =
in-line images!',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 Type=A0=A0=A0 =
=3D>'multipart/related'=20
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 );
=A0=A0=A0 $msg->attach(Type =3D> 'text/html',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 Data =3D> qq{ <body>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0 Here's <i>my</i> image:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0 <img src=3D"cid: casey.jpg">
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0 </body> }
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 ) or die;
=A0=A0=A0 $msg->attach(Type =3D> 'image/jpeg',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 Id=A0=A0 =3D> =
'myimage.gif',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 Path =3D> =
'/home/davevg/casey.jpg',=20
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
A0=A0=A0 ) or die;
=A0=A0=A0 $msg->send() or die;
On 11/17/06, David Van Ginneken <davevg@gmail.com > wrote:
Maybe try to slurp your html file into a perl variable and include it in =
the data section?=A0=A0Also, note the use of <img src=3D" =
cid:myimage.gif"> in the html and " Id=A0=A0 =3D> 'myimage.gif'," for =
the image attachment.=A0=20


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=

#working program:
#! /usr/bin/perl -w
#
#This program takes a list of email addresses and emails them.
#NOTE: This program must be changed for each edition of the newsletter
#

use strict;
use MIME::Lite;

open(INDEX, "<index.htm") or die "Can't open index.htm file";
my $body =3D do { local $/; <INDEX> }; #Slurp the whole file in
close(INDEX);

my $msg =3D MIME::Lite->new(
Subject =3D>'New phase of funding to support life-saving =
vaccines',
Type =3D>'multipart/related',
);
=20
$msg->attach(Type =3D>'text/html', Data =3D>$body, =
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =
=3D>'index_files/Hivheader1.jpg', Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =3D>'index_files/image001.jpg', =
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =3D>'index_files/image002.jpg', =
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/gif', Path =3D>'index_files/image003.gif', =
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =3D>'index_files/image004.jpg', =
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =3D>'index_files/image005.jpg', =
Disposition=3D>'inline');
$msg->attach(Type =3D>'image/jpg', Path =3D>'index_files/image006.jpg', =
Disposition=3D>'inline');
=20
while (<> ) {
$msg->add(To=3D>"$_");
$msg->send("sendmail","/usr/bin/qmail-inject");
}; #while there are more address on the command line
Mumia W.

2006-11-17, 9:57 pm

On 11/17/2006 09:07 AM, Zembower, Kevin wrote:
> I'm trying to send an HTML (boss' demand) using MIME::Lite. The program
> I'm using is pasted in below my signature. When I comment out all the
> '$msg->attach' lines, the body of the email message appears as HTML in
> my Outlook email reader. As soon as I uncomment just one of the
> '$msg->attach' lines, both the new attachment and the file 'index.htm'
> appear as attachments, and the body of the email appears blank.
>
> Is this behavior just an artifact of the way Outlook displays it? Can
> this behavior be controlled, to put the file 'index.htm' in the body and
> attach the images?
>
> Thanks a lot for all your help and advice.
>
> -Kevin
>
> Kevin Zembower
> Internet Services Group manager
> Center for Communication Programs
> Bloomberg School of Public Health
> Johns Hopkins University
> 111 Market Place, Suite 310
> Baltimore, Maryland 21202
> 410-659-6139
> ========================================
====================
> #! /usr/bin/perl -w
> #
> #This program takes a list of email addresses and emails them.
> #NOTE: This program must be changed for each edition of the newsletter
> #
>
> use strict;
>
> use MIME::Lite;
>
> while (<> ) {
> my $msg = MIME::Lite->new(
> To =>"$_",
> Subject =>'New phase of funding to support life-saving
> vaccines',
> Type =>'text/html',
> Path =>'index.htm',
> );
>
> #$msg->attach(Type =>'text/html', Path =>'index.htm');
> $msg->attach(Type =>'image/jpg', Path =>'index_files/Hivheader1.jpg',
> Disposition=>'attachment');
> #$msg->attach(Type =>'image/jpg', Path =>'index_files/image001.jpg',
> Disposition=>'attachment');
> #$msg->attach(Type =>'image/jpg', Path =>'index_files/image002.jpg',
> Disposition=>'attachment');
> #$msg->attach(Type =>'image/gif', Path =>'index_files/image003.gif',
> Disposition=>'attachment');
> #$msg->attach(Type =>'image/jpg', Path =>'index_files/image004.jpg',
> Disposition=>'attachment');
> #$msg->attach(Type =>'image/jpg', Path =>'index_files/image005.jpg',
> Disposition=>'attachment');
> #$msg->attach(Type =>'image/jpg', Path =>'index_files/image006.jpg',
> Disposition=>'attachment');
>
> $msg->send("sendmail","/usr/bin/qmail-inject");
> }; #while there are more address on the command line
>


I don't have Outlook. When I disable viewing attachments inline, and I
specify the data to MIME::Lite using Path => '...', I get the same
results in Thunderbird as you do in Outlook (the HTML part is not
displayed).

However, if I specify the data using Data => '...', the HTML code is
viewable in Thunderbird.

use strict;
use MIME::Lite;
use File::Slurp;

die ("No source address") unless $ENV{SRCADDR};
die ("No destination addresses") unless $ENV{DSTADDR};

while ($ENV{DSTADDR} =~ m/([^:]+)/g) {
my $To = $1;
my $mime = MIME::Lite->new(
From => $ENV{SRCADDR},
To => $To,
Subject => 'MIME::Lite test message',
Type => 'text/html',
# Path => 'index.htm',
Data => scalar(read_file('index.htm')),
);

die ("object creation failed") unless $mime;

$mime->attach(Type => 'image/png', Path => 'image-2.png');

$mime->send();
}


__END__

Note, when I enable viewing attachments inline in TB, I can see the HTML
text even when I use the Path => '...' .



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com