From d2d7a4d0030cdbaaa50cf477435d4757a1ee6b7d Mon Sep 17 00:00:00 2001 From: Neil Blakey-Milner Date: Mon, 6 Jun 2005 22:03:02 +0000 Subject: [PATCH] Allow emails to be sent with documents as attachments. --- lib/documentmanagement/DocumentTransaction.inc | 1 + lib/email/Email.inc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- presentation/lookAndFeel/knowledgeTree/documentmanagement/emailBL.php | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- presentation/lookAndFeel/knowledgeTree/documentmanagement/emailUI.inc | 1 + sql/mysql/upgrade/2.0.6/email_attachment_transaction_type.sql | 1 + 5 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 sql/mysql/upgrade/2.0.6/email_attachment_transaction_type.sql diff --git a/lib/documentmanagement/DocumentTransaction.inc b/lib/documentmanagement/DocumentTransaction.inc index b6bf220..fa16639 100644 --- a/lib/documentmanagement/DocumentTransaction.inc +++ b/lib/documentmanagement/DocumentTransaction.inc @@ -14,6 +14,7 @@ DEFINE("EXPUNGE", 11); DEFINE("FORCE_CHECKIN", 12); DEFINE("EMAIL_LINK", 13); DEFINE("COLLAB_ACCEPT", 14); +DEFINE("EMAIL_ATTACH", 15); /** * $Id$ * diff --git a/lib/email/Email.inc b/lib/email/Email.inc index 59e81d5..2513781 100644 --- a/lib/email/Email.inc +++ b/lib/email/Email.inc @@ -62,7 +62,7 @@ class Email { * @return boolean true on email successfully sent, false otherwise and set $_SESSION["errorMessage"] */ function send($mToEmail, $sSubject, $sBody, $sFromEmail = "", $sFromName = "") { - global $default, $lang_err_email; + global $default; // set defaults for optional params $sFromEmail = ((strlen($sFromEmail) == 0) || ($sFromEmail == "")) ? $default->system->get("emailFrom") : $sFromEmail; @@ -115,6 +115,70 @@ class Email { } /** + * Sends an email to a specified recipient. + * + * @param string the recipients email address + * @param string the subject of the email + * @param string the body of the email + * @param string the path to the document to attach + * @param string the name to the document to attach + * @return boolean true on email successfully sent, false otherwise and set $_SESSION["errorMessage"] + */ + function sendAttachment($mToEmail, $sSubject, $sBody, $sDocumentPath, $sDocumentName) { + global $default; + + // set defaults for optional params + $sFromEmail = $default->system->get("emailFrom"); + $sFromName = $default->system->get("emailFromName"); + + // set optional params + if ((strlen($sFromEmail) > 0) && ($sFromEmail != "")) { + $this->oMailer->From = $sFromEmail; + } + if ((strlen($sFromName) > 0) && ($sFromName != "")) { + $this->oMailer->FromName = $sFromName; + } + + if ( (is_string($mToEmail) && (strlen($mToEmail) > 0)) || + (is_array($mToEmail) && (count($mToEmail) > 0)) ) { + + // just one email address, add it + if (is_string($mToEmail) && (strpos($mToEmail, ";") === false)) { + $this->oMailer->AddAddress($mToEmail); + } else { + $aEmailAddresses = array(); + // if we're passed an array, then use it + if (is_array($mToEmail)) { + $aEmailAddresses = $mToEmail; + // if there are multiple addresses (; separated), explode it + } elseif (strpos($mToEmail, ";") > 0) { + $aEmailAddresses = explode(";", $mToEmail); + } + for ($i=0; $ioMailer->AddAddress($aEmailAddresses[$i]); + $default->log->debug("Email.inc adding " . $aEmailAddresses[$i]); + } + } + + $this->oMailer->Subject = stripslashes($sSubject); + $this->oMailer->Body = stripslashes($sBody); + $this->oMailer->AddAttachment($sDocumentPath, $sDocumentName); + + //send the email + if(!$this->oMailer->Send()) { + $default->log->error("Error sending mail to $mToEmail; mailer error code=" . $this->oMailer->ErrorInfo); + return PEAR::raiseError("Error sending mail to $mToEmail; mailer error code=" . $this->oMailer->ErrorInfo); + } else { + $default->log->info("Successfully sent mail to $mToEmail"); + } + return true; + } else { + // no valid email addresses supplied + return PEAR::raiseError("No valid email addresses supplied"); + } + } + + /** * Sends an email containing a hyperlink to a specified recipient * * @param The sender's email address diff --git a/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailBL.php b/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailBL.php index fea18f1..b156bd4 100644 --- a/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailBL.php +++ b/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailBL.php @@ -27,7 +27,7 @@ require_once("../../../../config/dmsDefaults.php"); -KTUtil::extractGPC('fComment', 'fDocumentID', 'fSendEmail', 'groupNewRight', 'userNewRight'); +KTUtil::extractGPC('fAttachDocument', 'fComment', 'fDocumentID', 'fSendEmail', 'groupNewRight', 'userNewRight'); require_once("$default->fileSystemRoot/lib/security/Permission.inc"); require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc"); @@ -44,7 +44,7 @@ require_once("emailUI.inc"); /** * Sends emails to the selected groups */ -function sendGroupEmails($aGroupIDs, $oDocument, $sComment = "") { +function sendGroupEmails($aGroupIDs, $oDocument, $sComment = "", $bAttachDocument) { global $default; // loop through groups @@ -62,7 +62,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = "") { if (strlen($aUsers[$j]->getEmail())>0 && $aUsers[$j]->getEmailNotification()) { //if the to address is valid, send the mail if (validateEmailAddress($aUsers[$j]->getEmail())) { - sendEmail($aUsers[$j]->getEmail(), $aUsers[$j]->getName(), $oDocument->getID(), $oDocument->getName(), $sComment); + sendEmail($aUsers[$j]->getEmail(), $aUsers[$j]->getName(), $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument); } else { $default->log->error("email validation failed for " . $aUsers[$j]->getEmail()); } @@ -79,7 +79,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = "") { /** * Sends emails to the selected users */ -function sendUserEmails($aUserIDs, $oDocument, $sComment = "") { +function sendUserEmails($aUserIDs, $oDocument, $sComment = "", $bAttachDocument) { global $default; // loop through users @@ -91,7 +91,7 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = "") { if (strlen($oDestUser->getEmail())>0 && $oDestUser->getEmailNotification()) { //if the to address is valid, send the mail if (validateEmailAddress($oDestUser->getEmail())) { - sendEmail($oDestUser->getEmail(), $oDestUser->getName(), $oDocument->getID(), $oDocument->getName(), $sComment); + sendEmail($oDestUser->getEmail(), $oDestUser->getName(), $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument); } } else { $default->log->info("either " . $oDestUser->getUserName() . " has no email address, or notification is not enabled"); @@ -105,17 +105,64 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = "") { /** * Constructs the email message text and sends the message */ -function sendEmail($sDestEmailAddress, $sDestUserName, $fDocumentID, $sDocumentName, $sComment) { +function sendEmail($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, $bAttachDocument = false) { + if ($bAttachDocument !== true) { + return sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment); + } else { + return sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment); + } +} + +function sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment) { + global $default; + global $emailerrors; + $oSendingUser = User::get($_SESSION["userID"]); + + $sMessage = 'Your colleague, ' . $oSendingUser->getName() . ', wishes you to view the attached document entitled "' . $sDocumentName . '".'; + $sMessage .= "\n\n"; + if (strlen($sComment) > 0) { + $sMessage .= "

Comments:
$sComment"; + } + $sTitle = "Document: " . $sDocumentName . " from " . $oSendingUser->getName(); + $oEmail = new Email(); + $oDocument = Document::get($iDocumentID); + $sDocumentPath = $oDocument->getPath(); + $sDocumentFileName = $oDocument->getFileName(); + $res = $oEmail->sendAttachment($sDestEmailAddress, $sTitle, $sMessage, $sDocumentPath, $sDocumentFileName); + if (PEAR::isError($res)) { + $default->log->error($res->getMessage()); + $emailerrors[] = $res->getMessage(); + return $res; + } else if ($res === false) { + $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress"); + $emailerrors[] = "Error sending email ($sTitle) to $sDestEmailAddress"; + return PEAR::raiseError("Error sending email ($sTitle) to $sDestEmailAddress"); + } else { + $default->log->info("Send email ($sTitle) to $sDestEmailAddress"); + } + + // emailed link transaction + $oDocumentTransaction = & new DocumentTransaction($iDocumentID, "Document link emailed to $sDestEmailAddress", EMAIL_ATTACH); + if ($oDocumentTransaction->create()) { + $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID"); + } else { + $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$iDocumentID"); + } +} + +function sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment) { global $default; global $emailerrors; $oSendingUser = User::get($_SESSION["userID"]); $sMessage = ""; - $sMessage .= $sDestUserName . ",

