Commit 2ee28c53b7d04aa9c21475bce08e44b047dfea91

Authored by megan_w
1 parent d64c932a

KTS-2372

"Emails sent multiple times when emailing groups with sub-groups (SUP-197)"
If a user is in more than one of the groups selected then he/she will get the email more than once.
The email is now sent once to multiple users and ignores duplicate email addresses.

Committed by: Megan Watson
Reviewed by: Conrad Vermeulen



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7229 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 49 additions and 28 deletions
plugins/ktstandard/KTEmail.php
... ... @@ -40,7 +40,7 @@ require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
40 40 /**
41 41 * Sends emails to the selected groups
42 42 */
43   -function sendGroupEmails($aGroupIDs, $oDocument, $sComment = '', $bAttachDocument, &$aEmailErrors) {
  43 +function sendGroupEmails($aGroupIDs, &$aUserEmails, &$aEmailErrors) {
44 44 global $default;
45 45  
46 46 // loop through groups
... ... @@ -62,20 +62,22 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = '', $bAttachDocumen
62 62 }
63 63  
64 64 // FIXME: this should send one email with multiple To: users
  65 + // The FIX (26-09-2007): create an array of users to email
65 66 for ($j=0; $j<count($aUsers); $j++) {
66 67 $default->log->info('sendingEmail to group-member ' . $aUsers[$j]->getName() . ' with email ' . $aUsers[$j]->getEmail());
67 68 // the user has an email address and has email notification enabled
68 69 if (strlen($aUsers[$j]->getEmail())>0 && $aUsers[$j]->getEmailNotification()) {
69 70 //if the to address is valid, send the mail
70 71 if (validateEmailAddress($aUsers[$j]->getEmail())) {
71   - sendEmail($aUsers[$j]->getEmail(), $aUsers[$j]->getName(), $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument, $aEmailErrors);
  72 + // use the email address as the index to ensure the user is only sent 1 email
  73 + $aUserEmails[$aUsers[$j]->getEmail()] = $aUsers[$j]->getName();
72 74 } else {
73 75 $default->log->error('email validation failed for ' . $aUsers[$j]->getEmail());
74 76 }
75 77 } else {
76 78 $default->log->info('either ' . $aUsers[$j]->getUserName() . ' has no email address, or notification is not enabled');
77 79 }
78   - }
  80 + }
