/**
   * Data is fetch into the repository to get the different parameters of the email
   *
   * @param id the id to find under the template section of the repository
   * @return a new <code>MgnlMail</code> instance, with the template set
   * @throws Exception if fails
   */
  public MgnlEmail getEmailFromTemplate(String id, Map map) throws Exception {
    if (id == null) {
      return new SimpleEmail(getSession());
    }
    HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
    String nodeTemplatePath = templatePath + "/" + id;
    if (!hm.isExist(nodeTemplatePath)) {
      throw new MailException("Template:[" + id + "] configuration was not found in repository");
    }

    Content node = hm.getContent(nodeTemplatePath);

    // type
    NodeData typeNode = node.getNodeData(MAIL_TYPE);
    String type = typeNode.getValue().getString();

    MgnlEmail mail = getEmailFromType(type);

    // body
    NodeData bodyNode = node.getNodeData(MAIL_BODY);
    String body = bodyNode.getValue().getString();
    mail.setBodyFromResourceFile(body, map);

    // from
    NodeData fromNode = node.getNodeData(MAIL_FROM);
    String from = fromNode.getValue().getString();
    mail.setFrom(from);

    // subject
    NodeData subjectNode = node.getNodeData(MAIL_SUBJECT);
    String subject = subjectNode.getValue().getString();
    mail.setSubject(subject);

    String attachNodePath = node.getHandle() + "/" + MAIL_ATTACHMENT;
    if (hm.isExist(attachNodePath)) {
      Content attachments = hm.getContent(attachNodePath);
      Collection atts = attachments.getChildren();
      Iterator iter = atts.iterator();
      while (iter.hasNext()) {
        Content att = (Content) iter.next();
        String cid = att.getNodeData("cid").getString();
        String url = att.getNodeData("url").getString();
        MailAttachment a = new MailAttachment(new URL(url), cid);
        mail.addAttachment(a);
      }
    }

    // parameters
    mail.setParameters(map);

    return mail;
  }