From 70dceb3b379f87f6ff51582609e96ce1bc761a89 Mon Sep 17 00:00:00 2001
From: David Martin <david_martin@fastmail.com>
Date: Sat, 20 May 2017 16:48:40 +1000
Subject: [PATCH 1/6] Allow to configure the email inbox via config file.

Same as all the other parameters it makes sense to set it in the config
file as well.
---
 paperless.conf.example    | 4 ++++
 src/paperless/settings.py | 6 ++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/paperless.conf.example b/paperless.conf.example
index 98029b3cc..e923f26b6 100644
--- a/paperless.conf.example
+++ b/paperless.conf.example
@@ -37,6 +37,10 @@ PAPERLESS_CONSUME_MAIL_PORT=""
 PAPERLESS_CONSUME_MAIL_USER=""
 PAPERLESS_CONSUME_MAIL_PASS=""
 
+# Override the default IMAP inbox here. If not set paperless defaults to
+# "INBOX".
+#PAPERLESS_CONSUME_MAIL_INBOX=""
+
 
 ###############################################################################
 ####                              Security                                 ####
diff --git a/src/paperless/settings.py b/src/paperless/settings.py
index f95c69be5..bab997537 100644
--- a/src/paperless/settings.py
+++ b/src/paperless/settings.py
@@ -244,8 +244,10 @@ MAIL_CONSUMPTION = {
     "PORT": os.getenv("PAPERLESS_CONSUME_MAIL_PORT"),
     "USERNAME": os.getenv("PAPERLESS_CONSUME_MAIL_USER"),
     "PASSWORD": os.getenv("PAPERLESS_CONSUME_MAIL_PASS"),
-    "USE_SSL": os.getenv("PAPERLESS_CONSUME_MAIL_USE_SSL", "y").lower() == "y",  # If True, use SSL/TLS to connect
-    "INBOX": "INBOX"  # The name of the inbox on the server
+    # If True, use SSL/TLS to connect
+    "USE_SSL": os.getenv("PAPERLESS_CONSUME_MAIL_USE_SSL", "y").lower() == "y",
+    # The name of the inbox on the server
+    "INBOX": os.getenv("PAPERLESS_CONSUME_MAIL_INBOX", "INBOX")
 }
 
 # This is used to encrypt the original documents and decrypt them later when

From c647daace2d49a4178ae002104ee18b336eab71e Mon Sep 17 00:00:00 2001
From: David Martin <david_martin@fastmail.com>
Date: Sun, 21 May 2017 08:34:49 +1000
Subject: [PATCH 2/6] Connect to configured inbox instead of hardcoded one.

Now the retrieving of emails from the inbox set in the config file works
as expected.
---
 src/documents/mail.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/documents/mail.py b/src/documents/mail.py
index 012ee0cf9..af4371652 100644
--- a/src/documents/mail.py
+++ b/src/documents/mail.py
@@ -219,7 +219,7 @@ class MailFetcher(Loggable):
         if not login[0] == "OK":
             raise MailFetcherError("Can't log into mail: {}".format(login[1]))
 
-        inbox = self._connection.select("INBOX")
+        inbox = self._connection.select(self._inbox)
         if not inbox[0] == "OK":
             raise MailFetcherError("Can't find the inbox: {}".format(inbox[1]))
 

From 7b1812a9bed5aeb133d1818fe6a2ef0178ec98ef Mon Sep 17 00:00:00 2001
From: David Martin <david_martin@fastmail.com>
Date: Sun, 21 May 2017 08:44:41 +1000
Subject: [PATCH 3/6] Capitalise Paperless in example config.

This is in line with how it is spelled in the rest of the config file.
---
 paperless.conf.example | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/paperless.conf.example b/paperless.conf.example
index e923f26b6..1ba573cfb 100644
--- a/paperless.conf.example
+++ b/paperless.conf.example
@@ -37,7 +37,7 @@ PAPERLESS_CONSUME_MAIL_PORT=""
 PAPERLESS_CONSUME_MAIL_USER=""
 PAPERLESS_CONSUME_MAIL_PASS=""
 
-# Override the default IMAP inbox here. If not set paperless defaults to
+# Override the default IMAP inbox here. If not set Paperless defaults to
 # "INBOX".
 #PAPERLESS_CONSUME_MAIL_INBOX=""
 

From 3153bbd6a8d674362eccb4d48b8458b33298f6a9 Mon Sep 17 00:00:00 2001
From: David Martin <david_martin@fastmail.com>
Date: Sun, 21 May 2017 14:23:46 +1000
Subject: [PATCH 4/6] Fetch emails right at startup instead of waiting for 10
 minutes.

Especially when first setting up the configuration for consuming
documents from emails it makes sense to quickly test the changes. Having
to wait for 10 minutes is not acceptable.

