View Full Version : trying to use mail() function to send an html styled email

10-27-2009, 11:55 AM
Hi im trying to use the mail() function in php to send a styled html email but when sent i just recive the code through rather then a view of the styled html email.

Is there something im meant to do with the code before putting it in the message part of the mail() function as a variable?

10-27-2009, 12:05 PM
The PHP documentation for mail() includes a basic guide for creating HTML emails, you need to add something to the headers:

'MIME-Version: 1.0' . "\r\n"
'Content-type: text/html; charset=iso-8859-1' . "\r\n"

10-27-2009, 12:07 PM
Perhaps I should expand on this a little:

// Headers:
'From: '.$from."\r\n".
'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=iso-8859-1'

10-27-2009, 02:13 PM
Ok thanks it's working now. Although it's not displaying images. Is there a way to get the imail to show images?

10-27-2009, 04:16 PM
Ahh, you want multi-part mime, which gets quite complicated. PHP documentation suggests using:


Or you could just use <img> links to a webserver. I believe hotmail won't display inline images, and most email clients won't display linked images without confirmation. Either way, you've got a fight on your hands.

10-27-2009, 04:19 PM
If you really want to investigate this matter further, you feel free to steal some code I wrote a while ago. Haven't tested it for ages, but it does illustrate basic use of multi-part mime messages, if you can understand it:

class mai_email {
private $ffrom;
private $fto;
private $fcc;
private $fbcc;
private $fattach;
private $fboundary;
private $fsubject;

function __construct() {
$this->fboundary = '----maiestasweb----boundary'.rand(99999,999999);
$this->ffrom = NULL;
$this->fto = NULL;
$this->fcc = NULL;
$this->fbcc = NULL;
$this->fsubject = 'No subject';
$this->fattach = array();

function sender($email, $name = NULL) {
if (is_null($name)) {
$this->ffrom = $email;
} else {
$this->ffrom = $name.' <'.$email.'>';

function recipient($to, $cc = NULL, $bcc = NULL) {
if (is_array($to)) { $this->fto = $to; } else { $this->fto = array($to); }
if (!is_null($cc)) if (is_array($cc)) { $this->fcc = $cc; } else { $this->fcc = array($cc); }
if (!is_null($bcc)) if (is_array($bcc)) { $this->fbcc = $bcc; } else { $this->fbcc = array($bcc); }

function subject($subj) {
$this->fsubject = $subj;

function messageastext($msg) {
$this->fattach[] =
'Content-Transfer-Encoding: quoted-printable'."\n".
'Content-Type: text/plain; charset=windows-1252'."\n".

function messageashtml($msg, $setastext = True) {
if ($setastext) $this->messageastext(strip_tags($msg));
$this->fattach[] =
'Content-Transfer-Encoding: quoted-printable'."\n".
'Content-Type: text/html; charset=windows-1252'."\n".

function attach($filename, $content, $mime = NULL) {
if (is_null($mime)) {
$fileinfo = pathinfo($filename,PATHINFO_EXTENSION);
// If the mime module is loaded, use it
if (function_exists('mai_mime_convert')) {
$mime = mai_mime_convert($fileinfo['extension']);
} else {
// Minimal essential mime types. Yes, I know it's "officially" application/x-javascript.
$mimetypes = array('jpg'=>'image/jpeg','htm'=>'text/html','html'=>'text/html','xml'=>'application/xml',
if (isset($mimetypes[$fileinfo['extension']])) {
$mime = $mimetypes[$fileinfo['extension']];
} else {
$mime = 'application/octet-stream';
$this->fattach[] =
'Content-Type: '.$mime.";\r\n".
' name="'.$filename.'"'."\r\n".
'Content-Disposition: inline;'."\r\n".
' filename="'.$filename.'"'."\r\n".
'Content-Transfer-Encoding: base64'."\r\n".

private function _headerfmt($data) {
// Safely muli-lines, indents and comma seperates header entries
return implode(",\r\n ",$data)."\r\n";

function send() {
$header =
'From: '.$this->ffrom."\r\n".
'Reply-To: '.$this->ffrom."\r\n".
'Date: '.date('r')."\r\n".
'MIME-Version: 1.0'."\r\n".
'Content-Type: multipart/alternative;'."\r\n".
' boundary='.$this->fboundary."\r\n".
'X-Mailer: Maiestas Web module mai_email '.MAI_EMAIL."\r\n".
'To: '.$this->_headerfmt($this->fto);
if (!is_null($this->fcc)) $header .= 'Cc: '.$this->_headerfmt($this->fcc);
if (!is_null($this->fbcc))$header .= 'Bcc: '.$this->_headerfmt($this->fbcc);
return mail(
implode(', ',$this->fto).(is_null($this->fcc)?'':', '.implode(', ',$this->fcc)).(is_null($this->fbcc)?'':', '.implode(', ',$this->fbcc)),
'This is a multipart message in MIME format.'."\n\n--".$this->fboundary."\n".