old_contents: from app import mail, app, db
  
  from os.path import expanduser, isfile
  
  from flask.ext.mail import Message
  
  import pystache
  import logging
  
  
  def send_email_template_for_user(id_user, template, server, **kwargs):
      from app.User.services import get_user
  
      logging.info("Sending email to user: %s (%s)", id_user, template)
  
      params = {}
      for key, value in kwargs.items():
          params[key] = value
  
      user = get_user(id_user)
      if user is None:
          return False
  
      params['screenname'] = user.screen_name
  
      send_email_template_to_address(user.email, template, server, user.locale, params)
  
  
  def send_email_template_to_address(recipient, template, server, locale, params=None, **kwargs):
      # Read templates
      params = params or {}
      for key, value in kwargs.items():
          params[key] = value
      params['email'] = recipient
      params['locale'] = locale
  
      (subject, plain, rich) = _read_templates(template, server, locale, params)
  
      send_email(recipient, subject, plain, rich)
  
  
  def send_email(recipient, subject, email_text, rich_email_text=None):
      msg = Message(
          recipients=[recipient],
          subject=subject.rstrip(),
          body=email_text,
          html=rich_email_text,
          sender=app.config['DEFAULT_MAIL_SENDER']
      )
      mail.send(msg)
  
  
  def _read_templates(template, server, locale, params):
      header = _read_template(template, server, locale, 'header', params)
      plain = _read_template(template, server, locale, 'plain', params)
      rich = _read_template(template, server, locale, 'rich', params, True)
  
      return header, plain, rich
  
  
  def _read_template(template, server, locale, part, params, none_if_missing=False):
      template_file = expanduser("~/templates/%s/%s/%s/%s.mustache" % (locale, template, server, part))
      if isfile(template_file):
          logging.debug('Looking for template file: %s', template_file)
          renderer = pystache.Renderer()
          rendered = renderer.render_path(template_file, params)
          #print(rendered)
          return rendered
      else:
          logging.error('Looking for template file: %s, but the file is missing', template_file)
          if none_if_missing:
              return None
          else:
              return 'Template missing'
  
new_contents: from app import mail, app, db
  
  from os.path import expanduser, isfile
  
  from flask.ext.mail import Message
  
  import pystache
  import logging
  
  
  def send_email_template_for_user(id_user, template, server, **kwargs):
      from app.User.services import get_user
  
      logging.info("Sending email to user: %s (%s)", id_user, template)
  
      params = {}
      for key, value in kwargs.items():
          params[key] = value
  
      user = get_user(id_user)
      if user is None:
          return False
  
      params['screenname'] = user.screen_name
  
      send_email_template_to_address(user.email, template, server, user.locale, params)
  
  
  def send_email_template_to_address(recipient, template, server, locale, params=None, **kwargs):
      # Read templates
      params = params or {}
      for key, value in kwargs.items():
          params[key] = value
      params['email'] = recipient
      params['locale'] = locale
  
      (subject, plain, rich) = _read_templates(template, server, locale, params)
  
      send_email(recipient, subject, plain, rich)
  
  
  def send_email(recipient, subject, email_text, rich_email_text=None):
      msg = Message(
          recipients=[recipient],
          subject=subject.rstrip(),
          body=email_text,
          html=rich_email_text,
          sender=app.config['DEFAULT_MAIL_SENDER']
      )
      mail.send(msg)
  
  
  def _read_templates(template, server, locale, params):
      header = _read_template(template, server, locale, 'header', params)
      plain = _read_template(template, server, locale, 'plain', params)
      rich = _read_template(template, server, locale, 'rich', params, True)
  
      return header, plain, rich
  
  
  def _read_template(template, server, locale, part, params, none_if_missing=False):
      """
      Render a mustache template.
  
      :param template: Base template name
      :param server: Host server
      :param locale: Language designator
      :param part: Generic message type descriptor
      :param params: Text string to replace tags embedded within the template
      :param none_if_missing: Return 'none' if the requested template is not found
  
      :return: Upon success, return a Renderer object. Return none or a general
               error message if the none_is_missing flag is false
      """
      template_file = expanduser("~/templates/%s/%s/%s/%s.mustache" % (locale, template, server, part))
      if isfile(template_file):
          logging.debug('Looking for template file: %s', template_file)
          renderer = pystache.Renderer()
          rendered = renderer.render_path(template_file, params)
          return rendered
      else:
          logging.warn('Looking for template file: %s, but the file is missing', template_file)
          if none_if_missing:
              return None
          else:
              return 'Template missing'
  
subject: Change characterization of a missing template file as a warning instead of an error. Added function documentation.