php mail mime example and notes

Sending a nice, properly formatted, HTML email in PHP is relatively easy, once you've done it once. I have, and I posted such notes on Usenet. Copy attached below.

ALso, beware of using a "reply-to" header at the same time as the "BCC" header. I couldn't seem to get it working for some reason.



From: Leonard (lc__@REMOVE_UNDERSCORES__lc.yi.org)
Subject: Re: Trouble with PEAR class (Mail_Mime)


View this article only
Newsgroups: comp.lang.php
Date: 2003-07-12 01:08:14 PST
Hi Weston,

A while back I struggled with Mail_mime too. At the time the docs
didn't seem to provide a good sample HTML email that included a
graphic. So, I've pasted pared down a function I used in the
construction of Papaya Polls (http://www.papayapolls.com/index.php) at
the end of this post. I've deleted quite a lot of it, but I think you
can use it as a good reference.

I think you've missed a parameter in the "addHTMLImage" call.
Everything else looks OK to me... but I don't claim to be an expert.

Good Luck.

Leonard
http://www.perceptus.ca/


function sendConfirm($email, $username,$fname, $lname, $password){
// PEARifying to send (proper) mixed HTML/Text.
include_once('Mail.php'); // PEAR
include_once('Mail/mime.php'); // PEAR

$imagename = $GLOBALS['ESPCONFIG']['email_logo'];
// path and filename of image - FILE path, not URL
$imagefile =
$GLOBALS['ESPCONFIG']['prefix'] . '/images/' . $imagename;

$to = "$fname $lname <$email>";
$subject = "Thank you for registering with $";

/* message */
$html = "

Welcome to $ourName...


";

// way to get text format - for those not using HTML email.
$text=strip_tags($html);
//   s not stripped - do it now. =)
$text=ereg_replace(' ', ' ', $text);
// try to save a few bytes of line breaks and tabs.
$html=ereg_replace("[rtn]","",$html);

$from_name="$ourName - Customer Support";
$from_address=$csEmail;

$crlf = "rn";
$hdrs = array('From' => "$from_name <$from_address>",
'BCC' => "<$csEmail>", 'Subject' => $subject );

$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
//$mime->addAttachment($file); // works, but not used here.
$mime->addHTMLImage($imagefile, "image/jpg",$imagename);
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$ok=$mail->send($to, $hdrs, $body);
} //end f:sendConfirmed


Related Scribbles:


ID: 377
Author:
leonard
Date Updated:
2004-11-12 18:05:31
Date Created:
2003-12-18 22:47:34

Edit

Comments?
 >> Leonard Chan's Homepage  >> Scribble Web  >> php mail mime example and notes
leonard.lotus-land.ca is hosted by Perceptus Solutions Inc.