There are two ways around it that come to my mind: the simple approach
is to always fetch the emails when Paperless first starts. This way the
fetching of emails can be tested straight away.
The alternative would be to have a configuration option that allows to
set the interval in which emails are checked. The user could then reduce
it to test the setup and increase it again later on. This seems
needlessly complicated though, so fetching at startup it is.
---
 src/documents/management/commands/document_consumer.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.py
index d3b406fe7..f50b489ee 100644
--- a/src/documents/management/commands/document_consumer.py
+++ b/src/documents/management/commands/document_consumer.py
@@ -28,6 +28,7 @@ class Command(BaseCommand):
 
         self.file_consumer = None
         self.mail_fetcher = None
+        self.first_iteration = True
 
         BaseCommand.__init__(self, *args, **kwargs)
 
@@ -66,6 +67,9 @@ class Command(BaseCommand):
         self.file_consumer.consume()
 
         # Occasionally fetch mail and store it to be consumed on the next loop
+        # We fetch email when we first start up so that it is not necessary to
+        # wait for 10 minutes after making changes to the config file.
         delta = self.mail_fetcher.last_checked + self.MAIL_DELTA
-        if delta < datetime.datetime.now():
+        if self.first_iteration or delta < datetime.datetime.now():
+            self.first_iteration = False
             self.mail_fetcher.pull()

From 13b4610c1d9c2dfa7554e591c4c77869d503a177 Mon Sep 17 00:00:00 2001
From: David Martin <david_martin@fastmail.com>
Date: Sun, 21 May 2017 14:52:12 +1000
Subject: [PATCH 5/6] Clarify consumption documentation to match the current
 Paperless behaviour.

The configuration does not have to be hardcoded in settings.py anymore,
and instead happens in the config file. Also, we added that the emails
are checked at startup [0].

[0] see commit 3153bbd6a8d674362eccb4d48b8458b33298f6a9
---
 docs/consumption.rst | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/docs/consumption.rst b/docs/consumption.rst
index 8fa0a9a32..ee5ee4f64 100644
--- a/docs/consumption.rst
+++ b/docs/consumption.rst
@@ -121,14 +121,15 @@ So, with all that in mind, here's what you do to get it running:
 
 1. Setup a new email account somewhere, or if you're feeling daring, create a
    folder in an existing email box and note the path to that folder.
-2. In ``settings.py`` set all of the appropriate values in ``MAIL_CONSUMPTION``.
+2. In ``/etc/paperless.conf`` set all of the appropriate values in
+   ``PATHS AND FOLDERS`` and ``SECURITY``.
    If you decided to use a subfolder of an existing account, then make sure you
-   set ``INBOX`` accordingly here.  You also have to set the
-   ``UPLOAD_SHARED_SECRET`` to something you can remember 'cause you'll have to
-   include that in every email you send.
+   set ``PAPERLESS_CONSUME_MAIL_INBOX`` accordingly here.  You also have to set
+   the ``PAPERLESS_SHARED_SECRET`` to something you can remember 'cause you'll
+   have to include that in every email you send.
 3. Restart the :ref:`consumer <utilities-consumer>`.  The consumer will check
-   the configured email account every 10 minutes for something new and pull down
-   whatever it finds.
+   the configured email account at startup and from then on every 10 minutes
+   for something new and pulls down whatever it finds.
 4. Send yourself an email!  Note that the subject is treated as the file name,
    so if you set the subject to ``Correspondent - Title - tag,tag,tag``, you'll
    get what you expect.  Also, you must include the aforementioned secret

From 8c06dc2dd1663d1c65f4525916b99b38c11864d3 Mon Sep 17 00:00:00 2001
From: David Martin <david_martin@fastmail.com>
Date: Sun, 21 May 2017 15:03:02 +1000
Subject: [PATCH 6/6] Mention safe characters for email titles in
 documentation.

This makes it clear that only a specific set of characters is allowed to
be used for email titles. It is worth mentioning this in the
documentation as it otherwise needs to be figured out from the Paperless
sources [0].

[0] SAFE_REGEX in src/documents/models.py
---
 docs/consumption.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docs/consumption.rst b/docs/consumption.rst
index ee5ee4f64..280ba6d50 100644
--- a/docs/consumption.rst
+++ b/docs/consumption.rst
@@ -134,6 +134,8 @@ So, with all that in mind, here's what you do to get it running:
    so if you set the subject to ``Correspondent - Title - tag,tag,tag``, you'll
    get what you expect.  Also, you must include the aforementioned secret
    string in every email so the fetcher knows that it's safe to import.
+   Note that Paperless only allows the email title to consist of safe characters
+   to be imported. These consist of alpha-numeric characters and ``-_ ,.'``.
 5. After a few minutes, the consumer will poll your mailbox, pull down the
    message, and place the attachment in the consumption directory with the
    appropriate name.  A few minutes later, the consumer will import it like any