<!--- 
  Filename: CheckMailMsg3.cfm
  Author:   Nate Weiss (NMW)
  Purpose:  Allows the user to view a message on their POP server
--->

<html>
<head><title>Mail Message</title></head>
<body>

<!--- Simple CSS-based formatting styles --->
<style>
  body {font-family:sans-serif;font-size:12px}
  th   {font-size:12px;background:navy;color:white}
  td   {font-size:12px;background:lightgrey;color:navy}
</style>

<h2>Mail Message</h2>

<!--- A message number must be passed in the URL --->
<cfparam name="URL.msgNum" type="numeric">
<cfparam name="URL.delete" type="boolean" default="No">
<cfparam name="URL.attachmentName" type="string" default="">

<!--- Store attachments in "Attach" subfolder --->
<cfset attachDir = getTempDirectory()>
<!--- Set a variable to hold the Tab character --->
<cfset TAB = chr(9)>
  
<!--- Create the folder if it doesn't already exist --->
<cfif not directoryExists(attachDir)>
  <cfdirectory action="create" directory="#attachDir#">
</cfif>

<!--- If we don't have a username/password --->
<!--- send user to main CheckMail.cfm page --->
<cfif not isDefined("SESSION.mail.getMessages")>
  <cflocation url="CheckMail.cfm">
</cfif>

<!--- If the user is trying to delete the message --->
<cfif URL.delete>
  <!--- Contact POP Server and delete the message --->
  <cfpop action="Delete"
    messagenumber="#URL.msgNum#"
    server="#SESSION.mail.popServer#"
    username="#SESSION.mail.username#"
    password="#SESSION.mail.password#">

  <!--- Send user back to main "Check Mail" page --->    
  <cflocation url="CheckMail.cfm?refresh=Yes">


<!--- If not deleting, retrieve and show the message --->
<cfelse>

  <!--- Contact POP Server and retrieve the message --->
  <cfpop action="GetAll" name="getMsg"
    messagenumber="#URL.msgNum#"
    server="#SESSION.mail.popServer#"
    username="#SESSION.mail.username#"
    password="#SESSION.mail.password#"
    attachmentPath="#attachDir#"
    generateUniqueFilenames="Yes">
    
  <!--- Parse message's date string to CF Date value --->  
  <cfset msgDate = parseDateTime(getMsg.Date, "pop")>  
  
  <!--- If message was not retrieved from POP server --->  
  <cfif getMsg.recordCount neq 1>
    <cfthrow 
      message="Message could not be retrieved."
      detail="Perhaps the message has already been deleted.">
  </cfif>  
  
  <!--- We will provide a link to Delete message --->
  <cfset deleteURL = "#CGI.script_name#?msgNum=#msgNum#&delete=Yes">
    
  <!--- Display message in a simple table format --->  
  <table border="0" cellSpacing="0" cellPadding="3">
    <cfoutput>
      <tr>
        <th bgcolor="wheat" align="left" nowrap>
          Message #URL.msgNum# of #SESSION.mail.getMessages.recordCount#
        </th>
        <td align="right" bgcolor="beige">
          <!--- Provide "Back" button, if appropriate --->    
          <cfif URL.msgNum gt 1>
            <a href="CheckMailMsg.cfm?MsgNum=#decrementValue(URL.msgNum)#">
              <img src="../images/browseback.gif" 
                width="40" height="16" alt="Back" border="0"></a>
          </cfif>
          <!--- Provide "Next" button, if appropriate --->
          <cfif URL.msgNum lt SESSION.mail.getMessages.recordCount>
            <a href="CheckMailMsg.cfm?msgNum=#incrementValue(URL.msgNum)#">
              <img src="../images/browsenext.gif" 
                width="40" height="16" alt="Next" border="0"></a>
          </cfif>
        </td>
      </TR>
      <tr>
        <th align="right">From:</th>
        <td>#htmlEditFormat(getMsg.From)#</td>
      </tr>
      <cfif getMsg.CC neq "">
      <tr>
        <th align="right">CC:</th>
        <td>#htmlEditFormat(getMsg.CC)#</td>
      </tr>
      </cfif>
      <tr>
        <th align="right">Date:</th>
        <td>#dateFormat(msgDate)# #timeFormat(msgDate)#</td>
      </tr>
      <tr>
        <th align="right">Subject:</th>
        <td>#getMsg.Subject#</td>
      </tr>
      <tr>
        <td bgcolor="beige" colspan="2">
          <strong>Message:</strong><br>
          
          <cfif getMsg.Header contains "Content-Type: text/html">
            #getMsg.Body# 
          <cfelse>
            #htmlCodeFormat(getMsg.Body)#
          </cfif>
        </td>
      </tr>
      <!--- If this message has any attachments --->
      <cfset numAttachments = listLen(getMsg.Attachments, TAB)>
      <cfif numAttachments gt 0>
        <tr>
          <th align="right">Attachments:</th>
          <td>
            <!--- For each attachment, provide a link  --->
            <cfloop from="1" to="#numAttachments#" index="i">
              <!--- Original filename, as it was attached to message  --->
              <cfset thisFileOrig = listGetAt(getMsg.Attachments, i, TAB)>
              <!--- Full path to file, as it was saved on this server --->
              <cfset thisFilePath = listGetAt(getMsg.AttachmentFiles, i, TAB)>
              
              <!--- Link to download attachment --->
              <!--- Reloads this page with AttachmentName parameter in URL --->
              <cfset link = CGI.script_name & "?msgNum=#URL.msgNum#">
              <cfset link = link & "&attachmentName=">
              <cfset link = link & "#urlEncodedFormat(thisFileOrig)#">  
              <a href="#link#">#thisFileOrig#</a>
              <br>
              
              <!--- If the user is trying to download this attachment --->
              <cfif thisFileOrig eq URL.attachmentName>
                <!--- We'll send the file via <CFCONTENT>, 
                after this <CFLOOP> block --->
                <cfset downloadFile = thisFilePath>
                <!--- Suggest default filename for attachment --->
                <cfheader name="content-disposition" 
                value="attachment; filename=#thisFileOrig#">
              <!--- If the user is not trying to download this attachment --->  
              <cfelse>
                <!--- Just delete the attachment --->
                <cffile action="Delete" file="#thisFilePath#">
              </cfif>
            </cfloop>

            <!--- If we found the attachment the user is requesting --->
            <cfif isDefined("DownloadFile")>
              <!--- Stream it to the user, then delete it from disk --->
              <cfcontent type="unknown/type" file="#downloadFile#"
                deletefile="yes" reset="Yes">
            </cfif>
          </td>
        </tr>
      </cfif>
    </cfoutput>
  </table>  

  <cfoutput>  
    <!--- Provide link back to list of messages --->
    <strong><a href="CheckMail.cfm">Back To Message List</a></strong><br>
    <!--- Provide link to Delete message --->
    <a href="#deleteURL#">Delete Message</a><br>
    <!--- "Log Out" link to discard SESSION.Mail info --->
    <a href="CheckMail.cfm?Logout=Yes">Log Out</a><br>
  </cfoutput>
</cfif>

</body>
</html>

