Skip to content

Commit

Permalink
Improved: Ensure unique email when sendEmail OFBIZ-13170 (#850)
Browse files Browse the repository at this point in the history
When you run the service sendCommEventAsEmail if commEvent to send contains multiple party with the same email (through different contactMech), email will be sent to many times to the same box (depending on the smtp configuration).

To avoid this case, we will ensure that an email is unique for the sender list.
  • Loading branch information
nmalin authored Nov 13, 2024
1 parent d644fe3 commit 22942db
Showing 1 changed file with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,45 +165,35 @@ public static Map<String, Object> sendCommEventAsEmail(DispatchContext ctx, Map<
return ServiceUtil.returnError(errMsg + " " + communicationEventId);
}

// add other parties from roles
String sendCc = null;
String sendBcc = null;
// add other parties from roles, collect all email on map to parse it after
List<String> alreadyLoaded = UtilMisc.toList(sendTo);
List<String> availableRoleTypeIds = UtilMisc.toList("ADDRESSEE", "CC", "BCC");
Map<String, Object> emailsCollector = UtilMisc.toMap("ADDRESSEE", UtilMisc.toList(sendTo));
List<GenericValue> commRoles = communicationEvent.getRelated("CommunicationEventRole", null, null, false);
if (UtilValidate.isNotEmpty(commRoles)) {
for (GenericValue commRole : commRoles) { // 'from' and 'to' already defined on communication event
if (commRole.getString("partyId").equals(communicationEvent.getString("partyIdFrom"))
|| commRole.getString("partyId").equals(communicationEvent.getString("partyIdTo"))) {
continue;
}
GenericValue contactMech = commRole.getRelatedOne("ContactMech", false);
if (contactMech != null && UtilValidate.isNotEmpty(contactMech.getString("infoString"))) {
if ("ADDRESSEE".equals(commRole.getString("roleTypeId"))) {
sendTo += "," + contactMech.getString("infoString");
} else if ("CC".equals(commRole.getString("roleTypeId"))) {
if (sendCc != null) {
sendCc += "," + contactMech.getString("infoString");
} else {
sendCc = contactMech.getString("infoString");
}
} else if ("BCC".equals(commRole.getString("roleTypeId"))) {
if (sendBcc != null) {
sendBcc += "," + contactMech.getString("infoString");
} else {
sendBcc = contactMech.getString("infoString");
}
String infoString = contactMech.getString("infoString");
String roleTypeId = commRole.getString("roleTypeId");
if (alreadyLoaded.contains(infoString)
&& !availableRoleTypeIds.contains(roleTypeId)) {
continue;
}
alreadyLoaded.add(infoString);
UtilMisc.addToListInMap(infoString, emailsCollector, roleTypeId);
}
}
}
sendMailParams.put("sendTo", String.join(",", UtilMisc.getListFromMap(emailsCollector, "ADDRESSEE")));
sendMailParams.put("sendCc", emailsCollector.containsKey("CC")
? String.join(",", UtilMisc.getListFromMap(emailsCollector, "CC"))
: null);
sendMailParams.put("sendBcc", emailsCollector.containsKey("BCC")
? String.join(",", UtilMisc.getListFromMap(emailsCollector, "BCC"))
: null);

sendMailParams.put("communicationEventId", communicationEventId);
sendMailParams.put("sendTo", sendTo);
if (sendCc != null) {
sendMailParams.put("sendCc", sendCc);
}
if (sendBcc != null) {
sendMailParams.put("sendBcc", sendBcc);
}
sendMailParams.put("partyId", communicationEvent.getString("partyIdTo")); // who it's going to

// send it - using a new transaction
Expand Down

0 comments on commit 22942db

Please sign in to comment.