Commit d2d7a4d0030cdbaaa50cf477435d4757a1ee6b7d

Authored by Neil Blakey-Milner
1 parent 326bcfd3

Allow emails to be sent with documents as attachments.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3334 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/DocumentTransaction.inc
@@ -14,6 +14,7 @@ DEFINE("EXPUNGE", 11); @@ -14,6 +14,7 @@ DEFINE("EXPUNGE", 11);
14 DEFINE("FORCE_CHECKIN", 12); 14 DEFINE("FORCE_CHECKIN", 12);
15 DEFINE("EMAIL_LINK", 13); 15 DEFINE("EMAIL_LINK", 13);
16 DEFINE("COLLAB_ACCEPT", 14); 16 DEFINE("COLLAB_ACCEPT", 14);
  17 +DEFINE("EMAIL_ATTACH", 15);
17 /** 18 /**
18 * $Id$ 19 * $Id$
19 * 20 *
lib/email/Email.inc
@@ -62,7 +62,7 @@ class Email { @@ -62,7 +62,7 @@ class Email {
62 * @return boolean true on email successfully sent, false otherwise and set $_SESSION["errorMessage"] 62 * @return boolean true on email successfully sent, false otherwise and set $_SESSION["errorMessage"]
63 */ 63 */
64 function send($mToEmail, $sSubject, $sBody, $sFromEmail = "", $sFromName = "") { 64 function send($mToEmail, $sSubject, $sBody, $sFromEmail = "", $sFromName = "") {
65 - global $default, $lang_err_email; 65 + global $default;
66 66
67 // set defaults for optional params 67 // set defaults for optional params
68 $sFromEmail = ((strlen($sFromEmail) == 0) || ($sFromEmail == "")) ? $default->system->get("emailFrom") : $sFromEmail; 68 $sFromEmail = ((strlen($sFromEmail) == 0) || ($sFromEmail == "")) ? $default->system->get("emailFrom") : $sFromEmail;
@@ -115,6 +115,70 @@ class Email { @@ -115,6 +115,70 @@ class Email {
115 } 115 }
116 116
117 /** 117 /**
  118 + * Sends an email to a specified recipient.
  119 + *
  120 + * @param string the recipients email address
  121 + * @param string the subject of the email
  122 + * @param string the body of the email
  123 + * @param string the path to the document to attach
  124 + * @param string the name to the document to attach
  125 + * @return boolean true on email successfully sent, false otherwise and set $_SESSION["errorMessage"]
  126 + */
  127 + function sendAttachment($mToEmail, $sSubject, $sBody, $sDocumentPath, $sDocumentName) {
  128 + global $default;
  129 +
  130 + // set defaults for optional params
  131 + $sFromEmail = $default->system->get("emailFrom");
  132 + $sFromName = $default->system->get("emailFromName");
  133 +
  134 + // set optional params
  135 + if ((strlen($sFromEmail) > 0) && ($sFromEmail != "")) {
  136 + $this->oMailer->From = $sFromEmail;
  137 + }
  138 + if ((strlen($sFromName) > 0) && ($sFromName != "")) {
  139 + $this->oMailer->FromName = $sFromName;
  140 + }
  141 +
  142 + if ( (is_string($mToEmail) && (strlen($mToEmail) > 0)) ||
  143 + (is_array($mToEmail) && (count($mToEmail) > 0)) ) {
  144 +
  145 + // just one email address, add it
  146 + if (is_string($mToEmail) && (strpos($mToEmail, ";") === false)) {
  147 + $this->oMailer->AddAddress($mToEmail);
  148 + } else {
  149 + $aEmailAddresses = array();
  150 + // if we're passed an array, then use it
  151 + if (is_array($mToEmail)) {
  152 + $aEmailAddresses = $mToEmail;
  153 + // if there are multiple addresses (; separated), explode it
  154 + } elseif (strpos($mToEmail, ";") > 0) {
  155 + $aEmailAddresses = explode(";", $mToEmail);
  156 + }
  157 + for ($i=0; $i<count($aEmailAddresses); $i++) {
  158 + $this->oMailer->AddAddress($aEmailAddresses[$i]);
  159 + $default->log->debug("Email.inc adding " . $aEmailAddresses[$i]);
  160 + }
  161 + }
  162 +
  163 + $this->oMailer->Subject = stripslashes($sSubject);
  164 + $this->oMailer->Body = stripslashes($sBody);
  165 + $this->oMailer->AddAttachment($sDocumentPath, $sDocumentName);
  166 +
  167 + //send the email
  168 + if(!$this->oMailer->Send()) {
  169 + $default->log->error("Error sending mail to $mToEmail; mailer error code=" . $this->oMailer->ErrorInfo);
  170 + return PEAR::raiseError("Error sending mail to $mToEmail; mailer error code=" . $this->oMailer->ErrorInfo);
  171 + } else {
  172 + $default->log->info("Successfully sent mail to $mToEmail");
  173 + }
  174 + return true;
  175 + } else {
  176 + // no valid email addresses supplied
  177 + return PEAR::raiseError("No valid email addresses supplied");
  178 + }
  179 + }
  180 +
  181 + /**
118 * Sends an email containing a hyperlink to a specified recipient 182 * Sends an email containing a hyperlink to a specified recipient
119 * 183 *
120 * @param The sender's email address 184 * @param The sender's email address
presentation/lookAndFeel/knowledgeTree/documentmanagement/emailBL.php
@@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
27 27
28 require_once("../../../../config/dmsDefaults.php"); 28 require_once("../../../../config/dmsDefaults.php");
29 29
30 -KTUtil::extractGPC('fComment', 'fDocumentID', 'fSendEmail', 'groupNewRight', 'userNewRight'); 30 +KTUtil::extractGPC('fAttachDocument', 'fComment', 'fDocumentID', 'fSendEmail', 'groupNewRight', 'userNewRight');
31 31
32 require_once("$default->fileSystemRoot/lib/security/Permission.inc"); 32 require_once("$default->fileSystemRoot/lib/security/Permission.inc");
33 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc"); 33 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc");
@@ -44,7 +44,7 @@ require_once(&quot;emailUI.inc&quot;); @@ -44,7 +44,7 @@ require_once(&quot;emailUI.inc&quot;);
44 /** 44 /**
45 * Sends emails to the selected groups 45 * Sends emails to the selected groups
46 */ 46 */
47 -function sendGroupEmails($aGroupIDs, $oDocument, $sComment = "") { 47 +function sendGroupEmails($aGroupIDs, $oDocument, $sComment = "", $bAttachDocument) {
48 global $default; 48 global $default;
49 49
50 // loop through groups 50 // loop through groups
@@ -62,7 +62,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = &quot;&quot;) { @@ -62,7 +62,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = &quot;&quot;) {
62 if (strlen($aUsers[$j]->getEmail())>0 && $aUsers[$j]->getEmailNotification()) { 62 if (strlen($aUsers[$j]->getEmail())>0 && $aUsers[$j]->getEmailNotification()) {
63 //if the to address is valid, send the mail 63 //if the to address is valid, send the mail
64 if (validateEmailAddress($aUsers[$j]->getEmail())) { 64 if (validateEmailAddress($aUsers[$j]->getEmail())) {
65 - sendEmail($aUsers[$j]->getEmail(), $aUsers[$j]->getName(), $oDocument->getID(), $oDocument->getName(), $sComment); 65 + sendEmail($aUsers[$j]->getEmail(), $aUsers[$j]->getName(), $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument);
66 } else { 66 } else {
67 $default->log->error("email validation failed for " . $aUsers[$j]->getEmail()); 67 $default->log->error("email validation failed for " . $aUsers[$j]->getEmail());
68 } 68 }
@@ -79,7 +79,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = &quot;&quot;) { @@ -79,7 +79,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = &quot;&quot;) {
79 /** 79 /**
80 * Sends emails to the selected users 80 * Sends emails to the selected users
81 */ 81 */
82 -function sendUserEmails($aUserIDs, $oDocument, $sComment = "") { 82 +function sendUserEmails($aUserIDs, $oDocument, $sComment = "", $bAttachDocument) {
83 global $default; 83 global $default;
84 84
85 // loop through users 85 // loop through users
@@ -91,7 +91,7 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = &quot;&quot;) { @@ -91,7 +91,7 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = &quot;&quot;) {
91 if (strlen($oDestUser->getEmail())>0 && $oDestUser->getEmailNotification()) { 91 if (strlen($oDestUser->getEmail())>0 && $oDestUser->getEmailNotification()) {
92 //if the to address is valid, send the mail 92 //if the to address is valid, send the mail
93 if (validateEmailAddress($oDestUser->getEmail())) { 93 if (validateEmailAddress($oDestUser->getEmail())) {
94 - sendEmail($oDestUser->getEmail(), $oDestUser->getName(), $oDocument->getID(), $oDocument->getName(), $sComment); 94 + sendEmail($oDestUser->getEmail(), $oDestUser->getName(), $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument);
95 } 95 }
96 } else { 96 } else {
97 $default->log->info("either " . $oDestUser->getUserName() . " has no email address, or notification is not enabled"); 97 $default->log->info("either " . $oDestUser->getUserName() . " has no email address, or notification is not enabled");
@@ -105,17 +105,64 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = &quot;&quot;) { @@ -105,17 +105,64 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = &quot;&quot;) {
105 /** 105 /**
106 * Constructs the email message text and sends the message 106 * Constructs the email message text and sends the message
107 */ 107 */
108 -function sendEmail($sDestEmailAddress, $sDestUserName, $fDocumentID, $sDocumentName, $sComment) { 108 +function sendEmail($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, $bAttachDocument = false) {
  109 + if ($bAttachDocument !== true) {
  110 + return sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment);
  111 + } else {
  112 + return sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment);
  113 + }
  114 +}
  115 +
  116 +function sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment) {
  117 + global $default;
  118 + global $emailerrors;
  119 + $oSendingUser = User::get($_SESSION["userID"]);
  120 +
  121 + $sMessage = 'Your colleague, ' . $oSendingUser->getName() . ', wishes you to view the attached document entitled "' . $sDocumentName . '".';
  122 + $sMessage .= "\n\n";
  123 + if (strlen($sComment) > 0) {
  124 + $sMessage .= "<br><br>Comments:<br>$sComment";
  125 + }
  126 + $sTitle = "Document: " . $sDocumentName . " from " . $oSendingUser->getName();
  127 + $oEmail = new Email();
  128 + $oDocument = Document::get($iDocumentID);
  129 + $sDocumentPath = $oDocument->getPath();
  130 + $sDocumentFileName = $oDocument->getFileName();
  131 + $res = $oEmail->sendAttachment($sDestEmailAddress, $sTitle, $sMessage, $sDocumentPath, $sDocumentFileName);
  132 + if (PEAR::isError($res)) {
  133 + $default->log->error($res->getMessage());
  134 + $emailerrors[] = $res->getMessage();
  135 + return $res;
  136 + } else if ($res === false) {
  137 + $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress");
  138 + $emailerrors[] = "Error sending email ($sTitle) to $sDestEmailAddress";
  139 + return PEAR::raiseError("Error sending email ($sTitle) to $sDestEmailAddress");
  140 + } else {
  141 + $default->log->info("Send email ($sTitle) to $sDestEmailAddress");
  142 + }
  143 +
  144 + // emailed link transaction
  145 + $oDocumentTransaction = & new DocumentTransaction($iDocumentID, "Document link emailed to $sDestEmailAddress", EMAIL_ATTACH);
  146 + if ($oDocumentTransaction->create()) {
  147 + $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID");
  148 + } else {
  149 + $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$iDocumentID");
  150 + }
  151 +}
  152 +
  153 +function sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment) {
109 global $default; 154 global $default;
110 global $emailerrors; 155 global $emailerrors;
111 $oSendingUser = User::get($_SESSION["userID"]); 156 $oSendingUser = User::get($_SESSION["userID"]);
112 157
113 $sMessage = "<font face=\"arial\" size=\"2\">"; 158 $sMessage = "<font face=\"arial\" size=\"2\">";
114 - $sMessage .= $sDestUserName . ",<br><br>"; 159 + if ($sDestUserName) {
  160 + $sMessage .= $sDestUserName . ",<br><br>";
  161 + }
115 $sMessage .= "Your colleague, " . $oSendingUser->getName() . ", wishes you to view the document entitled '" . $sDocumentName . "'.\n "; 162 $sMessage .= "Your colleague, " . $oSendingUser->getName() . ", wishes you to view the document entitled '" . $sDocumentName . "'.\n ";
116 $sMessage .= "Click on the hyperlink below to view it."; 163 $sMessage .= "Click on the hyperlink below to view it.";
117 // add the link to the document to the mail 164 // add the link to the document to the mail
118 - $sMessage .= "<br>" . generateControllerLink("viewDocument", "fDocumentID=$fDocumentID", $sDocumentName); 165 + $sMessage .= "<br>" . generateControllerLink("viewDocument", "fDocumentID=$iDocumentID", $sDocumentName);
119 // add optional comment 166 // add optional comment
120 if (strlen($sComment) > 0) { 167 if (strlen($sComment) > 0) {
121 $sMessage .= "<br><br>Comments:<br>$sComment"; 168 $sMessage .= "<br><br>Comments:<br>$sComment";
@@ -131,18 +178,18 @@ function sendEmail($sDestEmailAddress, $sDestUserName, $fDocumentID, $sDocumentN @@ -131,18 +178,18 @@ function sendEmail($sDestEmailAddress, $sDestUserName, $fDocumentID, $sDocumentN
131 return $res; 178 return $res;
132 } else if ($res === false) { 179 } else if ($res === false) {
133 $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress"); 180 $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress");
134 - $emailerrors[] = "Error sending email ($sTitle) to $sDestEmailAddress"); 181 + $emailerrors[] = "Error sending email ($sTitle) to $sDestEmailAddress";
135 return PEAR::raiseError("Error sending email ($sTitle) to $sDestEmailAddress"); 182 return PEAR::raiseError("Error sending email ($sTitle) to $sDestEmailAddress");
136 } else { 183 } else {
137 $default->log->info("Send email ($sTitle) to $sDestEmailAddress"); 184 $default->log->info("Send email ($sTitle) to $sDestEmailAddress");
138 } 185 }
139 186
140 // emailed link transaction 187 // emailed link transaction
141 - $oDocumentTransaction = & new DocumentTransaction($fDocumentID, "Document link emailed to $sDestEmailAddress", EMAIL_LINK); 188 + $oDocumentTransaction = & new DocumentTransaction($iDocumentID, "Document link emailed to $sDestEmailAddress", EMAIL_LINK);
142 if ($oDocumentTransaction->create()) { 189 if ($oDocumentTransaction->create()) {
143 - $default->log->debug("emailBL.php created email link document transaction for document ID=$fDocumentID"); 190 + $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID");
144 } else { 191 } else {
145 - $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$fDocumentID"); 192 + $default->log->error("emailBL.php couldn't create email link document transaction for document ID=$iDocumentID");
146 } 193 }
147 } 194 }
148 195
@@ -164,9 +211,9 @@ if (checkSession()) { @@ -164,9 +211,9 @@ if (checkSession()) {
164 //if we're going to send a mail, first make there is someone to send it to 211 //if we're going to send a mail, first make there is someone to send it to
165 if ((count($aGroupIDs) > 1) || (count($aUserIDs) > 1)) { 212 if ((count($aGroupIDs) > 1) || (count($aUserIDs) > 1)) {
166 // send group emails 213 // send group emails
167 - sendGroupEmails($aGroupIDs, $oDocument, $fComment); 214 + sendGroupEmails($aGroupIDs, $oDocument, $fComment, (boolean)$fAttachDocument);
168 // send user emails 215 // send user emails
169 - sendUserEmails($aUserIDs, $oDocument, $fComment); 216 + sendUserEmails($aUserIDs, $oDocument, $fComment, (boolean)$fAttachDocument);
170 217
171 if (count($emailerrors)) { 218 if (count($emailerrors)) {
172 $_SESSION['errorMessage'] = join("<br />\n", $emailerrors); 219 $_SESSION['errorMessage'] = join("<br />\n", $emailerrors);
presentation/lookAndFeel/knowledgeTree/documentmanagement/emailUI.inc
@@ -91,6 +91,7 @@ function getDocumentEmailPage($oDocument) { @@ -91,6 +91,7 @@ function getDocumentEmailPage($oDocument) {
91 $sToRender .= renderUserPicker(); 91 $sToRender .= renderUserPicker();
92 $sToRender .= "</table></td></tr>"; 92 $sToRender .= "</table></td></tr>";
93 $sToRender .= "<tr><td><table>\n"; 93 $sToRender .= "<tr><td><table>\n";
  94 + $sToRender .= "<tr><td valign=\"top\">" . _("Attach Document") . "</td><td><input type=\"checkbox\" name=\"fAttachDocument\" /></td></tr>";//</tr>\n";
94 $sToRender .= "<tr><td valign=\"top\">" . _("Comment") . "</td><td><textarea rows=\"5\" cols=\"30\" name=\"fComment\"></textarea></td>";//</tr>\n"; 95 $sToRender .= "<tr><td valign=\"top\">" . _("Comment") . "</td><td><textarea rows=\"5\" cols=\"30\" name=\"fComment\"></textarea></td>";//</tr>\n";
95 $sToRender .= "<td valign=\"bottom\"><table><tr><td><input type=\"image\" src=\"" . KTHtml::getEmailButton() . "\" border=\"0\" /></td><td><a href=\"$default->rootUrl/control.php?action=viewDocument&fDocumentID=" . $oDocument->getID() . "\"><img src=\"" . KTHtml::getCancelButton() . "\" border=\"0\"/></a></td></tr></table></td></tr>\n"; 96 $sToRender .= "<td valign=\"bottom\"><table><tr><td><input type=\"image\" src=\"" . KTHtml::getEmailButton() . "\" border=\"0\" /></td><td><a href=\"$default->rootUrl/control.php?action=viewDocument&fDocumentID=" . $oDocument->getID() . "\"><img src=\"" . KTHtml::getCancelButton() . "\" border=\"0\"/></a></td></tr></table></td></tr>\n";
96 $sToRender .= "</table></td></tr>\n"; 97 $sToRender .= "</table></td></tr>\n";
sql/mysql/upgrade/2.0.6/email_attachment_transaction_type.sql 0 → 100644
  1 +INSERT INTO document_transaction_types_lookup VALUES (15, 'Email Attachment');