Date: Sat. Jan. 28 2006
Description
------------------------------------------------------------------
------------------------------------------------------------------
The application, to send emails, will do the following:
$mailObj = new xmlMail();
$mailObj->getHandle();
$mailObj->writeEmail($to,$cc,$bcc,$from,$from,$subject,$body);
$mailObj->closeHandle();
The email will be written to send.xml. The idea is to have all emails sent
in batches. Every x minutes send.xml will be accessed and the emails
subsequently sent.
.......
........
If you are using this script on two different computers (client/server) copy the same file
To learm more about the functions read below..
------------------------------------------------------------------
*/
class xmlMail {
// the file handler to the next send.xml
var $xml;
// the path to send.xml - make sure the folder is writable
var $xmlPath = "send.xml";
function xmlMail() {}
// get the handle on send.xml
// pointer at end of file, opened for write only
function getHandle() {
$init = false;
if (!file_exists($this->xmlPath)) { $init = true; }
if (!$this->xml = fopen($this->xmlPath, 'a')) {
echo "Cannot open file ($this->xmlPath)";
exit();
} else {
// the file is not there, most likely it was just sent, re-create it
if ($init == true) { $this->initFile(); }
}
}
// initialize the fresh file, make it somewhat XML compliant
function initFile() {
if (fwrite($this->xml, "") === FALSE) {
echo "Cannot write to file ($this->xmlPath)";
exit();
}
}
// end the send.xml file -- this will allow it to be viewed and parsed
// successfully
function endFile() {
$this->getHandle();
if (fwrite($this->xml, "") === FALSE) {
echo "Cannot write to file ($this->xmlPath)";
exit();
}
$this->closeHandle();
}
// close the handle, important to call this when done
function closeHandle() {
fclose($this->xml);
}
// xml files is a great way to keep a log of emails sent.
// the idea, is just before the emails are sent, then send.xml will be
// appeneded to xx-xx-xx.xml (the current day)
function backUpMail() {
$xmlBackup = date('m-d-y').".xml";
if (!$backup = fopen($xmlBackup, 'a')) {
echo "Cannot open file ($xmlBackup)";
exit();
}
if (!$current = fopen($this->xmlPath, 'r')) {
echo "Cannot open file ($this->xmlPath)";
exit();
}
while (!feof($current)) {
if (fwrite($backup,fread($current, 8192)) === FALSE) {
echo "Backup copy did not work ($this->xmlBackup)";
exit();
}
}
fclose($backup);
fclose($current);
}
// after send.xml is processed, you definitly do not want to have
// the current emails remaining in send.xml. call resetMail() to clear
// the current send.xml file and re-initialize it
function resetMail() {
if (file_exists($this->xmlPath)) {
unlink($this->xmlPath);
} else {
echo "Cannot delete file file ($this->xmlPath)";
exit();
}
}
// sending an email (or writing it to send.xml) is as easy as calling this
// function. see the description above.
function writeEmail($to, $cc, $bcc, $from, $subject, $body) {
if (fwrite($this->xml, "$to$cc$bcc$from$subject") === FALSE) {
echo "Cannot write to file ($this->xmlPath)";
exit();
}
}
// function to send the emails. Uses a really fast way to parse the XML
// this is definitly a great way to parse the XML quickly
// the PEAR Mail Package
function sendMail() {
include('Mail.php');
$webPath = "http://xxxx/send.xml";
$params['sendmail_path'] = '/usr/lib/sendmail';
$batch = new DOMDocument();
$batch->load($webPath);
$emails = $batch->getElementsByTagName("email");
foreach( $emails as $email ) {
$recipients['To'] = $headers['To'] = $email->getElementsByTagName("to")->item(0)->nodeValue;
$recipients['Cc'] = $email->getElementsByTagName("cc")->item(0)->nodeValue;
$recipients['Bcc'] = $email->getElementsByTagName("bcc")->item(0)->nodeValue;
$headers['From'] = $email->getElementsByTagName("from")->item(0)->nodeValue;
$headers['Subject'] = $email->getElementsByTagName("subject")->item(0)->nodeValue;
$body = $email->getElementsByTagName("body")->item(0)->nodeValue;
$mail_object =& Mail::factory('sendmail', $params);
$mail_object->send($recipients, $headers, $body);
}
}
}
// to send mail
// -------------------------------
// $mailObj = new xmlMail();
// $mailObj->endFile();
// $mailObj->backUpMail();
// $mailObj->sendMail();
// $mailObj->resetMail()
?>