File size: 2,911 Bytes
8247f19 | 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 57 58 59 60 61 62 63 64 65 66 | # Patch-overlapping Python context after the fix
## `sydent/util/emailutils.py` — `function:sendEmail` (lines 34-92)
```python
def sendEmail(sydent, templateName, mailTo, substitutions):
mailFrom = sydent.cfg.get('email', 'email.from')
mailTemplateFile = sydent.cfg.get('email', templateName)
myHostname = sydent.cfg.get('email', 'email.hostname')
if myHostname == '':
myHostname = socket.getfqdn()
midRandom = "".join([random.choice(string.ascii_letters) for _ in range(16)])
messageid = "<%d%s@%s>" % (time_msec(), midRandom, myHostname)
allSubstitutions = {}
allSubstitutions.update(substitutions)
allSubstitutions.update({
'messageid': messageid,
'date': email.utils.formatdate(localtime=False),
'to': mailTo,
'from': mailFrom,
})
for k,v in allSubstitutions.items():
allSubstitutions[k] = v.decode('utf8')
allSubstitutions[k+"_forhtml"] = cgi.escape(v.decode('utf8'))
allSubstitutions[k+"_forurl"] = urllib.quote(v)
mailString = open(mailTemplateFile).read().decode('utf8') % allSubstitutions
parsedFrom = email.utils.parseaddr(mailFrom)[1]
parsedTo = email.utils.parseaddr(mailTo)[1]
if parsedFrom == '' or parsedTo == '':
logger.info("Couldn't parse from / to address %s / %s", mailFrom, mailTo)
raise EmailAddressException()
mailServer = sydent.cfg.get('email', 'email.smtphost')
mailPort = sydent.cfg.get('email', 'email.smtpport')
mailUsername = sydent.cfg.get('email', 'email.smtpusername')
mailPassword = sydent.cfg.get('email', 'email.smtppassword')
mailTLSMode = sydent.cfg.get('email', 'email.tlsmode')
logger.info("Sending mail to %s with mail server: %s" % (mailTo, mailServer,))
try:
if mailTLSMode == 'SSL' or mailTLSMode == 'TLS':
smtp = smtplib.SMTP_SSL(mailServer, mailPort, myHostname)
elif mailTLSMode == 'STARTTLS':
smtp = smtplib.SMTP(mailServer, mailPort, myHostname)
smtp.starttls()
else:
smtp = smtplib.SMTP(mailServer, mailPort, myHostname)
if mailUsername != '':
smtp.login(mailUsername, mailPassword)
# We're using the parsing above to do basic validation, but instead of
# failing it may munge the address it returns. So we should *not* use
# that parsed address, as it may not match any validation done
# elsewhere.
smtp.sendmail(mailFrom, mailTo, mailString.encode('utf-8'))
smtp.quit()
except Exception as origException:
twisted.python.log.err()
ese = EmailSendException()
ese.cause = origException
raise ese
```
|