Projects:Outbound Email/Add cc and bcc
Contents |
Outbound Email - Adding CC and BCC
WARNING
The changes listed below are made to the core, and are not part of any module. This means that they are not upgrade safe, and that any core upgrade will overwrite them. Proceed with this in mind.
Overview
The purpose of this modification is to add CC and BCC to emails sent using Openbravo. To achieve this, the "Email Document" window has been changed to include fields for CC and BCC input, and the BCC field is automatically filled with the sales representative's address. This was implemented using OpenbravoERP 2.50MP12, and all the code listed below is based on the source of this version.
Files to be changed
src/org/openbravo/erpCommon/utility/reporting/printing/EmailOptions.html
This file specifies the layout of the actual window. Two new fields were added, CC and BCC. As this makes the content use more space that before inside the browser's window, the resizeWindow() function was changed in order to accommodate that change in height.
function resizeWindow() { window.resizeTo(750,600); }
<!-- Existing code:"From" field--> <tr id="salesRep"> <td style="width: 1%; text-align: right;"> <span style="white-space: nowrap; padding-right: 10px;" class="LabelText">From</span> </td> <td id="salesrepEmailArea" colspan="2"> <input style="width: 55%;" type="text" dojoType="openbravo:ValidationTextBox" class="dojoValidateValid required TextBox_btn_TwoCells_width" maxlength="200" name="salesrepEmail" id="salesrepEmail" value="" required="true" onkeyup="validateTextBox(this.id);"><span class="LabelText" name="salesrepName" id="salesrepName" value="" style="padding-left: 10px"></span> </td> </tr> <!-- Start of new code --> <!-- New code: Spacer and "CC" field--> <tr style="height: 8px"> </tr> <tr id="cc"> <td style="width: 1%; text-align: right;"> <span style="white-space: nowrap; padding-right: 10px;" class="LabelText">Cc</span> </td> <td id="ccEmailArea" colspan="2"> <input style="width: 55%;" type="text" dojoType="openbravo:ValidationTextBox" class="dojoValidateValid TextBox_btn_TwoCells_width" maxlength="200" name="ccEmail" id="ccEmail" value="" required="true" onkeyup="validateTextBox(this.id);"><span class="LabelText" name="ccName" id="ccName" value="" style="padding-left: 10px"></span> </td> </tr> <!-- New code: Spacer and "BCC" field--> <tr style="height: 8px"> </tr> <tr id="bcc"> <td style="width: 1%; text-align: right;"> <span style="white-space: nowrap; padding-right: 10px;" class="LabelText">Bcc</span> </td> <td id="bccEmailArea" colspan="2"> <input style="width: 55%;" type="text" dojoType="openbravo:ValidationTextBox" class="dojoValidateValid TextBox_btn_TwoCells_width" maxlength="200" name="bccEmail" id="bccEmail" value="" required="true" onkeyup="validateTextBox(this.id);"><span class="LabelText" name="bccName" id="bccName" value="" style="padding-left: 10px"></span> </td> </tr> <!-- End of new code --> <!-- Existing code: spacer and "Subject" field--> <tr style="height: 8px"> </tr> <tr id="emailField03"> <td style="width: 1%; text-align: right;"> <span style="white-space: nowrap; padding-right: 10px;" class="LabelText">Subject</span> </td> <td colspan="2"> <input style="width: 55%;" type="text" dojoType="openbravo:ValidationTextBox" class="dojoValidateValid required TextBox_btn_TwoCells_width" maxlength="200" name="emailSubject" id="emailSubject" value="" required="true" onkeyup="validateTextBox(this.id);"> </td> </tr>
src/org/openbravo/erpCommon/utility/reporting/printing/EmailOptions.xml
This file specifies the data that is made available on the "Email Document" window, and needs to be changed in order to add the two new fields, "ccEmail" and "bccEmail". The field "bccEmail" is kept with name "salesrepName" in order to be autocompleted with the sales rep's email address. This is surely not the best way to implement this features, but it minimizes the changes that need to be made to the code.
Just add this two lines undeneath the "salesrepEmail" parameter line:
<PARAMETER id="ccEmail" name="ccEmail" attribute="value" default=""/> <PARAMETER id="bccEmail" name="salesrepEmail" attribute="value" default=""/>
src/org/openbravo/erpCommon/utility/reporting/printing/PrintController.java
This Java class contains the sendDocumentEmail(...) function, which is responsible for reading your input and sending the email. The changes made here intend to read the the cc and bcc information from the html form, and adding them to the email sent.
void sendDocumentEmail(Report report, VariablesSecureApp vars, Vector<Object> object, PocData documentData, String senderAddess, HashMap<String, Boolean> checks) throws IOException, ServletException { final String documentId = report.getDocumentId(); final String attachmentFileLocation = report.getTargetLocation(); final String ourReference = report.getOurReference(); final String cusReference = report.getCusReference(); if (log4j.isDebugEnabled()) log4j.debug("our document ref: " + ourReference); if (log4j.isDebugEnabled()) log4j.debug("cus document ref: " + cusReference); // Also send it to the current user final PocData[] currentUserInfo = PocData.getContactDetailsForUser(this, vars.getUser()); final String userName = currentUserInfo[0].userName; final String userEmail = currentUserInfo[0].userEmail; if (log4j.isDebugEnabled()) log4j.debug("user name: " + userName); if (log4j.isDebugEnabled()) log4j.debug("user email: " + userEmail); final String contactName = documentData.contactName; String contactEmail = null; final String salesrepName = documentData.salesrepName; String salesrepEmail = null; boolean moreThanOneCustomer = checks.get("moreThanOneCustomer").booleanValue(); boolean moreThanOnesalesRep = checks.get("moreThanOnesalesRep").booleanValue(); if (moreThanOneCustomer) { contactEmail = documentData.contactEmail; } else { contactEmail = vars.getStringParameter("contactEmail"); } if (moreThanOnesalesRep) { salesrepEmail = documentData.contactEmail; } else { salesrepEmail = vars.getStringParameter("salesrepEmail"); } String ccEmail = vars.getStringParameter("ccEmail"); String bccEmail = vars.getStringParameter("bccEmail"); String emailSubject = vars.getStringParameter("emailSubject"); String emailBody = vars.getStringParameter("emailBody"); if (log4j.isDebugEnabled()) log4j.debug("sales rep name: " + salesrepName); if (log4j.isDebugEnabled()) log4j.debug("sales rep email: " + salesrepEmail); if (log4j.isDebugEnabled()) log4j.debug("recipient name: " + contactName); if (log4j.isDebugEnabled()) log4j.debug("recipient email: " + contactEmail); // TODO: Move this to the beginning of the print handling and do nothing // if these conditions fail!!!) if ((salesrepEmail == null || salesrepEmail.length() == 0)) { throw new ServletException(Utility.messageBD(this, "NoSalesRepEmail", vars.getLanguage())); } if ((contactEmail == null || contactEmail.length() == 0)) { throw new ServletException(Utility.messageBD(this, "NoCustomerEmail", vars.getLanguage())); } // Replace special tags emailSubject = emailSubject.replaceAll("@cus_ref@", cusReference); emailSubject = emailSubject.replaceAll("@our_ref@", ourReference); emailSubject = emailSubject.replaceAll("@cus_nam@", contactName); emailSubject = emailSubject.replaceAll("@sal_nam@", salesrepName); emailBody = emailBody.replaceAll("@cus_ref@", cusReference); emailBody = emailBody.replaceAll("@our_ref@", ourReference); emailBody = emailBody.replaceAll("@cus_nam@", contactName); emailBody = emailBody.replaceAll("@sal_nam@", salesrepName); try { final Session session = EmailManager .newMailSession(this, vars.getClient(), report.getOrgId()); final Message message = new MimeMessage(session); Address[] address = new InternetAddress[1]; address[0] = new InternetAddress(salesrepEmail); message.setReplyTo(address); message.setFrom(new InternetAddress(senderAddess)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(contactEmail)); if (bccEmail != null && bccEmail.length() > 0) message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bccEmail)); else bccEmail=""; if (ccEmail != null && ccEmail.length() > 0) message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccEmail)); else ccEmail=""; if (userEmail != null && userEmail.length() > 0) message.addRecipient(Message.RecipientType.BCC, new InternetAddress(userEmail)); message.setSubject(emailSubject); // Content consists of 2 parts, the message body and the attachment // We therefor use a multipart message final Multipart multipart = new MimeMultipart(); // Create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(emailBody); multipart.addBodyPart(messageBodyPart); // Create the attachment part messageBodyPart = new MimeBodyPart(); final DataSource source = new FileDataSource(attachmentFileLocation); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachmentFileLocation.substring(attachmentFileLocation .lastIndexOf("/") + 1)); multipart.addBodyPart(messageBodyPart); // Add aditional attached documents if (object != null) { final Vector<Object> vector = (Vector<Object>) object; for (int i = 0; i < vector.size(); i++) { final AttachContent content = (AttachContent) vector.get(i); final File file = prepareFile(content); messageBodyPart = new MimeBodyPart(); messageBodyPart.attachFile(file); multipart.addBodyPart(messageBodyPart); } } message.setContent(multipart); // Send the email Transport.send(message); final String clientId = vars.getClient(); final String organizationId = vars.getOrg(); final String userId = vars.getUser(); final String from = salesrepEmail; final String to = contactEmail; final String cc = ccEmail; String bcc = bccEmail; if (userEmail != null && userEmail.length() > 0) { if (bccEmail.length() > 0) bcc = bcc + "; "; bcc = bcc + userEmail; } final String subject = emailSubject; final String body = emailBody; final String dateOfEmail = Utility.formatDate(new Date(), "yyyyMMddHHmmss"); final String bPartnerId = report.getBPartnerId(); // Store the email in the database Connection conn = null; try { conn = this.getTransactionConnection(); // First store the email message final String newEmailId = SequenceIdData.getUUID(); if (log4j.isDebugEnabled()) log4j.debug("New email id: " + newEmailId); EmailData.insertEmail(conn, this, newEmailId, clientId, organizationId, userId, EmailType.OUTGOING.getStringValue(), from, to, cc, bcc, dateOfEmail, subject, body, bPartnerId); releaseCommitConnection(conn); } catch (final NoConnectionAvailableException exception) { log4j.error(exception); throw new ServletException(exception); } catch (final SQLException exception) { log4j.error(exception); try { releaseRollbackConnection(conn); } catch (final Exception ignored) { } throw new ServletException(exception); } } catch (final PocException exception) { log4j.error(exception); throw new ServletException(exception); } catch (final AddressException exception) { log4j.error(exception); throw new ServletException(exception); } catch (final MessagingException exception) { log4j.error(exception); throw new ServletException("problems with the SMTP server configuration: " + exception.getMessage(), exception); } }