def CONFSUBMIT_Send_APP_Mail(parameters, curdir, form, user_info=None):
    """
    This function send an email informing the original submitter of a
    document that the referee has approved/ rejected the document.

    Parameters:

       * addressesAPP: email addresses of the people who will receive
         this email (comma separated list). this parameter may contain
         the <CATEG> string. In which case the variable computed from
         the [categformatAFP] parameter replaces this string.
         eg.: "<CATEG>[email protected]"

       * categformatAPP contains a regular expression used to compute
         the category of the document given the reference of the
         document.
         eg.: if [categformatAFP]="TEST-<CATEG>-.*" and the reference
         of the document is "TEST-CATEGORY1-2001-001", then the computed
         category equals "CATEGORY1"

       * emailFile: Name of the file containing the email of the
                    submitter of the document

       * newrnin: Name of the file containing the 2nd reference of the
                  document (if any).

       * decision_file: Name of the file containing the decision of the
                document.

       * comments_file: Name of the file containing the comments of the
                document.

       * edsrn: Name of the file containing the reference of the
                document.
    """
    global titlevalue,authorvalue,sysno,rn
    doctype = form['doctype']
    titlevalue = titlevalue.replace("\n"," ")
    authorvalue = authorvalue.replace("\n","; ")
    # variables declaration
    categformat = parameters['categformatAPP']
    otheraddresses = parameters['addressesAPP']
    newrnpath = parameters['newrnin']
    ## Get the name of the decision file:
    try:
        decision_filename = parameters['decision_file']
    except KeyError:
        decision_filename = ""
    ## Get the name of the comments file:
    try:
        comments_filename = parameters['comments_file']
    except KeyError:
        comments_filename = ""

    ## Now try to read the comments from the comments_filename:
    if comments_filename in (None, "", "NULL"):
        ## We don't have a name for the comments file.
        ## For backward compatibility reasons, try to read the comments from
        ## a file called 'COM' in curdir:
        if os.path.exists("%s/COM" % curdir):
            try:
                fh_comments = open("%s/COM" % curdir, "r")
                comment = fh_comments.read()
                fh_comments.close()
            except IOError:
                ## Unable to open the comments file
                exception_prefix = "Error in WebSubmit function " \
                                   "Send_APP_Mail. Tried to open " \
                                   "comments file [%s/COM] but was " \
                                   "unable to." % curdir
                register_exception(prefix=exception_prefix)
                comment = ""
            else:
                comment = comment.strip()
        else:
            comment = ""
    else:
        ## Try to read the comments from the comments file:
        if os.path.exists("%s/%s" % (curdir, comments_filename)):
            try:
                fh_comments = open("%s/%s" % (curdir, comments_filename), "r")
                comment = fh_comments.read()
                fh_comments.close()
            except IOError:
                ## Oops, unable to open the comments file.
                comment = ""
                exception_prefix = "Error in WebSubmit function " \
                                "Send_APP_Mail. Tried to open comments " \
                                "file [%s/%s] but was unable to." \
                                % (curdir, comments_filename)
                register_exception(prefix=exception_prefix)
            else:
                comment = comment.strip()
        else:
            comment = ""

    ## Now try to read the decision from the decision_filename:
    if decision_filename in (None, "", "NULL"):
        ## We don't have a name for the decision file.
        ## For backward compatibility reasons, try to read the decision from
        ## a file called 'decision' in curdir:
        if os.path.exists("%s/decision" % curdir):
            try:
                fh_decision = open("%s/decision" % curdir, "r")
                decision = fh_decision.read()
                fh_decision.close()
            except IOError:
                ## Unable to open the decision file
                exception_prefix = "Error in WebSubmit function " \
                                   "Send_APP_Mail. Tried to open " \
                                   "decision file [%s/decision] but was " \
                                   "unable to." % curdir
                register_exception(prefix=exception_prefix)
                decision = ""
            else:
                decision = decision.strip()
        else:
            decision = ""
    else:
        ## Try to read the decision from the decision file:
        try:
            fh_decision = open("%s/%s" % (curdir, decision_filename), "r")
            decision = fh_decision.read()
            fh_decision.close()
        except IOError:
            ## Oops, unable to open the decision file.
            decision = ""
            exception_prefix = "Error in WebSubmit function " \
                               "Send_APP_Mail. Tried to open decision " \
                               "file [%s/%s] but was unable to." \
                               % (curdir, decision_filename)
            register_exception(prefix=exception_prefix)
        else:
            decision = decision.strip()

    if os.path.exists("%s/%s" % (curdir,newrnpath)):
        fp = open("%s/%s" % (curdir,newrnpath) , "r")
        newrn = fp.read()
        fp.close()
    else:
        newrn = ""
    # Document name
    res = run_sql("SELECT ldocname FROM sbmDOCTYPE WHERE sdocname=%s", (doctype,))
    docname = res[0][0]
    # retrieve category
    categformat = categformat.replace("<CATEG>", "([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # Creation of the mail for the referee
    otheraddresses = otheraddresses.replace("<CATEG>",category)
    addresses = ""
    if otheraddresses != "":
        addresses += otheraddresses
    else:
        addresses = re.sub(",$","",addresses)

    ## Add the record's submitter(s) into the list of recipients:
    # The submitters email address is read from the file specified by 'emailFile'
    try:
        fp = open("%s/%s" % (curdir,parameters['emailFile']),"r")
        addresses += fp.read().replace ("\n"," ")
        fp.close()
    except:
        pass

    if decision == "approve":
        mailtitle = "%s has been approved" % rn
        mailbody = "The submitted conference with reference number %s has been fully approved." % (rn,)
        mailbody += "\n\nIt will soon become visible in the INSPIRE-HEP Conference database - <%s/Conferences>" % (CFG_SITE_URL,)
    else:
        mailtitle = "%s has been rejected" % rn
        mailbody = "The %s %s has been rejected." % (docname,rn)
    if rn != newrn and decision == "approve" and newrn != "":
        mailbody += "\n\nIts new reference number is: %s" % newrn
    mailbody += "\n\nTitle: %s\n\nContact(s): %s\n\n" % (titlevalue,authorvalue)
    if comment != "":
        mailbody += "Comments from the referee:\n%s\n" % comment
    # Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_CONF_FROMADDR, toaddr=addresses, subject=mailtitle, \
               content=mailbody, footer=email_footer(support_email=CFG_WEBSUBMIT_CONF_SUPPORT_EMAIL), copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN)
    return ""
