PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


Email: Sending Dual Format (Part 3)


When we send only html email, it still have problems. How if someone is using a mail client that cannot understand HTML email. To solve this problems, it is best to send any HTML email as dual format. This means providing both a text and an HTML version in the same email. Your client can choose which version to display.

<?php
// Setting a timezone, mail() uses this.
date_default_timezone_set('America/New_York');
// recipients
$to  = "you@phpeveryday.com" . ", " ; // note the comma 
$to .= "we@phpeveryday.com"; 

// subject
$subject = "Test for HTML Format"; 

// Create a boundary string.  It needs to be unique
$sep = sha1(date('r', time()));

// Add in our content boundary, and mime type 
//specification: 
$headers .=
    "\r\nContent-Type: multipart/alternative; 
boundary=\"PHP-alt-{$sep}\"";

// additional headers
$headers .= "To: You <you@phpeveryday.com>, 
We <we@phpeveryday.com>\r\n"; 
$headers .= "From: Me \r\n"; 
$headers .= "Cc: he@phpeveryday.com\r\n"; 
$headers .= "Bcc: she@phpeveryday.com\r\n";

// Your message here:
$body =<<<EOBODY
--PHP-alt-{$sep}
Content-Type: text/plain

Hai, It's me!

--PHP-alt-{$sep}
Content-Type: text/html

<html>
<head>
<title>Test HTML Mail</title>
</head>
<body>
<font color='red'>Hai, it is me!</font>
</body>
</html>

--PHP-alt-{$sep}--
EOBODY;

// Finally, send the email
mail($to, $subject, $body, $headers);
?>


Series this article:
Email: Sending a Simple Email (Part 1)
Email: Sending a Simple HTML Mail (Part 2)
Email: Sending Dual Format (Part 3)
Email: Send Attachement Mail
PHP Email: Using Embedded Images in HTML Email
PHP Email: Sending Mass Email use BCC
PHP Email: Protecting Email Address from Spam Collectors


Tag: email Category: PHP Basic Post : November 18th 2007 Read: 5,087 Bookmark and Share

blog comments powered by Disqus


Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration