<?php
/**
* WebTemplate Core
*
* @version 2.1
* @module WebTemplate Core
*/
/**
* A Class to send email</br>
* Example:<br/>
* $mail = new WTMail();<br/>
* $mail->setFrom("contact@webtemplate.com.au", "WebTemplate");<br/>
* $mail->setTo("contact@domain.com")<br/>
* $mail->setContent("subject", "<html><body>message</body></html>", true);
* $mail->send();
*
* @class WTMail
*/
require_once($GLOBALS["WTEXTERNALDIRECTORY"] . "phpmailer/class.phpmailer.php");
if($GLOBALS["WTSITEID"] == 11477) {
$GLOBALS["WTMAILENCODING"] = "8bit";
}
class WTMail
{
var $m_from = "bouncer@webtemplate.com.au";
var $m_fromName = "WebTemplate";
var $m_replyTo = "bouncer@webtemplate.com.au";
var $m_sender = "bouncer@webtemplate.com.au";
var $m_subject = "";
var $m_body = "";
var $m_altBody = "";
var $m_contentType = "text/plain";
var $m_to = "bouncer@webtemplate.com.au";
var $m_cc = "";
var $m_bcc = "";
var $m_attachments = Array();
var $m_encrypt = false;
var $m_key = "";
var $m_embeddedImages = Array();
/**
* Set whether to encrypt the email
*
* @method setEncrypt
* @param {boolean} doEncryption Set to true to encrypt the email
* @param {String} publicKey the gnupg public key to be used to encrypt the email
*/
function setEncrypt($doEncryption,$publicKey)
{
$this->m_encrypt = $doEncryption;
$this->m_key = $publicKey;
}
function bodyEncrypt()
{
//Tell gnupg where the key ring is. Home dir of user web server is running as.
putenv("GNUPGHOME=/data/webtemplate/.gnupg");
//create a unique file name
$infile = tempnam("/tmp", "PGP.asc");
$outfile = $infile.".asc";
//write form variables to email
$fp = fopen($infile, "w");
fwrite($fp, $this->m_body);
fclose($fp);
//set up the gnupg command. Note: Remember to put E-mail address on the gpg keyring.
$command = "/usr/bin/gpg -a --homedir=/data/webtemplate/.gnupg --recipient '{$this->m_key}' --always-trust --encrypt -o $outfile $infile";
// $GLOBALS["WT"]->debugMessage("command: $command");
//execute the gnupg command
system($command, $result);
//delete the unencrypted temp file
unlink($infile);
if ($result==0) {
$fp = fopen($outfile, "r");
if(!$fp||filesize ($outfile)==0) {
$result = -1;
} else {
//read the encrypted file
$contents = fread ($fp, filesize ($outfile));
//delete the encrypted file
unlink($outfile);
//Store Encrypted content in the Email Body
$this->m_body = $contents;
}
} else {
$this->m_body = "WARNING! This message is unencrypted!<br><br>" . $this->m_body;
}
}
/**
* Set who the email will be sent to
*
* @method setTo
* @param {String} address The email address of the person to send the email to
*/
function setTo($address) {
$this->m_to = $address;
}
/**
* Set the carbon copy addresses for the email
*
* @method setCC
* @param {String} address The email address of the CC recipient (Can be a comma separated list of addresses)
*/
function setCC($address)
{
$this->m_cc = $address;
}
/**
* Set the blind carbon copy address for the email
*
* @method setBCC
* @param {String} address The email address of the BCC recipient (Can be a comma separated list of addresses)
*/
function setBCC($address)
{
$this->m_bcc = $address;
}
/**
* Set who the email is from
*
* @method setFrom
* @param {String} address The email address of the sender
* @param {String} [name] The name of the sender
*/
function setFrom($address, $name = "")
{
$this->m_from = $address;
$this->m_fromName = $name;
}
/**
* Set the reply to address of the email
*
* @method setReplyTo
* @param {String} address The reply to address of the email
*/
function setReplyTo($address)
{
$this->m_replyTo = $address;
}
/**
* Set the content of the email
*
* @method setContent
* @param {String} subject The subject of the email
* @param {String} body The body of the email
* @param {boolean} [bodyIsHTML=false] Set to false if sending a HTML email
* @param {String} altbody The plain text version of a HTML email
*/
function setContent($subject, $body, $bodyIsHTML = false, $altbody = "") {
if($bodyIsHTML) {
$this->m_contentType = "text/html";
} else {
$this->m_contentType = "text/plain";
}
$this->m_subject = $subject;
$this->m_body = $body;
$this->m_altBody = $altbody;
}
/**
* Add an attachment to the email
*
* @method addAttachment
* @param {String} path The path to the file to attach
* @param {String} [name] The name of the attachment
*/
function addAttachment($path, $name = "") {
$this->m_attachments[] = Array("path" => $path, "name" => $name);
}
/**
* Add an embedded image to the email<br/>
* TODO: provide an example
*
* @method addEmbeddedImage
* @param {String} path The path to the image
* @param {String} contentID The id used to identify the image
* @param {String} [name] The name of the image
* @param {String} [encoding="base64"] The encoding to be used for the image
* @param {String} [mimeType] The mime type. If not supplied, the mime type will be determined from the extension
*/
function addEmbeddedImage($path, $contentID, $name = "", $encoding = "base64", $type = "") {
$pathinfo = pathinfo($path);
if($name == "") {
$name = $pathinfo["basename"];
}
$extension = strtolower($pathinfo["extension"]);
if($type == "") {
$type = wtFile::getMimeType($extension);
}
$this->m_embeddedImages[] = Array("Path" => $path, "Content ID" => $contentID, "Name" => $name, "Encoding" => $encoding, "Type" => $type);
}
function getNumberQueued($id)
{
$sql = "SELECT
COUNT(*)
FROM
wtMail.tblMailQueue
WHERE
ID = $id";
$query = mysql_query($sql);
if($row = mysql_fetch_row($query)) {
return $row[0];
}
return 0;
}
function processQueue($number = 100, $id = "")
{
$idSql = "";
if($id != "") {
$idSql = " WHERE ID = $id ";
}
$sql = "SELECT
*
FROM
wtMail.tblMailQueue
$idSql
ORDER BY
Date
LIMIT
$number";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$this->setTo($row["To"]);
$this->setFrom($row["From"], $row["FromName"]);
$bodyIsHTML = $row["ContentType"] = "text/html";
$body = "";
$filename = $GLOBALS["WTMAILDIRECTORY"] . $row["BodyFile"];
$fp = fopen($filename, "r");
$body = fread($fp, filesize($filename));
fclose($fp);
$this->setContent($row["Subject"], $body, $bodyIsHTML);
$this->send();
print $row["To"] . "\n";
unlink($filename);
$filename = $GLOBALS["WTMAILDIRECTORY"] . $row["AltBodyFile"];
unlink($filename);
$sql = "DELETE FROM
wtMail.tblMailQueue
WHERE
mailQueueID = {$row["mailQueueID"]}";
mysql_query($sql);
}
}
/**
* Send the email
*
* @method send
*/
function send($queue = false, $id = "")
{
if($queue) {
$id = (int)$id;
$sql = "SELECT
COUNT(*)
FROM
wtMail.tblMailQueue
WHERE
ID = $id
AND `To` = '" . mysql_escape_string($this->m_to) . "'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
if($row[0] > 0) {
return;
}
$sql = "INSERT INTO
wtMail.tblMailQueue (`ID`, `From`, `FromName`, `ReplyTo`, `Sender`, `Subject`, `To`, `ContentType`)
VALUES
('" . mysql_escape_string($id) . "',
'" . mysql_escape_string($this->m_from) . "',
'" . mysql_escape_string($this->m_fromName) . "',
'" . mysql_escape_string($this->m_replyTo) . "',
'" . mysql_escape_string($this->m_sender) . "',
'" . mysql_escape_string($this->m_subject) . "',
'" . mysql_escape_string($this->m_to) . "',
'" . mysql_escape_string($this->m_contentType) . "')";
$count = 0;
$bodyfile = "";
while(true) {
$bodyfile = md5(uniqid());
$filepath = $GLOBALS["WTMAILDIRECTORY"] . $bodyfile;
if(!file_exists($filepath)) {
break;
}
$count++;
if($count > 1000) {
return false;
}
}
$fp = fopen($filepath, "w");
fwrite($fp, $this->m_body);
fclose($fp);
$altbodyfile = "";
while(true) {
$altbodyfile = md5(uniqid());
$filepath = $GLOBALS["WTMAILDIRECTORY"] . $altbodyfile;
if(!file_exists($filepath)) {
break;
}
$count++;
if($count > 1000) {
return false;
}
}
$fp = fopen($filepath, "w");
fwrite($fp, $this->m_altBody);
fclose($fp);
$sql = "INSERT INTO
wtMail.tblMailQueue
(`ID`,
`From`,
`FromName`,
`ReplyTo`,
`Sender`,
`Subject`,
`To`,
`ContentType`,
`BodyFile`,
`AltBodyFile`,
`Date`)
VALUES
('" . mysql_escape_string($id) . "',
'" . mysql_escape_string($this->m_from) . "',
'" . mysql_escape_string($this->m_fromName) . "',
'" . mysql_escape_string($this->m_replyTo) . "',
'" . mysql_escape_string($this->m_sender) . "',
'" . mysql_escape_string($this->m_subject) . "',
'" . mysql_escape_string($this->m_to) . "',
'" . mysql_escape_string($this->m_contentType) . "',
'" . mysql_escape_string($bodyfile) . "',
'" . mysql_escape_string($altbodyfile) . "',
NOW())";
if(!mysql_query($sql)) {
print mysql_error();
}
return mysql_insert_id();
} else {
if ($this->m_encrypt) {
$this->bodyEncrypt();
}
$mail = new PHPMailer();
$mailEncoding = "quoted-printable";
if($GLOBALS["WTMAILENCODING"]) {
$mailEncoding = $GLOBALS["WTMAILENCODING"];
}
$mail->Encoding = $mailEncoding;//"base64";
$mail->Host = "localhost";
$mail->From = $this->m_from;
$mail->FromName = $this->m_fromName;
$mail->AddReplyTo($this->m_replyTo);
$mail->Sender = $this->Sender;
$mail->Subject = $this->m_subject;
$mail->ContentType = $this->m_contentType;
$mail->Body = $this->m_body;
$mail->AltBody = $this->m_altBody;
if($this->m_bcc != "") {
$mail->AddBCC($this->m_bcc);
if($this->m_to != "bouncer@webtemplate.com.au") {
$mail->AddAddress($this->m_to);
}
} else {
$mail->AddAddress($this->m_to);
}
if($this->m_cc != "") {
$mail->AddAddress($this->m_cc);
}
if(count($this->m_attachments) > 0) {
foreach($this->m_attachments as $attachment) {
$mail->AddAttachment($attachment["path"], $attachment["name"]);
}
}
if(count($this->m_embeddedImages) > 0) {
foreach($this->m_embeddedImages as $embeddedImage) {
$mail->AddEmbeddedImage($embeddedImage["Path"], $embeddedImage["Content ID"], $embeddedImage["Name"], $embeddedImage["Encoding"], $embeddedImage["Type"]);
}
}
$mail->Send();
}
}
}
?>