Skip to content

Commit

Permalink
initialization of mail server connection just once per session
Browse files Browse the repository at this point in the history
  • Loading branch information
mtill committed Dec 9, 2013
1 parent da58c72 commit 581b81e
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions MailWebsiteChanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
uriAttributes = [['//img[@src]', 'src'], ['//a[@href]', 'href']]
cmdscheme = 'cmd://'

mailsession = None


def toAbsoluteURIs(trees, baseuri):
for tree in trees:
Expand Down Expand Up @@ -175,6 +177,8 @@ def genFeedItem(subject, content, link, change):


def sendmail(subject, content, sendAsHtml, link):
global mailsession

if sendAsHtml:
baseurl = None
if link != None:
Expand All @@ -190,13 +194,14 @@ def sendmail(subject, content, sendAsHtml, link):
mail['To'] = config.receiver
mail['Subject'] = Header(subject, defaultEncoding)

s = smtplib.SMTP(config.smtphost, config.smtpport)
if config.useTLS:
s.ehlo()
s.starttls()
s.login(config.smtpusername, config.smtppwd)
s.sendmail(config.sender, config.receiver, mail.as_string())
s.quit()
if mailsession is None:
mailsession = smtplib.SMTP(config.smtphost, config.smtpport)
if config.useTLS:
mailsession.ehlo()
mailsession.starttls()
mailsession.login(config.smtpusername, config.smtppwd)

mailsession.sendmail(config.sender, config.receiver, mail.as_string())


def getFileContents(shortname):
Expand Down Expand Up @@ -309,3 +314,6 @@ def pollWebsites():
if config.receiver != '':
sendmail('[MailWebsiteChanges] Something went wrong ...', msg, False, None)

if mailsession:
mailsession.quit()

0 comments on commit 581b81e

Please sign in to comment.