<!--- Mailbox info for "mailings@orangewhipstudios.com" --->
<CFSET POPServer = "pop.orangewhipstudios.com">
<CFSET Username  = "mailings">
<CFSET Password  = "ThreeOrangeWhips">

<!--- We will delete all messages in this list --->
<CFSET MsgDeleteList = "">

<HTML>
<HEAD><TITLE>List Unsubscriber Agent</TITLE></HEAD>
<BODY>
<H2>List Unsubscriber Agent</H2>
  
<P>Checking the mailings@orangewhipstudios.com mailbox for new messages...<BR>  
This may take a minute, depending on traffic and the number of messages.<BR>

<!--- Flush output buffer so the above messages --->
<!--- are shown while <CFPOP> is doing its work --->
<CFFLUSH>

<!--- Contact POP Server and retrieve messages --->
<CFPOP
  ACTION="GetHeaderOnly"
  NAME="GetMessages"
  SERVER="#POPServer#"
  USERNAME="#Username#"
  PASSWORD="#Password#"
  MAXROWS="20">
  
<!--- Short status message --->  
<CFOUTPUT>
  <P><STRONG>#GetMessages.RecordCount# messages to process.</STRONG><BR>
</CFOUTPUT>  
  
<!--- For each message currently in the mailbox... --->
<CFLOOP QUERY="GetMessages">
  <!--- Short status message --->
  <CFOUTPUT>
    <P><STRONG>Message from:</STRONG> #HTMLEditFormat(GetMessages.From)#<BR>
  </CFOUTPUT>

  <!--- If the subject line contains the word "Remove" --->
  <CFIF GetMessages.Subject does not contain "Remove">
    <!--- Short status message --->
    Message does not contain "Remove".<BR>
  <CFELSE>   
    <!--- Short status message --->
    Message contains "Remove".<BR>

    <!--- Which "word" in From address contains @ sign? --->
    <CFSET AddrPos = ListContains(GetMessages.From, "@", "<> ")>
    <!--- Assuming one of the "words" contains @ sign, --->
    <CFIF AddrPos EQ 0>
      <!--- Short status message --->
      Address not found in From line.<BR>
    <CFELSE>
    
      <!--- Email address is that word in From address --->
      <CFSET FromAddress = ListGetAt(GetMessages.From, AddrPos, "<> ")>
      
      <!--- Who in mailing list has this email address? --->
      <CFQUERY NAME="GetContact" DATASOURCE="#REQUEST.DataSource#" MAXROWS="1">
        SELECT ContactID, FirstName, LastName 
        FROM Contacts
        WHERE Email = '#FromAddress#'
        AND MailingList = 1
      </CFQUERY>

      <!--- Assuming someone has this address... --->      
      <CFIF GetContact.RecordCount EQ 0>
        <!--- Short status message --->
        <CFOUTPUT>Recipient #FromAddress# not on list.<BR></CFOUTPUT>
      <CFELSE>
        <!--- Short status message --->
        <CFOUTPUT>Removing #FromAddress# from list.<BR></CFOUTPUT>
      
        <!--- Update the database to take them off list --->
        <CFQUERY DATASOURCE="#REQUEST.DataSource#">
          UPDATE Contacts SET
            MailingList = 0
          WHERE ContactID = #GetContact.ContactID#  
        </CFQUERY>
      
        <!--- Short status message --->
        Sending confirmation message via email.<BR>
      
        <!--- Mail user a confirmation note --->
        <CFMAIL
          TO="""#GetContact.FirstName# #GetContact.LastName#"" <#FromAddress#>"
          FROM="""Orange Whip Studios"" <mailings@orangewhipstudios.com>"
          SUBJECT="Mailing List Request"
        >You have been removed from our mailing list.</CFMAIL>
      
      </CFIF>
    </CFIF>
  </CFIF>
  
  <!--- Add this message to the list of ones to delete. --->
  <!--- If you wanted to only delete some messages, you --->
  <!--- would put some kind of <CFIF> test around this. --->
  <CFSET MsgDeleteList = ListAppend(MsgDeleteList, GetMessages.MessageNumber)>
</CFLOOP>


<!--- If there are messages to delete --->
<CFIF MsgDeleteList NEQ "">
  <!--- Short status message --->
  <P>Deleting messages...

  <!--- Flush output buffer so the above messages --->
  <!--- are shown while <CFPOP> is doing its work --->
  <CFFLUSH>
  
  <!--- Contact POP Server and delete messages --->
  <CFPOP
    ACTION="Delete"
    SERVER="#POPServer#"
    USERNAME="#Username#"
    PASSWORD="#Password#"
    MESSAGENUMBER="#MsgDeleteList#">
    
  Done.<BR>  
</CFIF>

</BODY>
</HTML>  