def CONFSUBMIT_Send_Approval_Request(parameters, curdir, form, user_info=None):
    """
    This function sends an email to the referee in order to start the
    simple approval process.  This function is very CERN-specific and
    should be changed in case of external use.  Must be called after
    the Get_Report_Number function.

    Parameters:

       * addressesDAM: email addresses of the people who will receive
                       this email (comma separated list). this
                       parameter may contain the <CATEG> string. In
                       which case the variable computed from the
                       [categformatDAM] parameter replaces this
                       string.
                       eg.:"<CATEG>[email protected]"

       * categformatDAM: contains a regular expression used to compute
                         the category of the document given the
                         reference of the document.

                         eg.: if [categformatAFP]="TEST-<CATEG>-.*"
                         and the reference of the document is
                         "TEST-CATEGORY1-2001-001", then the computed
                         category equals "CATEGORY1"

       * titleFile: name of the file in which the title is stored.

       * submitteremailfile: name of the file in which the title is stored.

       * submitternamefile: name of the file in which the title is stored.

       * contactnamefile: name of the file in which the title is stored.

       * contactemailfile: name of the file in which the title is stored.

       * referencefile: name of the file in which the title is stored.

       * affiliationfile: name of the file in which the title is stored.

       * regionfile: name of the file in which the title is stored.

       * rankfile: name of the file in which the title is stored.

       * fieldfile: name of the file in which the title is stored.

       * experimentsfile: name of the file in which the title is stored.

       * urlfile: name of the file in which the title is stored.

       * datefile: name of the file in which the title is stored.

       * abstractfile: name of the file in which the title is stored.

       * seriesnamefile: name of the file where the series name is stored.

       * seriesnumberfile: name of the file where the series number is stored.

       * directory: parameter used to create the URL to access the
                    files.
    """
    global rn, sysno
    # variables declaration
    doctype = re.search(".*/([^/]*)/([^/]*)/[^/]*$", curdir).group(2)
    otheraddresses = parameters['addressesDAM']
    categformat = parameters['categformatDAM']
    # retrieve category
    categformat = categformat.replace("<CATEG>", "([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # get record data
    date = get_file_contents(curdir, "date")
    title = get_file_contents(curdir,
                              parameters['titleFile']).replace("\n", "")
    title += " - %s" % date
    submitteremail = get_file_contents(
        curdir, parameters['submitteremailfile']).replace("\n", ", ")
    submittername = get_file_contents(curdir,
                                      parameters['submitternamefile']).replace(
                                          "\n", ", ")
    contactname = get_file_contents(curdir,
                                    parameters['contactnamefile']).replace(
                                        "\n", ", ")
    contactemail = get_file_contents(curdir,
                                     parameters['contactemailfile']).replace(
                                         "\n", ", ")
    subtitle = get_file_contents(curdir,
                                 parameters['subtitle']).replace("\n", ", ")
    city = get_file_contents(curdir,
                             parameters['cityfile']).replace("\n", ", ")
    country = get_file_contents(curdir,
                                parameters['countryfile']).replace("\n", ", ")
    state = get_file_contents(curdir,
                              parameters['statefile']).replace("\n", ", ")
    stdate = get_file_contents(curdir,
                               parameters['stdatefile']).replace("\n", ", ")
    fndate = get_file_contents(curdir,
                               parameters['fndatefile']).replace("\n", ", ")
    field = get_file_contents(curdir,
                              parameters['fieldfile']).replace("\n", ", ")
    url = get_file_contents(curdir, parameters['urlfile']).replace("\n", " ")
    shorttitle = get_file_contents(curdir, parameters['shorttitle']).replace(
        "\n", " ")
    keywords = get_file_contents(curdir,
                                 parameters['keywords']).replace("\n", " ")
    proceedings = get_file_contents(curdir, parameters['proceedings']).replace(
        "\n", " ")
    seriesname = get_file_contents(curdir,
                                   parameters['seriesnamefile']).replace(
                                       "\n", " ")
    seriesnumber = get_file_contents(curdir,
                                     parameters['seriesnumberfile']).replace(
                                         "\n", " ")
    abstract = get_file_contents(curdir, parameters['abstractfile'])

    # we get the referee password
    sth = run_sql("SELECT access FROM sbmAPPROVAL WHERE rn=%s", (rn, ))
    if len(sth) > 0:
        access = sth[0][0]
    # Build referee's email address
    refereeaddress = ""
    # Try to retrieve the referee's email from the referee's database
    for user in acc_get_role_users(
            acc_get_role_id("referee_%s_%s" % (doctype, category))):
        refereeaddress += user[1] + ","
    # And if there are general referees
    for user in acc_get_role_users(acc_get_role_id("referee_%s_*" % doctype)):
        refereeaddress += user[1] + ","
    refereeaddress = re.sub(",$", "", refereeaddress)
    # Creation of the mail for the referee
    addresses = ""
    if refereeaddress != "":
        addresses = refereeaddress + ","
    if otheraddresses != "":
        addresses += otheraddresses
    else:
        addresses = re.sub(",$", "", addresses)
    record_url = "%s/%s/%s" % (CFG_SITE_URL, CFG_SITE_RECORD, sysno)
    title_referee = "Request for approval of %s" % rn
    mail_referee = """
The document %(rn)s has been submitted to the Conferences database and it will appear here:\n%(recordlink)s.
To approve/reject the document, you should go to this URL:\n%(access)s\n

Title: %(title)s
Date: from %(stdate)s to %(fndate)s
Place: %(city)s, %(state)s, %(country)s
Series name: %(seriesname)s
Series number: %(seriesnumber)s

URL: %(url)s

Field(s): %(field)s

Description:
%(abstract)s

Contact name(s): %(contactname)s
Contact email(s): %(contactemail)s
Submitter name(s): %(submittername)s
Submitter email(s): %(submitteremail)s
    """ % {
        'rn': rn,
        'title': title,
        'submitteremail': submitteremail,
        'submittername': submittername,
        'contactname': contactname,
        'contactemail': contactemail,
        'field': field,
        'city': city,
        'state': state,
        'country': country,
        'stdate': stdate,
        'fndate': fndate,
        'url': url,
        'subtitle': subtitle,
        'shorttitle': shorttitle,
        'proceedings': proceedings,
        'keywords': keywords,
        'access': "%s/approve.py?access=%s" % (CFG_SITE_URL, access),
        'recordlink': record_url,
        'abstract': abstract,
        'seriesname': seriesname,
        'seriesnumber': seriesnumber
    }
    #Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_CONF_FROMADDR, toaddr=CFG_WEBSUBMIT_CONF_SUPPORT_EMAIL, subject=title_referee, \
               content=mail_referee, footer=email_footer(support_email=CFG_WEBSUBMIT_CONF_SUPPORT_EMAIL),
               copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN, bccaddr=addresses, replytoaddr=contactemail)
    return ""
