You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"content": "This is a test for the Mu Semtech Mail Server.",
668
672
"ready": "no"
@@ -725,11 +729,11 @@ and then create `config/delta-service/subscribers.json` and put this JSON inside
725
729
If we run `docker-compose rm` and then `docker-compose up` again, the delta service will be booting and already monitoring the changes that happen in the database! Of course we are not doing anything with them yet. So we will create a new micro-service just for this purpose.
726
730
727
731
#### The mail-fetching microservice
728
-
The next step is to build our mail handling microservice. To do this we create a new directory called `mail-service` in our base directory. Then we create a file in that directory called `Dockerfile`. We will start from a mu.semte.ch template to make developing this microservice that much quicker. Mu.semte.ch has templates for a bunch of languages ruby, javascript, python, … For this microservice we will go for python 2.7. To do this we simply need to create a `web.py` file which will serve as the location for our code. Next add the following to the Dockerfile:
732
+
The next step is to build our mail handling microservice. To do this we create a new directory called `mail-service` in our base directory. Then we create a file in that directory called `Dockerfile`. We will start from a mu.semte.ch template to make developing this microservice that much quicker. Mu.semte.ch has templates for a bunch of languages ruby, javascript, python, … For this microservice we will go for python 3. To do this we simply need to create a dockerfile to build the container and a `web.py` file which will serve as the location for our code. First we create the file 'Dockerfile' in our mail-service directory:
@@ -739,74 +743,103 @@ I know it doesn’t say much, but it doesn’t need to. The python template will
739
743
Then we need to add some mail manipulating functionality. Since this is not really the objective of this post I create a `mail_helpers.py` file and paste the following code in there:
As you can see the mail_helpers contain 2 functions, one to iterate over all emails in a mailbox and the other to save a single email to the triple store. Easy peasy!
782
786
783
-
Next we create `web.py`. For more information on how the python template can be used you can visit: https://github.com/mu-semtech/mu-python-template. I created the following method to process all mails:
787
+
Next we create `web.py`. For more information on how the python template can be used you can visit: https://github.com/mu-semtech/mu-python-template. We create the following method to add a GET route to process all mails:
784
788
```python
785
789
# mail-service/web.py
790
+
from os import environ
791
+
from imaplib import IMAP4, IMAP4_SSL
792
+
793
+
import mail_helpers
794
+
795
+
EMAIL_ADDRESS = environ['EMAIL_ADDRESS']
796
+
EMAIL_PWD = environ['EMAIL_PWD']
797
+
786
798
@app.route("/fetchMails")
787
799
def fetchMailMethod():
788
-
EMAIL_ADDRESS = "address"
789
-
EMAIL_PWD = "pwd"
790
-
791
-
MAIL_SERVER = imaplib.IMAP4_SSL('imap.gmail.com')
800
+
MAIL_SERVER = IMAP4_SSL(environ['IMAP_SERVER'])
792
801
793
-
try:
794
-
MAIL_SERVER.login(EMAIL_ADDRESS, EMAIL_PWD)
795
-
except imaplib.IMAP4.error:
796
-
print "Logging into mailbox failed! "
802
+
try:
803
+
MAIL_SERVER.login(EMAIL_ADDRESS, EMAIL_PWD)
804
+
except IMAP4.error:
805
+
return "Unable to log in to IMAP server", 503
797
806
798
-
rv, data = MAIL_SERVER.select("INBOX")
799
-
if rv == 'OK':
800
-
mail_helpers.process_mailbox(MAIL_SERVER)
801
-
MAIL_SERVER.close()
807
+
rv, data = MAIL_SERVER.select("INBOX")
808
+
if rv == 'OK':
809
+
mail_helpers.process_mailbox(MAIL_SERVER)
810
+
MAIL_SERVER.close()
802
811
803
-
MAIL_SERVER.logout()
812
+
MAIL_SERVER.logout()
804
813
805
-
return "ok"
814
+
return "ok"
806
815
```
807
816
808
817
This method is rather straightforward: it just opens a connection to an email address and opens the inbox mailbox. It then selects it for processing, thus inserting all mails into the triple store.
809
818
819
+
The last step to create this service is to add it to our docker-compose.yml file:
820
+
821
+
```yaml
822
+
mailservice:
823
+
build: ./mail-service
824
+
links:
825
+
- database:database
826
+
ports:
827
+
# Forward a port for testing purposes
828
+
- "8888:80"
829
+
environment:
830
+
# Set the python template to development mode to enable auto reloading
831
+
MODE: "development"
832
+
LOG_LEVEL: "debug"
833
+
# This email should work but won't send any real mails, check ethereal.email for details
834
+
# You can replace this with a real email SMTP server but beware, this will send real mails!
0 commit comments