Page 1 of 1

Send Html and Text content in the same mail using php

PostPosted: Wed Apr 21, 2010 11:08 pm
by silvester
There arise some situations when we send a mail using php with HTML content and the Receiver's mail client only supports text mail then the html content will be displayed with Tags.

To solve this issue we need to send both HTML and Text content with the mail.So that the matter will be displayed according to the Mail client.
This should be practiced so that no issues occurs in the future.
The script to use is added below.


$boundary = "nextPart";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Me <sales@mysite.com>\r\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n";

//text version
$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "This is the plain version";

//html version
$headers .= "\n--$boundary\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "This is the <b>HTML</b> version";

mail("test@techwyseintl.com", "An HTML Message", "", $headers); //NB:-There is no need to pass the body content as we regularly do since its already added in the headers.

Re: Send Html and Text content in the same mail using php

PostPosted: Wed Apr 21, 2010 11:23 pm
by ashik
I think this is very useful ,because recipient will get the mail in readable format.

Re: Send Html and Text content in the same mail using php

PostPosted: Thu Apr 22, 2010 12:05 am
by jay
Good Script Silvester !

So from now onwards we don't have to worry whether the client email software supports HTML or NOT !