Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 14th, 2005, 05:02 AM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default plain vs HTML format

Does somebody know a solution to that?

for a email application you are commonly required to send out emails in HTML format as well as in Plain text format. I figured how you can send different versions out dependent on a status which you retrieve out of a database. However, this requires that the recipient first has to specify whether he wants plain or HTML.

Is there a possibility to send out only the HTML version and the plain text recipients won't see any code, just plain text? so means that the code tags get stripped off, as well as the MIME headers.

So you wouldnt have to specify anymore what you want to receive. No matter if your email supports plain or HTML you only receive a version that shows the format applicable to your email provider.

Thanks for help

 
Old April 17th, 2005, 04:30 AM
Authorized User
 
Join Date: Apr 2005
Location: Spring Hill, Florida, USA.
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 Yes you can send a multi-part message with one part plain text and the other part containing your HTML. Clients that understand MIME, but not HTML will render the plain text version and ignore the HTML. Clients that understand HTML will render the HTML. Here is an example:

<?php
   $to="Recipient Name <somebody@example.com>";
   $from="Sender Name <jdoe@example.com>";
   $subject="Subject";
// HTML
   $mime_boundary="==Multipart_Boundary_x".md5(mt_ran d())."x";
   $headers = "From: $from\r\n" .
      "MIME-Version: 1.0\r\n" .
      "Content-Type:multipart/alternative;\n" .
      " boundary=\"{$mime_boundary}\"\r\n";
   $headers.= "From: $from\r\n";
   $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
// Plain Text Part
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
      "HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
      "<h1>HTML E-mail</h1>" .
      "<p>This is an <b>HTML</b> e-mail.</p>";
   if (mail($to, $subject, $message, $headers))
      echo "Message Sent!";
   else
      echo "Failed to send message.";
?>


     Take note that the content type is set to "multipart/alternative". Then we place boundary markers between the two versions of the message and set a content type for each part. What this means is that we're sending two versions of the message. The e-mail client can choose which of these to display. If the e-mail client understands HTML, it will display it. If it doesn't understand HTML, it will display the plain text version.

  Hope that does the trick.

Everything is temporary, some things are just more temporary than others... except for death, that seems to be pretty permanent
 
Old April 19th, 2005, 08:42 AM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cheers Bobby Fisher

seems like its working...thanks heaps for your help, saved me a lot of hours research on the internet

J





Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent - Send mail with plain view and html view ashish.dadhwal ASP.NET 2.0 Professional 0 November 27th, 2008 01:49 AM
How to show plain format from html hdpark BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 14 November 26th, 2008 05:31 PM
email in html format alitoday HTML Code Clinic 3 June 19th, 2004 08:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.