public Map<EmailTemplateLocaleUsers, RenderedTemplate> getRenderedTemplates(
      String key, List<String> userReferences, Map<String, String> replacementValues) {

    List<Locale> foundLocales = new ArrayList<Locale>();
    Map<Locale, EmailTemplateLocaleUsers> mapStore =
        new HashMap<Locale, EmailTemplateLocaleUsers>();

    for (int i = 0; i < userReferences.size(); i++) {
      String userReference = userReferences.get(i);
      String userId = developerHelperService.getUserIdFromRef(userReference);
      Locale loc = getUserLocale(userId);
      // have we found this locale?
      if (!foundLocales.contains(loc)) {
        // create a new EmailTemplateLocalUser
        EmailTemplateLocaleUsers etlu = new EmailTemplateLocaleUsers();
        log.debug("adding users " + userReference + " to new object");
        etlu.setLocale(loc);
        etlu.addUser(userReference);
        mapStore.put(loc, etlu);
        foundLocales.add(loc);
      } else {
        EmailTemplateLocaleUsers etlu = mapStore.get(loc);
        log.debug("adding users " + userReference + " to existing object");
        etlu.addUser(userReference);
        mapStore.remove(loc);
        mapStore.put(loc, etlu);
      }
    }

    Map<EmailTemplateLocaleUsers, RenderedTemplate> ret =
        new HashMap<EmailTemplateLocaleUsers, RenderedTemplate>();

    // now for each locale we need a rendered template
    Set<Entry<Locale, EmailTemplateLocaleUsers>> es = mapStore.entrySet();
    Iterator<Entry<Locale, EmailTemplateLocaleUsers>> it = es.iterator();
    while (it.hasNext()) {
      Entry<Locale, EmailTemplateLocaleUsers> entry = it.next();
      Locale loc = entry.getKey();
      RenderedTemplate rt = this.getRenderedTemplate(key, loc, replacementValues);
      if (rt != null) {
        ret.put(entry.getValue(), rt);
      } else {
        log.error("No template found for key: " + key + " in locale: " + loc);
      }
    }
    return ret;
  }
  public void sendRenderedMessages(
      String key,
      List<String> userReferences,
      Map<String, String> replacementValues,
      String fromEmail,
      String fromName) {

    Map<EmailTemplateLocaleUsers, RenderedTemplate> tMap =
        this.getRenderedTemplates(key, userReferences, replacementValues);
    Set<Entry<EmailTemplateLocaleUsers, RenderedTemplate>> set = tMap.entrySet();
    Iterator<Entry<EmailTemplateLocaleUsers, RenderedTemplate>> it = set.iterator();

    while (it.hasNext()) {

      Entry<EmailTemplateLocaleUsers, RenderedTemplate> entry = it.next();
      RenderedTemplate rt = entry.getValue();
      EmailTemplateLocaleUsers etlu = entry.getKey();
      List<User> toAddress = getUsersEmail(etlu.getUserIds());
      log.info(
          "sending template "
              + key
              + " for locale "
              + etlu.getLocale().toString()
              + " to "
              + toAddress.size()
              + " users");
      StringBuilder message = new StringBuilder();
      message.append(MIME_ADVISORY);
      if (rt.getRenderedMessage() != null) {
        message.append(BOUNDARY_LINE);
        message.append("Content-Type: text/plain; charset=iso-8859-1\n");
        message.append(rt.getRenderedMessage());
      }
      if (rt.getRenderedHtmlMessage() != null) {
        // append the HMTL part
        message.append(BOUNDARY_LINE);
        message.append("Content-Type: text/html; charset=iso-8859-1\n");
        message.append(rt.getRenderedHtmlMessage());
      }

      message.append(TERMINATION_LINE);

      // we need to manually construct the headers
      List<String> headers = new ArrayList<String>();
      // the template may specify a from address
      if (StringUtils.isNotBlank(rt.getFrom())) {
        headers.add("From: \"" + rt.getFrom());
      } else {
        headers.add("From: \"" + fromName + "\" <" + fromEmail + ">");
      }
      // Add a To: header of either the recipient (if only 1), or the sender (if multiple)
      String toName = fromName;
      String toEmail = fromEmail;

      if (toAddress.size() == 1) {
        User u = toAddress.get(0);
        toName = u.getDisplayName();
        toEmail = u.getEmail();
      }

      headers.add("To: \"" + toName + "\" <" + toEmail + ">");

      // SAK-21742 we need the rendered subject
      headers.add("Subject: " + rt.getRenderedSubject());
      headers.add("Content-Type: multipart/alternative; boundary=\"" + MULTIPART_BOUNDARY + "\"");
      headers.add("Mime-Version: 1.0");
      headers.add("Precedence: bulk");

      String body = message.toString();
      log.debug("message body " + body);
      emailService.sendToUsers(toAddress, headers, body);
    }
  }