"; + if ($sDestUserName) { + $sMessage .= $sDestUserName . ",

"; + } $sMessage .= "Your colleague, " . $oSendingUser->getName() . ", wishes you to view the document entitled '" . $sDocumentName . "'.\n "; $sMessage .= "Click on the hyperlink below to view it."; // add the link to the document to the mail - $sMessage .= "
" . generateControllerLink("viewDocument", "fDocumentID=$fDocumentID", $sDocumentName); + $sMessage .= "
" . generateControllerLink("viewDocument", "fDocumentID=$iDocumentID", $sDocumentName); // add optional comment if (strlen($sComment) > 0) { $sMessage .= "

Comments:
$sComment"; @@ -131,18 +178,18 @@ function sendEmail($sDestEmailAddress, $sDestUserName, $fDocumentID, $sDocumentN return $res; } else if ($res === false) { $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress"); - $emailerrors[] = "Error sending email ($sTitle) to $sDestEmailAddress"); + $emailerrors[] = "Error sending email ($sTitle) to $sDestEmailAddress"; return PEAR::raiseError("Error sending email ($sTitle) to $sDestEmailAddress"); } else { $default->log->info("Send email ($sTitle) to $sDestEmailAddress"); } // emailed link transaction - $oDocumentTransaction = & new DocumentTransaction($fDocumentID, "Document link emailed to $sDestEmailAddress", EMAIL_LINK); + $oDocumentTransaction = & new DocumentTransaction($iDocumentID, "Document link emailed to $sDestEmailAddress", EMAIL_LINK); if ($oDocumentTransaction->create()) { - $default->log->debug("emailBL.php created email link document transaction for document ID=$fDocumentID"); + $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID"); } else { - $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$fDocumentID"); + $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$iDocumentID"); } } @@ -164,9 +211,9 @@ if (checkSession()) { //if we're going to send a mail, first make there is someone to send it to if ((count($aGroupIDs) > 1) || (count($aUserIDs) > 1)) { // send group emails - sendGroupEmails($aGroupIDs, $oDocument, $fComment); + sendGroupEmails($aGroupIDs, $oDocument, $fComment, (boolean)$fAttachDocument); // send user emails - sendUserEmails($aUserIDs, $oDocument, $fComment); + sendUserEmails($aUserIDs, $oDocument, $fComment, (boolean)$fAttachDocument); if (count($emailerrors)) { $_SESSION['errorMessage'] = join("
\n", $emailerrors); diff --git a/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailUI.inc b/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailUI.inc index 37930e2..412bc05 100644 --- a/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailUI.inc +++ b/presentation/lookAndFeel/knowledgeTree/documentmanagement/emailUI.inc @@ -91,6 +91,7 @@ function getDocumentEmailPage($oDocument) { $sToRender .= renderUserPicker(); $sToRender .= ""; $sToRender .= "\n"; + $sToRender .= "";//\n"; $sToRender .= "";//\n"; $sToRender .= "\n"; $sToRender .= "
" . _("Attach Document") . "
" . _("Comment") . "
rootUrl/control.php?action=viewDocument&fDocumentID=" . $oDocument->getID() . "\">
\n"; diff --git a/sql/mysql/upgrade/2.0.6/email_attachment_transaction_type.sql b/sql/mysql/upgrade/2.0.6/email_attachment_transaction_type.sql new file mode 100644 index 0000000..e7dfd77 --- /dev/null +++ b/sql/mysql/upgrade/2.0.6/email_attachment_transaction_type.sql @@ -0,0 +1 @@ +INSERT INTO document_transaction_types_lookup VALUES (15, 'Email Attachment'); -- libgit2 0.21.4