title: How to set IMAP flags using Twisted
body: <p>How do you delete messages using imap4.IMAP4Client? I cannot get the "deleted" tag correctly applied for using the "expunge" method.</p>
  
  <p>I keep getting the following error:</p>
  
  <pre><code>Failure: twisted.mail.imap4.IMAP4Exception: Invalid system flag \
  </code></pre>
  
  <p>Sample code would be appreciated. This is what I have so far:</p>
  
  <pre><code>from twisted.internet import protocol, reactor
  from twisted.mail import imap4
  
  #Variables for connection
  username = 'user@host.com'
  password = 'mypassword'
  host = 'imap.host.com'
  port = 143
  
  class IMAP4LocalClient(imap4.IMAP4Client):
      def connectionMade(self):
          self.login(username,password).addCallbacks(self._getMessages, self._ebLogin)
  
      #reports any connection errors
      def connectionLost(self,reason):
          reactor.stop()
  
      #drops the connection
      def _ebLogin(self,result):
          print result
          self.transport.loseConnection()
  
      def _programUtility(self,result):
          print result
          return self.logout()
  
      def _cbExpungeMessage(self,result):
          return self.expunge().addCallback(self._programUtility)
  
      def _cbDeleteMessage(self,result):
          return self.setFlags("1:5",flags=r"\\Deleted",uid=False).addCallback(self._cbExpungeMessage)
  
      #gets the mailbox list
      def _getMessages(self,result):
          return self.list("","*").addCallback(self._cbPickMailbox)
  
      #selects the inbox desired    
      def _cbPickMailbox(self,result):
          mbox='INBOX.Trash'
          return self.select(mbox).addCallback(self._cbExamineMbox)
  
      def _cbExamineMbox(self,result):
          return self.fetchMessage("1:*",uid=False).addCallback(self._cbDeleteMessage)
  
  class IMAP4ClientFactory(protocol.ClientFactory):
      def buildProtocol(self,addr):
          return IMAP4LocalClient()
  
      def clientConnectionFailed(self,connector,reason):
          print reason
          reactor.stop()
  
  
  reactor.connectTCP(host,port,IMAP4ClientFactory())
  reactor.run()
  </code></pre>
  
  <p>Changed to:</p>
  
  <pre><code>def _cbDeleteMessage(self,result):
      return self.setFlags("1:5",flags=['\\Deleted'],uid=False).addCallback(self._cbExpungeMessage)
  </code></pre>
  
  <p>thanks to Jean-Paul Calderone and it worked, setFlags requires a list, not just a string.</p>