def CONFSUBMIT_Send_Approval_Request (parameters, curdir, form, user_info=None):
    """
    This function sends an email to the referee in order to start the
    simple approval process.  This function is very CERN-specific and
    should be changed in case of external use.  Must be called after
    the Get_Report_Number function.

    Parameters:

       * addressesDAM: email addresses of the people who will receive
                       this email (comma separated list). this
                       parameter may contain the <CATEG> string. In
                       which case the variable computed from the
                       [categformatDAM] parameter replaces this
                       string.
                       eg.:"<CATEG>[email protected]"

       * categformatDAM: contains a regular expression used to compute
                         the category of the document given the
                         reference of the document.

                         eg.: if [categformatAFP]="TEST-<CATEG>-.*"
                         and the reference of the document is
                         "TEST-CATEGORY1-2001-001", then the computed
                         category equals "CATEGORY1"

       * titleFile: name of the file in which the title is stored.

       * submitteremailfile: name of the file in which the title is stored.

       * submitternamefile: name of the file in which the title is stored.

       * contactnamefile: name of the file in which the title is stored.

       * contactemailfile: name of the file in which the title is stored.

       * referencefile: name of the file in which the title is stored.

       * affiliationfile: name of the file in which the title is stored.

       * regionfile: name of the file in which the title is stored.

       * rankfile: name of the file in which the title is stored.

       * fieldfile: name of the file in which the title is stored.

       * experimentsfile: name of the file in which the title is stored.

       * urlfile: name of the file in which the title is stored.

       * datefile: name of the file in which the title is stored.

       * abstractfile: name of the file in which the title is stored.

       * seriesnamefile: name of the file where the series name is stored.

       * seriesnumberfile: name of the file where the series number is stored.

       * directory: parameter used to create the URL to access the
                    files.
    """
    global rn,sysno
    # variables declaration
    doctype = re.search(".*/([^/]*)/([^/]*)/[^/]*$",curdir).group(2)
    otheraddresses = parameters['addressesDAM']
    categformat = parameters['categformatDAM']
    # retrieve category
    categformat = categformat.replace("<CATEG>","([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # get record data
    date = get_file_contents(curdir, "date")
    title = get_file_contents(curdir, parameters['titleFile']).replace("\n","")
    title += " - %s" % date
    submitteremail = get_file_contents(curdir, parameters['submitteremailfile']).replace("\n",", ")
    submittername = get_file_contents(curdir, parameters['submitternamefile']).replace("\n",", ")
    contactname = get_file_contents(curdir, parameters['contactnamefile']).replace("\n",", ")
    contactemail = get_file_contents(curdir, parameters['contactemailfile']).replace("\n",", ")
    subtitle = get_file_contents(curdir, parameters['subtitle']).replace("\n",", ")
    city = get_file_contents(curdir, parameters['cityfile']).replace("\n",", ")
    country = get_file_contents(curdir, parameters['countryfile']).replace("\n",", ")
    state = get_file_contents(curdir, parameters['statefile']).replace("\n",", ")
    stdate = get_file_contents(curdir, parameters['stdatefile']).replace("\n",", ")
    fndate = get_file_contents(curdir, parameters['fndatefile']).replace("\n",", ")
    field = get_file_contents(curdir, parameters['fieldfile']).replace("\n",", ")
    url = get_file_contents(curdir, parameters['urlfile']).replace("\n"," ")
    shorttitle = get_file_contents(curdir, parameters['shorttitle']).replace("\n"," ")
    keywords = get_file_contents(curdir, parameters['keywords']).replace("\n"," ")
    proceedings = get_file_contents(curdir, parameters['proceedings']).replace("\n"," ")
    seriesname = get_file_contents(curdir, parameters['seriesnamefile']).replace("\n"," ")
    seriesnumber = get_file_contents(curdir, parameters['seriesnumberfile']).replace("\n"," ")
    abstract = get_file_contents(curdir, parameters['abstractfile'])

    # we get the referee password
    sth = run_sql("SELECT access FROM sbmAPPROVAL WHERE rn=%s", (rn,))
    if len(sth) >0:
        access = sth[0][0]
    # Build referee's email address
    refereeaddress = ""
    # Try to retrieve the referee's email from the referee's database
    for user in acc_get_role_users(acc_get_role_id("referee_%s_%s" % (doctype,category))):
        refereeaddress += user[1] + ","
    # And if there are general referees
    for user in acc_get_role_users(acc_get_role_id("referee_%s_*" % doctype)):
        refereeaddress += user[1] + ","
    refereeaddress = re.sub(",$","",refereeaddress)
    # Creation of the mail for the referee
    addresses = ""
    if refereeaddress != "":
        addresses = refereeaddress + ","
    if otheraddresses != "":
        addresses += otheraddresses
    else:
        addresses = re.sub(",$","",addresses)
    record_url = "%s/%s/%s" % (CFG_SITE_URL, CFG_SITE_RECORD, sysno)
    title_referee = "Request for approval of %s" % rn
    mail_referee = """
The document %(rn)s has been submitted to the Conferences database and it will appear here:\n%(recordlink)s.
To approve/reject the document, you should go to this URL:\n%(access)s\n

Title: %(title)s
Date: from %(stdate)s to %(fndate)s
Place: %(city)s, %(state)s, %(country)s
Series name: %(seriesname)s
Series number: %(seriesnumber)s

URL: %(url)s

Field(s): %(field)s

Description:
%(abstract)s

Contact name(s): %(contactname)s
Contact email(s): %(contactemail)s
Submitter name(s): %(submittername)s
Submitter email(s): %(submitteremail)s
    """ % {'rn' : rn,
           'title' : title,
           'submitteremail' : submitteremail,
           'submittername' : submittername,
           'contactname' : contactname,
           'contactemail' : contactemail,
           'field' : field,
           'city' : city,
           'state' : state,
           'country' : country,
           'stdate' : stdate,
           'fndate' : fndate,
           'url' : url,
           'subtitle' : subtitle,
           'shorttitle' : shorttitle,
           'proceedings' : proceedings,
           'keywords' : keywords,
           'access' : "%s/approve.py?access=%s" % (CFG_SITE_URL,access),
           'recordlink' : record_url,
           'abstract' : abstract,
           'seriesname' : seriesname,
           'seriesnumber' : seriesnumber
           }
    #Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_CONF_FROMADDR, toaddr=CFG_WEBSUBMIT_CONF_SUPPORT_EMAIL, subject=title_referee, \
               content=mail_referee, footer=email_footer(support_email=CFG_WEBSUBMIT_CONF_SUPPORT_EMAIL),
               copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN, bccaddr=addresses, replytoaddr=contactemail)
    return ""
