...

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



oli_collins
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?

Lamped
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"

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



mail(
$to,
$subject,
$message_html,
// Headers:
'From: '.$from."\r\n".
'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=iso-8859-1'
)

oli_collins
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?

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

http://pear.php.net/package/Mail_Mime

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.

Lamped
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".
"\n".
wordwrap($msg);
}

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".
"\n".
wordwrap($msg);
}

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',
'css'=>'text/css','txt'=>'text/plain','gif'=>'image/gif','png'=>'image/png','js'=>'text/javascript');
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".
"\r\n".
chunk_split(base64_encode($content));
}

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->fsubject,
'This is a multipart message in MIME format.'."\n\n--".$this->fboundary."\n".
implode("\n\n--".$this->fboundary."\n",$this->fattach)."\n\n--".$this->fboundary."--\n",
$header
);
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum