Re: Problems send MIME multipart/alternative mail with php

From: Date: Wed, 20 Aug 2003 13:27:04 +0000
Subject: Re: Problems send MIME multipart/alternative mail with php
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-160173@lists.php.net to get a copy of this message
> I have had no success sending multipart/alternative emails with php. I have > tried everyone's various code snippets with no luck. I test the results with > Outlook and Outlook Express. Everytime my boundary tag ends up showing as > part of my message and thus the plain text and html portions show up as one > long blob of unformatted text. Below is the code I am currently using. Any > suggestions would be greatly appreciated. > > Thanks, > > Dan > > The code: > > <?php > error_reporting(E_ALL); > //FUNCTION future > multipartmailer($to,$from,$subject,$plaintextsource,$htmlsource); > //$boundry="**=-=-=D-=-=-=-MIME-A-Boundry-=-=-N=-=-=-**"; > $boundry= "---=".uniqid("MAILDIVIDERS"); > //set mime type > > $headers="From: Person <ppersom@mail.com>\n"; > $headers.= "MIME-Version: 1.0\n"; > $headers.="Content-Type: multipart/alternative; > boundary=\"$boundry\"\n"; > //these files have the raw message content > $plaintext = file("plaintextcontent.txt"); > $html = file("htmlcontent.txt"); > //warning for non-MIME lovin' clients > > $headers .= "This message is in MIME format. Since your mail reader does not > understand this format, some or all of this message may not be > legible.\n\r\n\r"; > > // CHOP > $message .= $boundry."\n"; > $message .= "Content-Type: text/plain; charset=ISO-8859-1\n"; > $message .= "Content-Transfer-Encoding: 8bit\n\r"; > foreach($plaintext as $line) { >     $message .=$line; > } > $message .="\n".$boundry."\n"; > $message .= "Content-Type: text/html; charset=ISO-8859-1\n"; > $message .= "Content-Transfer-Encoding: 8bit\n"; > foreach($html as $line) { >     $message .=$line; > } > $message .="\n".$boundry."\n"; > mail("me@me.com","Welcome to the Website",$message,$headers); > print $headers."".$message."<br>"; > > ?> > </body> > </html I also had a very hard time sending HTML-formatted email as multipart. Following are some code samples from what eventually worked for me. I think you can have lots of variations in the headers without problems, and that your main problem is how you're using the boundary. VERY IMPORTANT: Note how I'm adding "--" before the boundary when it's used to separate things, and how the last use of the boundary is followed by another "--". // define a boundary strings to use in multipart email $OB = "----=_OuterBoundary_000"; // set multipart/alternative headers $multiheaders = "From: $from_name<$from_email>\n"; if ($reply_to_email != "") { $multiheaders .= "Reply-To: " . $reply_to_name . "<" . $reply_to_email . ">\n"; } $multiheaders .= "Return-Path: <$from_email>\n"; $multiheaders .= "X-Mailer: PHP4\n"; $multiheaders .= "MIME-Version: 1.0\n"; $multiheaders .= "Content-Type: multipart/alternative;\n\tboundary=\"" . $OB . "\"\n\n"; $multiheaders .= "--" . $OB . "\n"; $multiheaders .= "Content-Type: text/plain; charset=us-ascii\n"; $multiheaders .= "Content-Transfer-Encoding: 7bit\n"; $multiheaders .= "Content-Disposition: inline\n\n"; My code samples are all chopped up because they're part of a queuing system.. I create a plain text message and an HTML-formatted message and write everything to a MySQL database. A separate script that's called by a crontab actually sends the email. (That let's me control frequency and volume.) After pulling from the database, I build the multipart message from the plain text message, the boundary, some more specific headers, the HTML-formatted message. // build multipart/alternative $multipart_message = $message . "\n\n--" . $boundary . "\n" . "Content-Type: text/html; charset=us-ascii" . "\n" . "Content-Disposition: inline" . "\n\n" . html_entity_decode($html_message) . "\n\n--" . $boundary . "--\n\n--End--\n\n\n"; if (!@mail("$fullname<$email>", $subject, $multipart_message, $multiheaders)) { // some stuff for error checking } Hopefully you can decipher my example. -- Lowell Allen

« previous php.general (#160173) next »