79 81 } else {
80 82 $default->log->info('filtered group id=' . $aGroupIDs[$i]);
81 83 }
... ... @@ -85,7 +87,7 @@ function sendGroupEmails($aGroupIDs, $oDocument, $sComment = &#39;&#39;, $bAttachDocumen
85 87 /**
86 88 * Sends emails to the selected users
87 89 */
88   -function sendUserEmails($aUserIDs, $oDocument, $sComment = '', $bAttachDocument, &$aEmailErrors) {
  90 +function sendUserEmails($aUserIDs, &$aUserEmails, &$aEmailErrors) {
89 91 global $default;
90 92  
91 93 // loop through users
... ... @@ -97,7 +99,8 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = &#39;&#39;, $bAttachDocument,
97 99 if (strlen($oDestUser->getEmail())>0 && $oDestUser->getEmailNotification()) {
98 100 //if the to address is valid, send the mail
99 101 if (validateEmailAddress($oDestUser->getEmail())) {
100   - sendEmail($oDestUser->getEmail(), $oDestUser->getName(), $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument, $aEmailErrors);
  102 + // use the email address as the index to ensure the user is only sent 1 email
  103 + $aUserEmails[$oDestUser->getEmail()] = $oDestUser->getName();
101 104 }
102 105 } else {
103 106 $default->log->info('either ' . $oDestUser->getUserName() . ' has no email address, or notification is not enabled');
... ... @@ -111,14 +114,15 @@ function sendUserEmails($aUserIDs, $oDocument, $sComment = &#39;&#39;, $bAttachDocument,
111 114 /**
112 115 * Sends emails to the manually entered email addresses
113 116 */
114   -function sendManualEmails($aEmailAddresses, $oDocument, $sComment = '', $bAttachDocument, &$aEmailErrors) {
  117 +function sendManualEmails($aEmailAddresses, &$aUserEmails, &$aEmailErrors) {
115 118 global $default;
116 119  
117 120 // loop through users
118 121 foreach ($aEmailAddresses as $sEmailAddress) {
119 122 $default->log->info('sendingEmail to address ' . $sEmailAddress);
120 123 if (validateEmailAddress($sEmailAddress)) {
121   - sendEmail($sEmailAddress, $sEmailAddress, $oDocument->getID(), $oDocument->getName(), $sComment, $bAttachDocument, $aEmailErrors);
  124 + // use the email address as the index to ensure the user is only sent 1 email
  125 + $aUserEmails[$sEmailAddress] = $sEmailAddress;
122 126 }
123 127 }
124 128 }
... ... @@ -126,16 +130,18 @@ function sendManualEmails($aEmailAddresses, $oDocument, $sComment = &#39;&#39;, $bAttach
126 130 /**
127 131 * Constructs the email message text and sends the message
128 132 */
129   -function sendEmail($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, $bAttachDocument = false, &$aEmailErrors) {
  133 +function sendEmail($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, $bAttachDocument = false, &$aEmailErrors) {
130 134 if ($bAttachDocument !== true) {
131   - return sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, $aEmailErrors);
  135 + return sendEmailHyperlink($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, $aEmailErrors);
132 136 } else {
133   - return sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, $aEmailErrors);
  137 + return sendEmailDocument($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, $aEmailErrors);
134 138 }
135 139 }
136 140  
137   -function sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors) {
  141 +function sendEmailDocument($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors) {
138 142 global $default;
  143 + // Get the email list as a string for the logs
  144 + $sDestEmails = implode(',', $aDestEmailAddress);
139 145 $oSendingUser = User::get($_SESSION['userID']);
140 146  
141 147 $sMessage .= sprintf(_kt("Your colleague, %s, wishes you to view the attached document entitled '%s'."), $oSendingUser->getName(), $sDocumentName);
... ... @@ -161,7 +167,7 @@ function sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sD
161 167 $sDocumentPath = $oStorage->temporaryFile($oDocument);
162 168  
163 169 $sDocumentFileName = $oDocument->getFileName();
164   - $res = $oEmail->sendAttachment($sDestEmailAddress, $sTitle, $sMessage, $sDocumentPath, $sDocumentFileName);
  170 + $res = $oEmail->sendAttachment($aDestEmailAddress, $sTitle, $sMessage, $sDocumentPath, $sDocumentFileName);
165 171  
166 172 // Tell the storage we don't need the temporary file anymore.
167 173 $oStorage->freeTemporaryFile($sDocumentPath);
... ... @@ -171,15 +177,15 @@ function sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sD
171 177 $aEmailErrors[] = $res->getMessage();
172 178 return $res;
173 179 } else if ($res === false) {
174   - $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress");
175   - $aEmailErrors[] = "Error sending email ($sTitle) to $sDestEmailAddress";
176   - return PEAR::raiseError(sprintf(_kt("Error sending email (%s) to %s"), $sTitle, $sDestEmailAddress));
  180 + $default->log->error("Error sending email ($sTitle) to $sDestEmails");
  181 + $aEmailErrors[] = "Error sending email ($sTitle) to $sDestEmails";
  182 + return PEAR::raiseError(sprintf(_kt("Error sending email (%s) to %s"), $sTitle, $sDestEmails));
177 183 } else {
178   - $default->log->info("Send email ($sTitle) to $sDestEmailAddress");
  184 + $default->log->info("Send email ($sTitle) to $sDestEmails");
179 185 }
180 186  
181 187 // emailed link transaction
182   - $oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Document copy emailed to %s"), $sDestEmailAddress), 'ktcore.transactions.email_attachement');
  188 + $oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Document copy emailed to %s"), $sDestEmails), 'ktcore.transactions.email_attachement');
183 189 if ($oDocumentTransaction->create()) {
184 190 $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID");
185 191 } else {
... ... @@ -187,14 +193,18 @@ function sendEmailDocument($sDestEmailAddress, $sDestUserName, $iDocumentID, $sD
187 193 }
188 194 }
189 195  
190   -function sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors) {
  196 +function sendEmailHyperlink($aDestEmailAddress, $iDocumentID, $sDocumentName, $sComment, &$aEmailErrors) {
191 197 global $default;
  198 + // Get the email list as a string for the logs
  199 + $sDestEmails = implode(',', $aDestEmailAddress);
192 200 $oSendingUser = User::get($_SESSION['userID']);
193 201  
194 202 $sMessage = '<font face="arial" size="2">';
  203 + /*
195 204 if ($sDestUserName) {
196 205 $sMessage .= $sDestUserName . ',<br><br>';
197 206 }
  207 + */
198 208 $sMessage .= sprintf(_kt("Your colleague, %s, wishes you to view the document entitled '%s'."), $oSendingUser->getName(), $sDocumentName);
199 209 $sMessage .= " \n";
200 210 $sMessage .= _kt('Click on the hyperlink below to view it.');
... ... @@ -217,24 +227,24 @@ function sendEmailHyperlink($sDestEmailAddress, $sDestUserName, $iDocumentID, $s
217 227 }
218 228 $oEmail = new Email($sEmail, $sEmailFrom);
219 229  
220   - $res = $oEmail->send($sDestEmailAddress, $sTitle, $sMessage);
  230 + $res = $oEmail->send($aDestEmailAddress, $sTitle, $sMessage);
221 231 if (PEAR::isError($res)) {
222 232 $default->log->error($res->getMessage());
223 233 $aEmailErrors[] = $res->getMessage();
224 234 return $res;
225 235 } else if ($res === false) {
226   - $default->log->error("Error sending email ($sTitle) to $sDestEmailAddress");
227   - $aEmailErrors[] = "Error sending email ($sTitle) to $sDestEmailAddress";
228   - return PEAR::raiseError(sprintf(_kt("Error sending email (%s) to %s"), $sTitle, $sDestEmailAddress));
  236 + $default->log->error("Error sending email ($sTitle) to $sDestEmails");
  237 + $aEmailErrors[] = "Error sending email ($sTitle) to $sDestEmails";
  238 + return PEAR::raiseError(sprintf(_kt("Error sending email (%s) to %s"), $sTitle, $sDestEmails));
229 239 } else {
230   - $default->log->info("Send email ($sTitle) to $sDestEmailAddress");
  240 + $default->log->info("Send email ($sTitle) to $sDestEmails");
231 241 }
232 242  
233 243 // emailed link transaction
234 244 // need a document to do this.
235 245 $oDocument =& Document::get($iDocumentID);
236 246  
237   - $oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Document link emailed to %s"), $sDestEmailAddress), 'ktcore.transactions.email_link');
  247 + $oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Document link emailed to %s"), $sDestEmails), 'ktcore.transactions.email_link');
238 248  
239 249 if ($oDocumentTransaction->create()) {
240 250 $default->log->debug("emailBL.php created email link document transaction for document ID=$iDocumentID");
... ... @@ -435,14 +445,25 @@ class KTDocumentEmailAction extends KTDocumentAction {
435 445 }
436 446  
437 447 $aEmailErrors = array();
  448 + $aUserEmails = array();
438 449  
439 450 // send group emails
440   - sendGroupEmails($aGroupIDs, $this->oDocument, $fComment, (boolean)$fAttachDocument, $aEmailErrors);
  451 + sendGroupEmails($aGroupIDs, $aUserEmails, $aEmailErrors);
441 452 // send user emails
442   - sendUserEmails($aUserIDs, $this->oDocument, $fComment, (boolean)$fAttachDocument, $aEmailErrors);
  453 + sendUserEmails($aUserIDs, $aUserEmails, $aEmailErrors);
443 454 // send manual email addresses
444   - sendManualEmails($aEmailAddresses, $this->oDocument, $fComment, (boolean)$fAttachDocument, $aEmailErrors);
445   -
  455 + sendManualEmails($aEmailAddresses, $aUserEmails, $aEmailErrors);
  456 +
  457 + // get list of email addresses and send
  458 + if(!empty($aUserEmails)){
  459 + // email addresses are in the keys -> extract the keys
  460 + $aListEmails = array_keys($aUserEmails);
  461 +
  462 + $iDocumentID = $this->oDocument->getID();
  463 + $sDocumentName = $this->oDocument->getName();
  464 + sendEmail($aListEmails, $iDocumentID, $sDocumentName, $fComment, (boolean)$fAttachDocument, &$aEmailErrors);
  465 + }
  466 +
446 467 if (count($aEmailErrors)) {
447 468 $_SESSION['KTErrorMessage'][] = join('<br />\n', $aEmailErrors);
448 469 }
... ...