def CONFSUBMIT_Send_APP_Mail(parameters, curdir, form, user_info=None):
    """
    This function send an email informing the original submitter of a
    document that the referee has approved/ rejected the document.

    Parameters:

       * addressesAPP: email addresses of the people who will receive
         this email (comma separated list). this parameter may contain
         the <CATEG> string. In which case the variable computed from
         the [categformatAFP] parameter replaces this string.
         eg.: "<CATEG>[email protected]"

       * categformatAPP contains a regular expression used to compute
         the category of the document given the reference of the
         document.
         eg.: if [categformatAFP]="TEST-<CATEG>-.*" and the reference
         of the document is "TEST-CATEGORY1-2001-001", then the computed
         category equals "CATEGORY1"

       * emailFile: Name of the file containing the email of the
                    submitter of the document

       * newrnin: Name of the file containing the 2nd reference of the
                  document (if any).

       * decision_file: Name of the file containing the decision of the
                document.

       * comments_file: Name of the file containing the comments of the
                document.

       * edsrn: Name of the file containing the reference of the
                document.
    """
    global titlevalue, authorvalue, sysno, rn
    doctype = form['doctype']
    titlevalue = titlevalue.replace("\n", " ")
    authorvalue = authorvalue.replace("\n", "; ")
    # variables declaration
    categformat = parameters['categformatAPP']
    otheraddresses = parameters['addressesAPP']
    newrnpath = parameters['newrnin']
    ## Get the name of the decision file:
    try:
        decision_filename = parameters['decision_file']
    except KeyError:
        decision_filename = ""
    ## Get the name of the comments file:
    try:
        comments_filename = parameters['comments_file']
    except KeyError:
        comments_filename = ""

    ## Now try to read the comments from the comments_filename:
    if comments_filename in (None, "", "NULL"):
        ## We don't have a name for the comments file.
        ## For backward compatibility reasons, try to read the comments from
        ## a file called 'COM' in curdir:
        if os.path.exists("%s/COM" % curdir):
            try:
                fh_comments = open("%s/COM" % curdir, "r")
                comment = fh_comments.read()
                fh_comments.close()
            except IOError:
                ## Unable to open the comments file
                exception_prefix = "Error in WebSubmit function " \
                                   "Send_APP_Mail. Tried to open " \
                                   "comments file [%s/COM] but was " \
                                   "unable to." % curdir
                register_exception(prefix=exception_prefix)
                comment = ""
            else:
                comment = comment.strip()
        else:
            comment = ""
    else:
        ## Try to read the comments from the comments file:
        if os.path.exists("%s/%s" % (curdir, comments_filename)):
            try:
                fh_comments = open("%s/%s" % (curdir, comments_filename), "r")
                comment = fh_comments.read()
                fh_comments.close()
            except IOError:
                ## Oops, unable to open the comments file.
                comment = ""
                exception_prefix = "Error in WebSubmit function " \
                                "Send_APP_Mail. Tried to open comments " \
                                "file [%s/%s] but was unable to." \
                                % (curdir, comments_filename)
                register_exception(prefix=exception_prefix)
            else:
                comment = comment.strip()
        else:
            comment = ""

    ## Now try to read the decision from the decision_filename:
    if decision_filename in (None, "", "NULL"):
        ## We don't have a name for the decision file.
        ## For backward compatibility reasons, try to read the decision from
        ## a file called 'decision' in curdir:
        if os.path.exists("%s/decision" % curdir):
            try:
                fh_decision = open("%s/decision" % curdir, "r")
                decision = fh_decision.read()
                fh_decision.close()
            except IOError:
                ## Unable to open the decision file
                exception_prefix = "Error in WebSubmit function " \
                                   "Send_APP_Mail. Tried to open " \
                                   "decision file [%s/decision] but was " \
                                   "unable to." % curdir
                register_exception(prefix=exception_prefix)
                decision = ""
            else:
                decision = decision.strip()
        else:
            decision = ""
    else:
        ## Try to read the decision from the decision file:
        try:
            fh_decision = open("%s/%s" % (curdir, decision_filename), "r")
            decision = fh_decision.read()
            fh_decision.close()
        except IOError:
            ## Oops, unable to open the decision file.
            decision = ""
            exception_prefix = "Error in WebSubmit function " \
                               "Send_APP_Mail. Tried to open decision " \
                               "file [%s/%s] but was unable to." \
                               % (curdir, decision_filename)
            register_exception(prefix=exception_prefix)
        else:
            decision = decision.strip()

    if os.path.exists("%s/%s" % (curdir, newrnpath)):
        fp = open("%s/%s" % (curdir, newrnpath), "r")
        newrn = fp.read()
        fp.close()
    else:
        newrn = ""
    # Document name
    res = run_sql("SELECT ldocname FROM sbmDOCTYPE WHERE sdocname=%s",
                  (doctype, ))
    docname = res[0][0]
    # retrieve category
    categformat = categformat.replace("<CATEG>", "([^-]*)")
    m_categ_search = re.match(categformat, rn)
    if m_categ_search is not None:
        if len(m_categ_search.groups()) > 0:
            ## Found a match for the category of this document. Get it:
            category = m_categ_search.group(1)
        else:
            ## This document has no category.
            category = "unknown"
    else:
        category = "unknown"

    # Creation of the mail for the referee
    otheraddresses = otheraddresses.replace("<CATEG>", category)
    addresses = ""
    if otheraddresses != "":
        addresses += otheraddresses
    else:
        addresses = re.sub(",$", "", addresses)

    ## Add the record's submitter(s) into the list of recipients:
    # The submitters email address is read from the file specified by 'emailFile'
    try:
        fp = open("%s/%s" % (curdir, parameters['emailFile']), "r")
        addresses += fp.read().replace("\n", " ")
        fp.close()
    except:
        pass

    if decision == "approve":
        mailtitle = "%s has been approved" % rn
        mailbody = "The submitted conference with reference number %s has been fully approved." % (
            rn, )
        mailbody += "\n\nIt will soon become visible in the INSPIRE-HEP Conference database - <%s/Conferences>" % (
            CFG_SITE_URL, )
    else:
        mailtitle = "%s has been rejected" % rn
        mailbody = "The %s %s has been rejected." % (docname, rn)
    if rn != newrn and decision == "approve" and newrn != "":
        mailbody += "\n\nIts new reference number is: %s" % newrn
    mailbody += "\n\nTitle: %s\n\nContact(s): %s\n\n" % (titlevalue,
                                                         authorvalue)
    if comment != "":
        mailbody += "Comments from the referee:\n%s\n" % comment
    # Send mail to referee
    send_email(fromaddr=CFG_WEBSUBMIT_CONF_FROMADDR, toaddr=addresses, subject=mailtitle, \
               content=mailbody, footer=email_footer(support_email=CFG_WEBSUBMIT_CONF_SUPPORT_EMAIL), copy_to_admin=CFG_WEBSUBMIT_COPY_MAILS_TO_ADMIN)
    return ""