Forum Moderators: coopster & phranque

Message Too Old, No Replies

Script help with multipart/alternative MIME issue

         

kelly001

1:31 am on Oct 27, 2002 (gmt 0)

10+ Year Member



Does anyone know what I need to do to this piece of script so that it enables me to send emails in the multipart/alternative format instead of the plain old text/html? thanks~

#!/usr/local/bin/perl

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
else { $INPUT{$name} = $value; }
}

$pid = fork();
print "Content-type: text/html \n\n fork failed: $!" unless defined $pid;

if ($pid) {

print "Content-type: text/html \n\n";

print "<html><head><title>List Administration</title></head><body>
<br><br><br><center>
<table width=280><tr><td width=280 valign=top>
<table width=\"280\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\" bordercolor=\"#808080\" bordercolorlight=\"black\" bordercolordark=\"white\">
<tr><td bgcolor=\"#0000A0\">
<font face=\"arial\" size=\"2\" color=\"white\"><b>Send Status</b>
</td></tr><tr><td bgcolor=\"#C0C0C0\"><font face=\"arial\" size=\"2\">
<br><center>Success!<BR>The message was sent.
<P><FORM><INPUT TYPE=BUTTON VALUE=\"Return to MLM\" onClick=\"history.go(-1)\"></form>
</td></tr></table></td></tr></table>
</center></body></html>";
exit(0);
}
else {

close (STDOUT);
open(LIST,"$INPUT{'address_file'}");
@addresses=<LIST>;
close(LIST);

foreach $line(@addresses) {
chomp($line);

open(MAIL, "¦$INPUT{'mail_prog'} -t") ¦¦ &error("Could not send out emails");
print MAIL "To: $line \n";
print MAIL "From: $INPUT{'listname'} <$INPUT{'from'}>\n";
print MAIL "Subject: $INPUT{'subject'}\n";
if ($INPUT{'html'}) {
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: text/html;\n";
print MAIL "Content-Transfer-Encoding: 7bit\n\n";
}
print MAIL "$INPUT{'body'}";
if ($INPUT{'html'}) {
print MAIL "<p>";
} else {
print MAIL "\n\n";
}
if ($INPUT{'unsubchk'}) {
print MAIL "===========================================================\n";
print MAIL "You have received this email because you subscribed to the $INPUT{'listname'} Mailing Service. ";
if ($INPUT{'html'}) {
print MAIL "To unsubscribe <a href=\"$INPUT{'url'}/unsub.php?list=$INPUT{'list'}&email=$line\">click here</a>\n";
} else {
print MAIL "To unsubscribe simply visit the link below.\n";
print MAIL "$INPUT{'url'}/unsub.php?list=$INPUT{'list'}&email=$line\n";
}
}
print MAIL "\n\n";
close (MAIL);
}
}

andreasfriedrich

11:53 am on Oct 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] Kelly001.

You have come to a great place with lots of helpful people around to answer your questions. Unfortunately the type of question you asked is one that is discouraged in this forum.

There is a nice introduction to Sending MIME e-mail from PHP [zend.com] at zend.com. Contrary to what the title may suggest, these articles are not PHP specific. If you want to learn more about MIME types this is a great place to start.

Adjusting your script to send multipart/alternative mails would involve changing the mail headers at the following lines:

print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: text/html;\n";
print MAIL "Content-Transfer-Encoding: 7bit\n\n";

print MAIL "MIME-Version: 1.0\n";  
print MAIL "Content-Type: multipart/alternative;\n";
print MAIL qq{boundary="aaron-14";\n};

Then you would need to start each part with "--aaron-14" and the Content-Type and Content-Transfer-Encoding headers specific to that part.

End your message with the boundary string followed by two dashes.

Hope this helps

Andreas

kelly001

4:01 pm on Oct 27, 2002 (gmt 0)

10+ Year Member



Hey Andreas,

Thanks for that. Do I need to also change this part of the script to reflect the multipart/alternative Content-type?


$pid = fork();
print "Content-type: text/html \n\n fork failed: $!" unless defined $pid;

