From bbd6556f454d598dcc527033e48b0fd0c4a3c3ee Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Mon, 28 Jul 2014 14:52:09 +0200 Subject: [PATCH] IDEMPIERE-2104 restrict sending EMails to one single to address --- .../src/org/compiere/model/MSysConfig.java | 5 +- .../src/org/compiere/util/EMail.java | 54 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MSysConfig.java b/org.adempiere.base/src/org/compiere/model/MSysConfig.java index 678aa9fc8d..8cdda434bf 100644 --- a/org.adempiere.base/src/org/compiere/model/MSysConfig.java +++ b/org.adempiere.base/src/org/compiere/model/MSysConfig.java @@ -42,9 +42,9 @@ public class MSysConfig extends X_AD_SysConfig /** * */ - private static final long serialVersionUID = -4635791167798916808L; + private static final long serialVersionUID = 8682693142401650434L; - public static final String ADDRESS_SAVE_REQUEST_RESPONSE_LOG = "ADDRESS_SAVE_REQUEST_RESPONSE_LOG"; + public static final String ADDRESS_SAVE_REQUEST_RESPONSE_LOG = "ADDRESS_SAVE_REQUEST_RESPONSE_LOG"; public static final String ADDRESS_VALIDATION = "ADDRESS_VALIDATION"; public static final String ALERT_SEND_ATTACHMENT_AS_XLS = "ALERT_SEND_ATTACHMENT_AS_XLS"; public static final String ALLOW_APPLY_PAYMENT_TO_CREDITMEMO = "ALLOW_APPLY_PAYMENT_TO_CREDITMEMO"; @@ -79,6 +79,7 @@ public class MSysConfig extends X_AD_SysConfig public static final String LOCATION_MAX_CITY_ROWS = "LOCATION_MAX_CITY_ROWS"; public static final String LOGIN_HELP_URL = "LOGIN_HELP_URL"; public static final String LOGIN_SHOW_RESETPASSWORD = "LOGIN_SHOW_RESETPASSWORD"; + public static final String MAIL_DONT_SEND_TO_ADDRESS = "MAIL_DONT_SEND_TO_ADDRESS"; public static final String MAIL_SEND_BCC_TO_ADDRESS = "MAIL_SEND_BCC_TO_ADDRESS"; public static final String MAIL_SEND_BCC_TO_FROM = "MAIL_SEND_BCC_TO_FROM"; public static final String MAIL_SEND_CREDENTIALS = "MAIL_SEND_CREDENTIALS"; diff --git a/org.adempiere.base/src/org/compiere/util/EMail.java b/org.adempiere.base/src/org/compiere/util/EMail.java index 0f4e61ffda..664ba1e36d 100644 --- a/org.adempiere.base/src/org/compiere/util/EMail.java +++ b/org.adempiere.base/src/org/compiere/util/EMail.java @@ -155,7 +155,7 @@ public final class EMail implements Serializable setSmtpHost(smtpHost); setFrom(from); String bccAddressForAllMails = MSysConfig.getValue(MSysConfig.MAIL_SEND_BCC_TO_ADDRESS, Env.getAD_Client_ID(Env.getCtx())); - if (bccAddressForAllMails != null && bccAddressForAllMails.length() > 0) + if (! Util.isEmpty(bccAddressForAllMails, true)) addBcc(bccAddressForAllMails); addTo(to); m_ctx = ctx; @@ -291,17 +291,37 @@ public final class EMail implements Serializable m_msg = new SMTPMessage(session); // Addresses m_msg.setFrom(m_from); - InternetAddress[] rec = getTos(); - if (rec.length == 1) - m_msg.setRecipient (Message.RecipientType.TO, rec[0]); - else - m_msg.setRecipients (Message.RecipientType.TO, rec); - rec = getCcs(); - if (rec != null && rec.length > 0) - m_msg.setRecipients (Message.RecipientType.CC, rec); - rec = getBccs(); - if (rec != null && rec.length > 0) - m_msg.setRecipients (Message.RecipientType.BCC, rec); + + // IDEMPIERE-2104 - intended for test or dev systems to not send undesired emails + boolean isDontSendToAddress = MSysConfig.getBooleanValue(MSysConfig.MAIL_DONT_SEND_TO_ADDRESS, false, Env.getAD_Client_ID(Env.getCtx())); + + if (! isDontSendToAddress) { + InternetAddress[] rec = getTos(); + if (rec.length == 1) + m_msg.setRecipient (Message.RecipientType.TO, rec[0]); + else + m_msg.setRecipients (Message.RecipientType.TO, rec); + rec = getCcs(); + if (rec != null && rec.length > 0) + m_msg.setRecipients (Message.RecipientType.CC, rec); + rec = getBccs(); + if (rec != null && rec.length > 0) + m_msg.setRecipients (Message.RecipientType.BCC, rec); + } else { + String bccAddressForAllMails = MSysConfig.getValue(MSysConfig.MAIL_SEND_BCC_TO_ADDRESS, Env.getAD_Client_ID(Env.getCtx())); + if (! Util.isEmpty(bccAddressForAllMails, true)) { + m_msg.setRecipient (Message.RecipientType.TO, new InternetAddress(bccAddressForAllMails, true)); + } + InternetAddress[] rec = getTos(); + if (rec != null && rec.length > 0) + m_msg.setHeader("OriginalTo", getCommaSeparatedString(rec)); + rec = getCcs(); + if (rec != null && rec.length > 0) + m_msg.setHeader("OriginalCC", getCommaSeparatedString(rec)); + rec = getBccs(); + if (rec != null && rec.length > 0) + m_msg.setHeader("OriginalBCC", getCommaSeparatedString(rec)); + } if (m_replyTo != null) m_msg.setReplyTo(new Address[] {m_replyTo}); // @@ -436,6 +456,16 @@ public final class EMail implements Serializable return m_sentMsg; } // send + private String getCommaSeparatedString(InternetAddress[] recs) { + StringBuilder retValue = new StringBuilder(); + for (InternetAddress rec : recs) { + if (retValue.length() > 0) + retValue.append(","); + retValue.append(rec.getAddress()); + } + return retValue.toString(); + } + /** * Get Send Result Msg * @return msg