public void notifyNewUserEmail(User user, String newUserPassword, String siteTitle) {

    String from = getSetupRequestEmailAddress();
    String productionSiteName = serverConfigurationService.getString("ui.service", "");
    String newUserEmail = user.getEmail();
    String to = newUserEmail;
    String headerTo = newUserEmail;
    String replyTo = newUserEmail;

    String content = "";

    if (from != null && newUserEmail != null) {
      /*
       * $userName
       * $localSakaiName
       * $currentUserName
       * $localSakaiUrl
       */
      Map<String, String> replacementValues = new HashMap<String, String>();
      replacementValues.put("userName", user.getDisplayName());
      replacementValues.put(
          "localSakaiName", serverConfigurationService.getString("ui.service", ""));
      replacementValues.put(
          "currentUserName", userDirectoryService.getCurrentUser().getDisplayName());
      replacementValues.put("userEid", user.getEid());
      replacementValues.put("localSakaiUrl", serverConfigurationService.getPortalUrl());
      replacementValues.put("newPassword", newUserPassword);
      replacementValues.put("siteName", siteTitle);
      replacementValues.put("productionSiteName", productionSiteName);
      RenderedTemplate template =
          emailTemplateService.getRenderedTemplateForUser(
              NOTIFY_NEW_USER, user.getReference(), replacementValues);
      if (template == null) return;
      content = template.getRenderedMessage();

      String message_subject = template.getRenderedSubject();
      List<String> headers = new ArrayList<String>();
      headers.add("Precedence: bulk");
      emailService.send(from, to, message_subject, content, headerTo, replyTo, headers);
    }
  }
  public RenderedTemplate getRenderedTemplate(
      String key, Locale locale, Map<String, String> replacementValues) {
    EmailTemplate temp = getEmailTemplate(key, locale);
    // if no template was found we need to return null to avoid an NPE
    if (temp == null) return null;

    RenderedTemplate ret = new RenderedTemplate(temp);

    // get the default current user fields
    log.debug("getting default values");

    Map<String, String> userVals = getCurrentUserFields();
    replacementValues.putAll(userVals);
    log.debug("got replacement values");

    ret.setRenderedSubject(this.processText(ret.getSubject(), replacementValues, key));
    ret.setRenderedMessage(this.processText(ret.getMessage(), replacementValues, key));
    // HTML component might be null
    if (ret.getHtmlMessage() != null)
      ret.setRenderedHtmlMessage(this.processText(ret.getHtmlMessage(), replacementValues, key));
    return ret;
  }
  public void notifyAddedParticipant(boolean newNonOfficialAccount, User user, String siteTitle) {

    String from =
        serverConfigurationService.getBoolean(NOTIFY_FROM_CURRENT_USER, false)
            ? getCurrentUserEmailAddress()
            : getSetupRequestEmailAddress();
    // we need to get the template

    if (from != null) {
      String productionSiteName = serverConfigurationService.getString("ui.service", "");
      String emailId = user.getEmail();
      String to = emailId;
      String headerTo = emailId;
      String replyTo = emailId;
      Map<String, String> rv = new HashMap<String, String>();
      rv.put("productionSiteName", productionSiteName);

      String content = "";
      /*
       * $userName
       * $localSakaiName
       * $currentUserName
       * $localSakaiUrl
       */
      Map<String, String> replacementValues = new HashMap<String, String>();
      replacementValues.put("userName", user.getDisplayName());
      replacementValues.put("userEid", user.getEid());
      replacementValues.put(
          "localSakaiName", serverConfigurationService.getString("ui.service", ""));
      replacementValues.put(
          "currentUserName", userDirectoryService.getCurrentUser().getDisplayName());
      replacementValues.put("localSakaiUrl", serverConfigurationService.getPortalUrl());
      String nonOfficialAccountUrl =
          serverConfigurationService.getString("nonOfficialAccount.url", null);
      replacementValues.put(
          "hasNonOfficialAccountUrl",
          nonOfficialAccountUrl != null
              ? Boolean.TRUE.toString().toLowerCase()
              : Boolean.FALSE.toString().toLowerCase());
      replacementValues.put("nonOfficialAccountUrl", nonOfficialAccountUrl);
      replacementValues.put("siteName", siteTitle);
      replacementValues.put("productionSiteName", productionSiteName);
      replacementValues.put(
          "newNonOfficialAccount", Boolean.valueOf(newNonOfficialAccount).toString().toLowerCase());

      M_log.debug("getting template: sitemange.notifyAddedParticipant");
      RenderedTemplate template = null;
      try {
        template =
            emailTemplateService.getRenderedTemplateForUser(
                NOTIFY_ADDED_PARTICIPANT, user.getReference(), replacementValues);
        if (template == null) return;
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }
      List<String> headers = new ArrayList<String>();
      headers.add("Precedence: bulk");

      content = template.getRenderedMessage();
      emailService.send(
          from, to, template.getRenderedSubject(), content, headerTo, replyTo, headers);
    } // if
  }
  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);
    }
  }