if ($pid) {

print "Content-type: text/html \n\n";

Does anything need to be done on the server side of things to allow for multipart/alternative content-type to be processed correctely?
So far, I tried modifying the script as you said and send a email through and the email got delivered but as plain text without taking the boundaries into account. (both the plain text and html version of the email were displayed) I'm confused...

andreasfriedrich

4:13 pm on Oct 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do I need to also change this part of the script

No you donīt, since that is part of the header for the HTML document you are generating with print "<html><head><title>List Administration</title></head><body>...

Does anything need to be done on the server side of things to allow for multipart/alternative content-type to be processed correctely?

No if you can send mail you can send multipart/alternative mail as well. All processing of the multipart mail needs to be done by the mail user agent (MS Outlook, Eudora, etc).

Did you try to reproduce one of the example mails given in the article? If you canīt get it to work post the source code of the email.

Andreas

kelly001

4:38 pm on Oct 27, 2002 (gmt 0)

10+ Year Member



Yes, I tried to follow thos directives but something is not working. I tried sending the email to both Microsoft Eutourage and a hotmail account and both just display the source code for the email, which is what follows:


Content-Type: multipart/alternative;

boundary="mike-14";

--mike-14

Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hey Joe! I just thought you'd like this website:

[zend.com...]

--mike-14

Content-Type: text/html
Content-Transfer-Encoding: 7bit

<B>Hey Joe! I just thought you'd like this website:</B><BR>

<a href="http://www.zend.com/"><img src="http://www.raybrown.biz/images/ray.jpg" width="75" height="97" border="5"></a>

--mike-14--

andreasfriedrich

5:49 pm on Oct 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There seems to be no MIME-Version: 1.0 header in your code. Test again with that header in place.

Andreas

kelly001

9:05 pm on Oct 27, 2002 (gmt 0)

10+ Year Member



Hey Andreas,

I tried adding the MIME info to the source code. Not sure if I got it right though. Here is what I tried but to no avail as I still get just the source code in the body of my email in the end. What's wrong with my source email code? thanks for your help once again!


Content-Type: multipart/alternative;
MIME-Version: 1.0
boundary="mike-14";

--mike-14

Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hey Joe! I just thought you'd like this website:

[zend.com...]

--mike-14

Content-Type: text/html
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">

<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>

<body bgcolor="#000000" text="#999999">
<B>Hey Joe! I just thought you'd like this website:</B><BR>

<a href="http://www.zend.com/"><img src="http://www.raybrown.biz/images/ray.jpg" width="75" height="97" border="5"></a>
</body>
</html>

--mike-14--

cminblues

9:56 pm on Oct 27, 2002 (gmt 0)

10+ Year Member



I think a problem is in the order of the lines.

Now you have:

Content-Type: multipart/alternative; 
MIME-Version: 1.0
boundary="mike-14";

The right one must be:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="mike-14";

Ah don't forget the double CR before the first boundary
[But I think you've already made so :)].

cminblues

<added>BTW, andreasfriedrich's posts are correct about this. :) </added>

kelly001

10:31 pm on Oct 27, 2002 (gmt 0)

10+ Year Member



still can't seem to get it to work correctly. I still get the source code when I receive the email. Doesn't make the difference between the plain text or the html version.
Am I forgetting anything in this script?


#!/usr/local/bin/perl

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; }
else { $INPUT{$name} = $value; }
}

$pid = fork();
print "Content-type: text/html \n\n fork failed: $!" unless defined $pid;

if ($pid) {

print "Content-type: text/html \n\n";

print "<html><head><title>List Administration</title></head><body>
<br><br><br><center>
<table width=280><tr><td width=280 valign=top>
<table width=\"280\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\" bordercolor=\"#808080\" bordercolorlight=\"black\" bordercolordark=\"white\">
<tr><td bgcolor=\"#0000A0\">
<font face=\"arial\" size=\"2\" color=\"white\"><b>Send Status</b>
</td></tr><tr><td bgcolor=\"#C0C0C0\"><font face=\"arial\" size=\"2\">
<br><center>Success!<BR>The message was sent.
<P><FORM><INPUT TYPE=BUTTON VALUE=\"Return to MLM\" onClick=\"history.go(-1)\"></form>
</td></tr></table></td></tr></table>
</center></body></html>";
exit(0);
}
else {

close (STDOUT);
open(LIST,"$INPUT{'address_file'}");
@addresses=<LIST>;
close(LIST);

foreach $line(@addresses) {
chomp($line);

open(MAIL, "¦$INPUT{'mail_prog'} -t") ¦¦ &error("Could not send out emails");
print MAIL "To: $line \n";
print MAIL "From: $INPUT{'listname'} <$INPUT{'from'}>\n";
print MAIL "Subject: $INPUT{'subject'}\n";
if ($INPUT{'html'}) {
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: multipart/alternative;\n";
print MAIL qq{boundary="mike-14";\n};
}
print MAIL "$INPUT{'body'}";
if ($INPUT{'html'}) {
print MAIL "<p>";
} else {
print MAIL "\n\n";
}
if ($INPUT{'unsubchk'}) {
print MAIL "===========================================================\n";
print MAIL "You have received this email because you subscribed to the $INPUT{'listname'} Mailing Service. ";
if ($INPUT{'html'}) {
print MAIL "To unsubscribe <a href=\"$INPUT{'url'}/unsub.php?list=$INPUT{'list'}&email=$line\">click here</a>\n";
} else {
print MAIL "To unsubscribe simply visit the link below.\n";
print MAIL "$INPUT{'url'}/unsub.php?list=$INPUT{'list'}&email=$line\n";
}
}
print MAIL "\n\n";
close (MAIL);
}
}

cminblues

11:03 pm on Oct 27, 2002 (gmt 0)

10+ Year Member



Hmmm, I fear you've forgot the double CR..
try with this substitution:

Your actual:


print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: multipart/alternative;\n";
print MAIL qq{boundary="mike-14";\n};

The correct one:


print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: multipart/alternative; ";
print MAIL "boundary=\"mike-14\";\n\n";

cminblues

andreasfriedrich

2:15 pm on Oct 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cminblues has it correct. However, just a short reminder:

print MAIL qq{boundary="mike-14";\n\n};

is syntactically equivalent to

print MAIL "boundary=\"mike-14\";\n\n";

You just avoid having to escape those double quotes.

Andreas

cminblues

3:15 pm on Oct 28, 2002 (gmt 0)

10+ Year Member



>>is syntactically equivalent to<<
Yes.. & also the n. of chars typed.. [ -> 4 in both cases.. ok I need some relax :)]

Wrote this way for 'continuity' with previous lines of script. :)

cminblues

kelly001

2:53 pm on Oct 30, 2002 (gmt 0)

10+ Year Member



thank you both for your help. I have tried everything and nothing seems to work. I have decided to explore a different approach and instead of keeping things on the server side, I'm going to use a piece of local software. Seems that Max Bulk Mailer will do what I need to do and considering the list I'm trying to send is reletaively small (about 500) I think it will do just find going through SMTP.

Thanks again!