View source | View content page | Page history | Printable version   
Main Page
Upload file
What links here
Recent changes

PDF Books
Show collection (0 pages)
Collections help


Projects:Outbound Email/Add cc and bcc


Outbound Email - Adding CC and BCC


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.


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


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()
<!-- 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 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(;"><span class="LabelText" name="salesrepName" id="salesrepName" value="" style="padding-left: 10px"></span>
<!-- Start of new code -->
<!-- New code: Spacer and "CC" field-->
<tr style="height: 8px">
<tr id="cc">
<td style="width: 1%; text-align: right;">
<span style="white-space: nowrap; padding-right: 10px;" class="LabelText">Cc</span>
<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(;"><span class="LabelText" name="ccName" id="ccName" value="" style="padding-left: 10px"></span>
<!-- New code: Spacer and "BCC" field-->
<tr style="height: 8px">
<tr id="bcc">
<td style="width: 1%; text-align: right;">
<span style="white-space: nowrap; padding-right: 10px;" class="LabelText">Bcc</span>
<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(;"><span class="LabelText" name="bccName" id="bccName" value="" style="padding-left: 10px"></span>
<!-- End of new code -->
<!-- Existing code: spacer and "Subject" field-->
<tr style="height: 8px">
<tr id="emailField03">
<td style="width: 1%; text-align: right;">
<span style="white-space: nowrap; padding-right: 10px;" class="LabelText">Subject</span>
<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 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=""/>


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.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));
      if (ccEmail != null && ccEmail.length() > 0)
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccEmail));
      if (userEmail != null && userEmail.length() > 0)
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(userEmail));
      // 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();
      // Create the attachment part
      messageBodyPart = new MimeBodyPart();
      final DataSource source = new FileDataSource(attachmentFileLocation);
      messageBodyPart.setDataHandler(new DataHandler(source));
          .lastIndexOf("/") + 1));
      // 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();
      // Send the email
      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,
      } catch (final NoConnectionAvailableException exception) {
        throw new ServletException(exception);
      } catch (final SQLException exception) {
        try {
        } catch (final Exception ignored) {
        throw new ServletException(exception);
    } catch (final PocException exception) {
      throw new ServletException(exception);
    } catch (final AddressException exception) {
      throw new ServletException(exception);
    } catch (final MessagingException exception) {
      throw new ServletException("problems with the SMTP server configuration: "
          + exception.getMessage(), exception);

Retrieved from ""

This page has been accessed 1,346 times. This page was last modified on 8 June 2012, at 05:29. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.