Skip to content

Commit

Permalink
used imap
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Vallette authored and Alexandre Vallette committed Dec 8, 2014
1 parent 4cbf718 commit e375c50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ will produce a file `out.pdf` with the processed image.
This script for cleaning is inspired by pyimageseach.


## Install
## Logins

Use
In a `login.json` at the root of the project:

```
sudo apt-get -y install libopencv-dev python-opencv
{"host": "gmail.com", "password": "yourpassword", "message": "test", "user": "bxnode.scan@gmail.com", "port": 587}
```


## Dependency

sudo apt-get -y install getmail4
mkdir -p ~/mail/new
mkdir -p ~/mail/cur
mkdir -p ~/mail/tmp
You'll need opencv installed:

```
sudo apt-get -y install libopencv-dev python-opencv
```
24 changes: 14 additions & 10 deletions check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import os
import re
import json
import smtplib
import poplib
import smtplib, imaplib
from email import parser
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
Expand All @@ -13,12 +12,13 @@

login = json.loads(open("login.json","r").read())

pop_conn = poplib.POP3_SSL("pop." + login["host"])
pop_conn.user(login["user"])
pop_conn.pass_(login["password"])

newmails = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
newmails = ["\n".join(mssg[1]) for mssg in newmails]
# get unseen mails
mail = imaplib.IMAP4_SSL("imap." + login["host"], 993)
mail.login(login["user"], login["password"])
mail.select('INBOX')
result, data = mail.search(None, "ALL")
uids = data[0].split()
newmails = [mail.fetch(uids[i], "(RFC822)")[1][0][1] for i in range(len(uids))]

for mail in newmails:
# parse mail and content
Expand Down Expand Up @@ -46,10 +46,10 @@
a4 = "false"

# process images and add them to answer
for attachment in msg.get_payload():
for part in msg.walk():
atype, format = attachment.get_content_type().split("/")
if atype == "image":
open('attachment.' + format, 'wb').write(attachment.get_payload(decode=True))
open('attachment.' + format, 'wb').write(part.get_payload(decode=True))
check_call(["python", os.path.abspath("scan.py"), "-i", 'attachment.' + format, "-b", bw, "-a", a4])
img_data = open(os.path.abspath("out.pdf"), 'rb').read()
image = MIMEImage(img_data, name=os.path.basename("out.pdf"), _subtype="pdf")
Expand All @@ -64,6 +64,10 @@
s.sendmail(login["user"], sender, msg.as_string())
s.quit()

# mark as read
for e_id in uids:
mail.store(e_id, '+FLAGS', '\Seen')




Expand Down

0 comments on commit e375c50

Please sign in to comment.