2020-01-05 13:15:02 +00:00
import smtplib
from datetime import datetime
from email . mime . multipart import MIMEMultipart
from email . mime . text import MIMEText
from email . header import Header
2020-02-26 21:13:44 +00:00
from geruecht import getLogger
LOGGER = getLogger ( ' E-MailController ' )
2020-01-05 13:15:02 +00:00
class EmailController ( ) :
2020-02-26 21:13:44 +00:00
def __init__ ( self , smtpServer , user , passwd , crypt , port = 587 , email = " " ) :
2020-01-05 13:15:02 +00:00
self . smtpServer = smtpServer
self . port = port
self . user = user
self . passwd = passwd
2020-02-26 21:13:44 +00:00
self . crypt = crypt
2020-01-05 13:15:02 +00:00
if email :
self . email = email
else :
self . email = user
2020-02-26 21:13:44 +00:00
LOGGER . debug ( ' Init EmailController with smtpServer= {} , port= {} , user= {} , crypt= {} , email= {} ' . format ( smtpServer , user , port , crypt , self . email ) )
2020-01-05 13:15:02 +00:00
def __connect__ ( self ) :
2020-02-26 21:13:44 +00:00
LOGGER . info ( ' Connect to E-Mail-Server ' )
if self . crypt == ' SSL ' :
self . smtp = smtplib . SMTP_SSL ( self . smtpServer , self . port )
log = self . smtp . ehlo ( )
LOGGER . debug ( log )
if self . crypt == ' STARTTLS ' :
self . smtp = smtplib . SMTP ( self . smtpServer , self . port )
log = self . smtp . ehlo ( )
LOGGER . debug ( log )
log = self . smtp . starttls ( )
LOGGER . debug ( log )
log = self . smtp . login ( self . user , self . passwd )
LOGGER . debug ( log )
2020-01-05 13:15:02 +00:00
def sendMail ( self , user ) :
try :
if user . mail == ' None ' or not user . mail :
LOGGER . debug ( " cant send email to {} . Has no email-address. {} " . format ( user . uid , { ' error ' : True , ' user ' : { ' userId ' : user . uid , ' firstname ' : user . firstname , ' lastname ' : user . lastname } } ) )
raise Exception ( " no valid Email " )
msg = MIMEMultipart ( )
msg [ ' From ' ] = self . email
msg [ ' To ' ] = user . mail
2020-02-19 20:47:44 +00:00
msg [ ' Subject ' ] = Header ( ' Gerücht, bezahle deine Schulden! ' , ' utf-8 ' )
2020-01-05 13:15:02 +00:00
sum = user . getGeruecht ( datetime . now ( ) . year ) . getSchulden ( )
if sum < 0 :
2020-02-19 20:47:44 +00:00
type = ' Schulden '
2020-01-05 13:15:02 +00:00
add = ' Bezahle diese umgehend an den Finanzer. '
else :
type = ' Guthaben '
add = ' '
text = MIMEText ( " Hallo {} {} , \n Du hast {} im Wert von {:.2f} €. {} \n \n Diese Nachricht wurde automatisch erstellt. " . format ( user . firstname , user . lastname , type , abs ( sum ) / 100 , add ) , ' plain ' , ' utf-8 ' )
msg . attach ( text )
LOGGER . debug ( " Send email to {} : ' {} ' " . format ( user . uid , msg . as_string ( ) ) )
self . __connect__ ( )
self . smtp . sendmail ( self . email , user . mail , msg . as_string ( ) )
LOGGER . debug ( " Sended email to {} . {} " . format ( user . uid , { ' error ' : False , ' user ' : { ' userId ' : user . uid , ' firstname ' : user . firstname , ' lastname ' : user . lastname } } ) )
return { ' error ' : False , ' user ' : { ' userId ' : user . uid , ' firstname ' : user . firstname , ' lastname ' : user . lastname } }
except Exception :
2020-02-19 20:47:44 +00:00
return { ' error ' : True , ' user ' : { ' userId ' : user . uid , ' firstname ' : user . firstname , ' lastname ' : user . lastname } }