forked from hghotra/eml2pdflib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
56 lines (45 loc) · 1.78 KB
/
test.py
File metadata and controls
56 lines (45 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import imaplib
import email
import os
from lib.eml2html import EmailtoHtml
from lib.html2pdf import HtmltoPdf
from lib.html2img import HtmltoImage
EMAIL_ADDRESS = os.environ['EMAIL_ADDRESS']
EMAIL_PSWD = os.environ['EMAIL_PSWD']
EMAIL_MAILBOX = os.environ['EMAIL_MAILBOX']
IMAP_SERVER = os.environ['IMAP_SERVER']
class EmailHelper(object):
def __init__(self, IMAP_SERVER, EMAIL_ADDRESS,
EMAIL_PSWD, EMAIL_MAILBOX):
# logs in to the desired account and navigates to the inbox
self.mail = imaplib.IMAP4_SSL(IMAP_SERVER)
self.mail.login(EMAIL_ADDRESS, EMAIL_PSWD)
self.mail.select()
def get_emails(self):
uids = self.mail.uid('SEARCH', 'ALL')[1][0].split()
return uids
def get_email_message(self, email_id):
_, data = self.mail.uid('FETCH', email_id, '(RFC822)')
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
return email_message
email_helper = EmailHelper(IMAP_SERVER, EMAIL_ADDRESS,
EMAIL_PSWD, EMAIL_MAILBOX)
email_to_html_convertor = EmailtoHtml()
html_to_pdf_convertor = HtmltoPdf()
html_to_img_convertor = HtmltoImage()
uids = email_helper.get_emails()
dir_path = os.path.dirname(os.path.realpath(__file__))
output_dir = os.path.join(dir_path, "outputs")
for uid in uids:
email_message = email_helper.get_email_message(uid)
html = email_to_html_convertor.convert(email_message)
filename = uid.decode() + ".jpg"
img_path = html_to_img_convertor.save_img(
html.encode(), output_dir, filename)
print(img_path)
filename = uid.decode() + ".pdf"
pdf_path = html_to_pdf_convertor.save_pdf(
html.encode(), output_dir, filename)
print(pdf_path)