@Override
  public void sendOrcidDeactivateEmail(OrcidProfile orcidToDeactivate) {
    // Create verification url

    Map<String, Object> templateParams = new HashMap<String, Object>();

    String subject = getSubject("email.subject.deactivate", orcidToDeactivate);
    String email =
        orcidToDeactivate.getOrcidBio().getContactDetails().retrievePrimaryEmail().getValue();
    String encryptedEmail = encryptionManager.encryptForExternalUse(email);
    String base64EncodedEmail = Base64.encodeBase64URLSafeString(encryptedEmail.getBytes());
    String deactivateUrlEndpointPath = "/account/confirm-deactivate-orcid";

    String emailFriendlyName = deriveEmailFriendlyName(orcidToDeactivate);
    templateParams.put("emailName", emailFriendlyName);
    templateParams.put("orcid", orcidToDeactivate.getOrcidIdentifier().getPath());
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put(
        "deactivateUrlEndpoint", deactivateUrlEndpointPath + "/" + base64EncodedEmail);
    templateParams.put("deactivateUrlEndpointUrl", deactivateUrlEndpointPath);
    templateParams.put("subject", subject);

    addMessageParams(templateParams, orcidToDeactivate);

    // Generate body from template
    String body = templateManager.processTemplate("deactivate_orcid_email.ftl", templateParams);
    // Generate html from template
    String html =
        templateManager.processTemplate("deactivate_orcid_email_html.ftl", templateParams);

    mailGunManager.sendEmail(DEACTIVATE_NOTIFY_ORCID_ORG, email, subject, body, html);
  }
  public String createEmailBaseUrl(String unencryptedParams, String baseUri, String path) {
    // Encrypt and encode params
    String encryptedUrlParams = encryptionManager.encryptForExternalUse(unencryptedParams);
    String base64EncodedParams = null;
    try {
      base64EncodedParams = Base64.encodeBase64URLSafeString(encryptedUrlParams.getBytes("UTF-8"));

    } catch (UnsupportedEncodingException e) {
      throw new IllegalArgumentException(e);
    }
    return String.format("%s/%s/%s", baseUri, path, base64EncodedParams);
  }
  @Override
  public EmailMessage createDigest(
      OrcidProfile orcidProfile, Collection<Notification> notifications, Locale locale) {
    int totalMessageCount = 0;
    int orcidMessageCount = 0;
    int addActivitiesMessageCount = 0;
    int amendedMessageCount = 0;
    int activityCount = 0;
    Set<String> memberIds = new HashSet<>();
    DigestEmail digestEmail = new DigestEmail();
    for (Notification notification : notifications) {
      digestEmail.addNotification(notification);
      totalMessageCount++;
      if (notification.getSource() == null) {
        orcidMessageCount++;
      } else {
        SourceClientId clientId = notification.getSource().getSourceClientId();
        if (clientId != null) {
          memberIds.add(clientId.getPath());
        }
      }
      if (notification instanceof NotificationPermission) {
        addActivitiesMessageCount++;
        NotificationPermission permissionNotification = (NotificationPermission) notification;
        activityCount += permissionNotification.getItems().getItems().size();
        String encryptedPutCode =
            encryptionManager.encryptForExternalUse(
                String.valueOf(permissionNotification.getPutCode()));
        try {
          permissionNotification.setEncryptedPutCode(
              Base64.encodeBase64URLSafeString(encryptedPutCode.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
          throw new RuntimeException(
              "Problem base 64 encoding notification put code for notification id = "
                  + permissionNotification.getPutCode(),
              e);
        }
      }
      if (notification instanceof NotificationAmended) {
        amendedMessageCount++;
      }
    }
    String emailName = notificationManager.deriveEmailFriendlyName(orcidProfile);
    String subject =
        messages.getMessage(
            "email.subject.digest",
            new String[] {emailName, String.valueOf(totalMessageCount)},
            locale);
    Map<String, Object> params = new HashMap<>();
    params.put("locale", locale);
    params.put("messages", messages);
    params.put("messageArgs", new Object[0]);
    params.put("orcidProfile", orcidProfile);
    params.put("emailName", emailName);
    params.put("digestEmail", digestEmail);
    params.put(
        "frequency", orcidProfile.getOrcidInternal().getPreferences().getSendEmailFrequencyDays());
    params.put("totalMessageCount", String.valueOf(totalMessageCount));
    params.put("orcidMessageCount", orcidMessageCount);
    params.put("addActivitiesMessageCount", addActivitiesMessageCount);
    params.put("activityCount", activityCount);
    params.put("amendedMessageCount", amendedMessageCount);
    params.put("memberIdsCount", memberIds.size());
    params.put("baseUri", orcidUrlManager.getBaseUrl());
    params.put("subject", subject);
    String bodyText = templateManager.processTemplate("digest_email.ftl", params, locale);
    String bodyHtml = templateManager.processTemplate("digest_email_html.ftl", params, locale);
    EmailMessage emailMessage = new EmailMessage();

    emailMessage.setSubject(subject);
    emailMessage.setBodyText(bodyText);
    emailMessage.setBodyHtml(bodyHtml);
    return emailMessage;
  }
 @Override
 public VerifyRegistrationToken parseEncyrptedParamsForVerification(String encryptedParams) {
   String decryptedParams = encryptionManager.decryptForExternalUse(encryptedParams);
   LOGGER.debug("Got verification params: {}", decryptedParams);
   return new VerifyRegistrationToken(decryptedParams);
 }