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-03-10 08:19:11 +00:00
from geruecht . logger import getDebugLogger
2020-05-22 19:55:14 +00:00
from . import mailConfig
2020-02-26 21:13:44 +00:00
2020-03-10 18:23:52 +00:00
debug = getDebugLogger ( )
2020-01-05 13:15:02 +00:00
class EmailController ( ) :
2020-05-22 19:55:14 +00:00
def __init__ ( self ) :
2020-03-10 18:23:52 +00:00
debug . info ( " init email controller " )
2020-05-22 19:55:14 +00:00
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 ) )
2020-01-05 13:15:02 +00:00
def __connect__ ( self ) :
2020-03-10 18:23:52 +00:00
debug . info ( ' connect to email server ' )
2020-02-26 21:13:44 +00:00
if self . crypt == ' SSL ' :
self . smtp = smtplib . SMTP_SSL ( self . smtpServer , self . port )
log = self . smtp . ehlo ( )
2020-03-10 18:23:52 +00:00
debug . debug ( " ehlo is {{ {} }} " . format ( log ) )
2020-02-26 21:13:44 +00:00
if self . crypt == ' STARTTLS ' :
self . smtp = smtplib . SMTP ( self . smtpServer , self . port )
log = self . smtp . ehlo ( )
2020-03-10 18:23:52 +00:00
debug . debug ( " ehlo is {{ {} }} " . format ( log ) )
2020-02-26 21:13:44 +00:00
log = self . smtp . starttls ( )
2020-03-10 18:23:52 +00:00
debug . debug ( " starttles is {{ {} }} " . format ( log ) )
2020-02-26 21:13:44 +00:00
log = self . smtp . login ( self . user , self . passwd )
2020-03-10 18:23:52 +00:00
debug . debug ( " login is {{ {} }} " . format ( log ) )
2020-01-05 13:15:02 +00:00
2020-02-27 14:01:41 +00:00
def jobTransact ( self , user , jobtransact ) :
2020-03-10 18:23:52 +00:00
debug . info ( " create email jobtransact {{ {} }}for user {{ {} }} " . format ( jobtransact , user ) )
2020-06-06 13:51:14 +00:00
date = ' {} . {} . {} ' . format ( jobtransact [ ' on_date ' ] [ ' day ' ] , jobtransact [ ' on_date ' ] [ ' month ' ] , jobtransact [ ' on_date ' ] [ ' year ' ] )
from_user = ' {} {} ' . format ( jobtransact [ ' from_user ' ] [ ' firstname ' ] , jobtransact [ ' from_user ' ] [ ' lastname ' ] )
job_kind = jobtransact [ ' job_kind ' ]
subject = ' Dienstanfrage am {} ' . format ( date )
2020-02-27 14:01:41 +00:00
text = MIMEText (
" Hallo {} {} , \n "
2020-06-28 10:31:58 +00:00
" {} fragt, ob du am {} den Dienst {} übernehmen willst. \n Beantworte die Anfrage im Userportal von Flaschengeist. " . format ( user . firstname , user . lastname , from_user , date , job_kind [ ' name ' ] ) , ' plain ' )
2020-06-06 13:51:14 +00:00
debug . debug ( " subject is {{ {} }}, text is {{ {} }} " . format ( subject , text . as_string ( ) ) )
return ( subject , text )
def jobInvite ( self , user , jobtransact ) :
debug . info ( " create email jobtransact {{ {} }}for user {{ {} }} " . format ( jobtransact , user ) )
date = ' {} . {} . {} ' . format ( jobtransact [ ' on_date ' ] [ ' day ' ] , jobtransact [ ' on_date ' ] [ ' month ' ] , jobtransact [ ' on_date ' ] [ ' year ' ] )
from_user = ' {} {} ' . format ( jobtransact [ ' from_user ' ] [ ' firstname ' ] , jobtransact [ ' from_user ' ] [ ' lastname ' ] )
subject = ' Diensteinladung am {} ' . format ( date )
text = MIMEText (
" Hallo {} {} , \n "
2020-06-28 10:31:58 +00:00
" {} fragt, ob du am {} mit Dienst haben willst. \n Beantworte die Anfrage im Userportal von Flaschengeist. " . format ( user . firstname , user . lastname , from_user , date ) , ' plain ' )
2020-03-10 18:23:52 +00:00
debug . debug ( " subject is {{ {} }}, text is {{ {} }} " . format ( subject , text . as_string ( ) ) )
2020-02-27 14:01:41 +00:00
return ( subject , text )
def credit ( self , user ) :
2020-03-10 18:23:52 +00:00
debug . info ( " create email credit for user {{ {} }} " . format ( user ) )
2020-02-27 14:01:41 +00:00
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 {} {} , \n Du hast {} im Wert von {:.2f} €. {} \n \n Diese Nachricht wurde automatisch erstellt. " . format (
2020-06-28 10:31:58 +00:00
user . firstname , user . lastname , type , abs ( sum ) / 100 , add ) , ' plain ' )
2020-03-10 18:23:52 +00:00
debug . debug ( " subject is {{ {} }}, text is {{ {} }} " . format ( subject , text . as_string ( ) ) )
2020-02-27 14:01:41 +00:00
return ( subject , text )
2020-06-28 10:31:58 +00:00
def passwordReset ( self , user , data ) :
debug . info ( " create email passwort reset for user {{ {} }} " . format ( user ) )
subject = Header ( " Password vergessen " )
text = MIMEText (
" Hallo {} {} , \n Du hast dein Password vergessen! \n Dies wurde nun mit Flaschengeist zurückgesetzt. \n Dein neues Passwort lautet: \n {} \n \n Bitte ändere es sofort in deinem Flaschengeistprolif in https://flaschengeist.wu5.de. " . format (
user . firstname , user . lastname , data [ ' password ' ]
) , ' plain '
)
debug . debug ( " subject is {{ {} }}, text is {{ {} }} " . format ( subject , text . as_string ( ) ) )
return ( subject , text )
def sendMail ( self , user , type = ' credit ' , jobtransact = None , * * kwargs ) :
2020-03-10 18:23:52 +00:00
debug . info ( " send email to user {{ {} }} " . format ( user ) )
2020-01-05 13:15:02 +00:00
try :
if user . mail == ' None ' or not user . mail :
2020-03-10 18:23:52 +00:00
debug . warning ( " user {{ {} }} has no email-address " . format ( user ) )
2020-01-05 13:15:02 +00:00
raise Exception ( " no valid Email " )
msg = MIMEMultipart ( )
msg [ ' From ' ] = self . email
msg [ ' To ' ] = user . mail
2020-02-27 14:01:41 +00:00
if type == ' credit ' :
subject , text = self . credit ( user )
elif type == ' jobtransact ' :
subject , text = self . jobTransact ( user , jobtransact )
2020-06-06 13:51:14 +00:00
elif type == ' jobinvite ' :
subject , text = self . jobInvite ( user , jobtransact )
2020-06-28 10:31:58 +00:00
elif type == ' passwordReset ' :
subject , text = self . passwordReset ( user , kwargs )
2020-01-05 13:15:02 +00:00
else :
2020-02-27 14:01:41 +00:00
raise Exception ( " Fail to send Email. No type is set. user= {} , type= {} , jobtransact= {} " . format ( user , type , jobtransact ) )
msg [ ' Subject ' ] = subject
2020-01-05 13:15:02 +00:00
msg . attach ( text )
2020-02-27 14:01:41 +00:00
2020-03-10 18:23:52 +00:00
debug . debug ( " send email {{ {} }} to user {{ {} }} " . format ( msg . as_string ( ) , user ) )
2020-01-05 13:15:02 +00:00
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 :
2020-03-10 18:23:52 +00:00
debug . warning ( " exception in send email " , exc_info = True )
2020-02-19 20:47:44 +00:00
return { ' error ' : True , ' user ' : { ' userId ' : user . uid , ' firstname ' : user . firstname , ' lastname ' : user . lastname } }