mirror of https://github.com/kianby/stacosys
manage imap/smtp directly. remove srmail dependency
parent
3281dd1aae
commit
7f5ea9c4d3
@ -1,43 +1,85 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import logging
|
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
import smtplib
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from conf import config
|
from conf import config
|
||||||
|
from core import imap
|
||||||
|
from model.email import Email
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def fetch():
|
def _open_mailbox():
|
||||||
mails = []
|
return imap.Mailbox(
|
||||||
r = requests.get(config.get(config.MAILER_URL) + "/mbox")
|
config.get(config.IMAP_HOST),
|
||||||
if r.status_code == 200:
|
config.get_int(config.IMAP_PORT),
|
||||||
payload = r.json()
|
config.get_bool(config.IMAP_SSL),
|
||||||
if payload["count"] > 0:
|
config.get(config.IMAP_LOGIN),
|
||||||
mails = payload["emails"]
|
config.get(config.IMAP_PASSWORD),
|
||||||
return mails
|
)
|
||||||
|
|
||||||
|
|
||||||
def get(id):
|
def _to_dto(msg):
|
||||||
payload = None
|
content = 'no plain-text part found in email'
|
||||||
r = requests.get(config.get(config.MAILER_URL) + "/mbox/" + str(id))
|
for part in msg['parts']:
|
||||||
if r.status_code == 200:
|
if part['content-type'] == 'text/plain':
|
||||||
payload = r.json()
|
content = part['content']
|
||||||
return payload
|
break
|
||||||
|
return Email(
|
||||||
|
id=msg['index'],
|
||||||
|
encoding=msg['encoding'],
|
||||||
|
date=msg['datetime'],
|
||||||
|
from_addr=msg['from'],
|
||||||
|
to_addr=msg['to'],
|
||||||
|
subject=msg['subject'],
|
||||||
|
content=content,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fetch():
|
||||||
|
msgs = []
|
||||||
|
try:
|
||||||
|
with _open_mailbox() as mbox:
|
||||||
|
count = mbox.get_count()
|
||||||
|
for num in range(count):
|
||||||
|
msg = _to_dto(mbox.fetch_message(num + 1))
|
||||||
|
msgs.append(msg)
|
||||||
|
except:
|
||||||
|
logger.exception('fetch mail exception')
|
||||||
|
return msgs
|
||||||
|
|
||||||
|
|
||||||
def send(to_email, subject, message):
|
def send(to_email, subject, message):
|
||||||
headers = {"Content-Type": "application/json; charset=utf-8"}
|
|
||||||
msg = {"to": to_email, "subject": subject, "content": message}
|
# Create the container (outer) email message.
|
||||||
r = requests.post(
|
msg = MIMEText(message)
|
||||||
config.get(config.MAILER_URL) + "/mbox", data=json.dumps(msg), headers=headers
|
msg['Subject'] = subject
|
||||||
)
|
msg['To'] = to_email
|
||||||
if r.status_code in (200, 201):
|
msg['From'] = config.get(config.SMTP_LOGIN)
|
||||||
logger.debug("Email for %s posted" % to_email)
|
|
||||||
else:
|
success = True
|
||||||
logger.warn("Cannot post email for %s" % to_email)
|
try:
|
||||||
|
s = smtplib.SMTP(config.get(config.SMTP_HOST), config.getInt(config.SMTP_PORT))
|
||||||
|
if config.get_bool(config.SMTP_STARTTLS):
|
||||||
|
s.starttls()
|
||||||
|
s.login(config.get(config.SMTP_LOGIN), config.get(config.SMTP_PASSWORD))
|
||||||
|
s.send_message(msg)
|
||||||
|
s.quit()
|
||||||
|
except:
|
||||||
|
logger.exception('send mail exception')
|
||||||
|
success = False
|
||||||
|
return success
|
||||||
|
|
||||||
|
|
||||||
def delete(id):
|
def delete(id):
|
||||||
requests.delete(config.get(config.MAILER_URL) + "/mbox/" + str(id))
|
try:
|
||||||
|
with _open_mailbox() as mbox:
|
||||||
|
mbox.delete_message(id)
|
||||||
|
except:
|
||||||
|
logger.exception('delete mail exception')
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
# -*- coding: UTF-8 -*-
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
from conf import config
|
|
||||||
|
|
||||||
|
|
||||||
def salt(value):
|
|
||||||
string = "%s%s" % (value, config.get(config.SECURITY_SALT))
|
|
||||||
dk = hashlib.sha256(string.encode())
|
|
||||||
return dk.hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
def md5(value):
|
|
||||||
dk = hashlib.md5(value.encode())
|
|
||||||
return dk.hexdigest()
|
|
@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
|
from typing import NamedTuple
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class Email(NamedTuple):
|
||||||
|
id: int
|
||||||
|
encoding: str
|
||||||
|
date: datetime
|
||||||
|
from_addr: str
|
||||||
|
to_addr: str
|
||||||
|
subject: str
|
||||||
|
content: str
|
Loading…
Reference in New Issue