93 lines
4.3 KiB
Python
93 lines
4.3 KiB
Python
import smtplib
|
|
from datetime import datetime
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
from email.header import Header
|
|
from geruecht.logger import getDebugLogger
|
|
from . import mailConfig
|
|
|
|
debug = getDebugLogger()
|
|
|
|
class EmailController():
|
|
|
|
def __init__(self):
|
|
debug.info("init email controller")
|
|
self.smtpServer = mailConfig['URL']
|
|
self.port = mailConfig['port']
|
|
self.user = mailConfig['user']
|
|
self.passwd = mailConfig['passwd']
|
|
self.crypt = mailConfig['crypt']
|
|
self.email = mailConfig['email']
|
|
|
|
debug.debug("smtpServer is {{ {} }}, port is {{ {} }}, user is {{ {} }}, crypt is {{ {} }}, email is {{ {} }}".format(self.smtpServer, self.port, self.user, self.crypt, self.email))
|
|
|
|
def __connect__(self):
|
|
debug.info('connect to email server')
|
|
if self.crypt == 'SSL':
|
|
self.smtp = smtplib.SMTP_SSL(self.smtpServer, self.port)
|
|
log = self.smtp.ehlo()
|
|
debug.debug("ehlo is {{ {} }}".format(log))
|
|
if self.crypt == 'STARTTLS':
|
|
self.smtp = smtplib.SMTP(self.smtpServer, self.port)
|
|
log = self.smtp.ehlo()
|
|
debug.debug("ehlo is {{ {} }}".format(log))
|
|
log = self.smtp.starttls()
|
|
debug.debug("starttles is {{ {} }}".format(log))
|
|
log = self.smtp.login(self.user, self.passwd)
|
|
debug.debug("login is {{ {} }}".format(log))
|
|
|
|
def jobTransact(self, user, jobtransact):
|
|
debug.info("create email jobtransact {{ {} }}for user {{ {} }}".format(jobtransact, user))
|
|
date = '{}.{}.{}'.format(jobtransact['date'].day, jobtransact['date'].month, jobtransact['date'].year)
|
|
from_user = '{} {}'.format(jobtransact['from_user'].firstname, jobtransact['from_user'].lastname)
|
|
subject = 'Bardienstanfrage am {}'.format(date)
|
|
text = MIMEText(
|
|
"Hallo {} {},\n"
|
|
"{} fragt, ob du am {} zum Bardienst teilnehmen willst. Beantworte die Anfrage im Userportal von Flaschengeist.".format(user.firstname, user.lastname, from_user, date), 'utf-8')
|
|
debug.debug("subject is {{ {} }}, text is {{ {} }}".format(subject, text.as_string()))
|
|
return (subject, text)
|
|
|
|
def credit(self, user):
|
|
debug.info("create email credit for user {{ {} }}".format(user))
|
|
subject = Header('Gerücht, bezahle deine Schulden!', 'utf-8')
|
|
sum = user.getGeruecht(datetime.now().year).getSchulden()
|
|
if sum < 0:
|
|
type = 'Schulden'
|
|
add = 'Bezahle diese umgehend an den Finanzer.'
|
|
else:
|
|
type = 'Guthaben'
|
|
add = ''
|
|
text = MIMEText(
|
|
"Hallo {} {},\nDu hast {} im Wert von {:.2f} €. {}\n\nDiese Nachricht wurde automatisch erstellt.".format(
|
|
user.firstname, user.lastname, type, abs(sum) / 100, add), 'plain', 'utf-8')
|
|
debug.debug("subject is {{ {} }}, text is {{ {} }}".format(subject, text.as_string()))
|
|
return (subject, text)
|
|
|
|
def sendMail(self, user, type='credit', jobtransact=None):
|
|
debug.info("send email to user {{ {} }}".format(user))
|
|
try:
|
|
if user.mail == 'None' or not user.mail:
|
|
debug.warning("user {{ {} }} has no email-address".format(user))
|
|
raise Exception("no valid Email")
|
|
msg = MIMEMultipart()
|
|
msg['From'] = self.email
|
|
msg['To'] = user.mail
|
|
|
|
if type == 'credit':
|
|
subject, text = self.credit(user)
|
|
elif type == 'jobtransact':
|
|
subject, text = self.jobTransact(user, jobtransact)
|
|
else:
|
|
raise Exception("Fail to send Email. No type is set. user={}, type={} , jobtransact={}".format(user, type, jobtransact))
|
|
|
|
msg['Subject'] = subject
|
|
msg.attach(text)
|
|
|
|
debug.debug("send email {{ {} }} to user {{ {} }}".format(msg.as_string(), user))
|
|
self.__connect__()
|
|
self.smtp.sendmail(self.email, user.mail, msg.as_string())
|
|
return {'error': False, 'user': {'userId': user.uid, 'firstname': user.firstname, 'lastname': user.lastname}}
|
|
except Exception:
|
|
debug.warning("exception in send email", exc_info=True)
|
|
return {'error': True, 'user': {'userId': user.uid, 'firstname': user.firstname, 'lastname': user.lastname}}
|