Move sendmail function out of class since it doesn't need any data from it an it was a static method anyway
This commit is contained in:
@@ -24,4 +24,5 @@
|
|||||||
"""
|
"""
|
||||||
Scripts for our redundances bot
|
Scripts for our redundances bot
|
||||||
"""
|
"""
|
||||||
from jogobot.jogobot import output # noqa
|
# noqa needed to prevent pyflakes from warning about unused imports
|
||||||
|
from jogobot.jogobot import ( output, sendmail ) # noqa
|
||||||
|
|||||||
116
jogobot.py
116
jogobot.py
@@ -99,6 +99,64 @@ def pywikibot_output( text, decoder=None, newline=True,
|
|||||||
pywikibot.output = pywikibot_output
|
pywikibot.output = pywikibot_output
|
||||||
|
|
||||||
|
|
||||||
|
def sendmail( Subject, Body, To=None, CC=None, BCC=None,
|
||||||
|
From="JogoBot <tools.jogobot@tools.wmflabs.org>" ):
|
||||||
|
"""
|
||||||
|
Provides a simple wrapper for exim (MTA) on tool labs
|
||||||
|
Params should be formated according related fields in RFC 5322
|
||||||
|
|
||||||
|
@param subject Mail subject
|
||||||
|
@type subject str
|
||||||
|
@param body Mail body as (formated) string
|
||||||
|
@type body unicode-str
|
||||||
|
@param to Mail-Recipiends (comma-separeded)
|
||||||
|
@type str
|
||||||
|
@param from Mail-Sender
|
||||||
|
@type str
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Create mail body as MIME-Object
|
||||||
|
msg = MIMEText(Body)
|
||||||
|
|
||||||
|
# Set up mail header
|
||||||
|
msg['Subject'] = Subject
|
||||||
|
|
||||||
|
msg['From'] = From
|
||||||
|
|
||||||
|
if To:
|
||||||
|
msg['To'] = To
|
||||||
|
|
||||||
|
if CC:
|
||||||
|
msg['CC'] = CC
|
||||||
|
|
||||||
|
if BCC:
|
||||||
|
msg['BCC'] = BCC
|
||||||
|
|
||||||
|
msg['Content-Type'] = 'text/plain; charset=utf-8'
|
||||||
|
|
||||||
|
# Make sure we have a recipient
|
||||||
|
if not( To or CC or BCC):
|
||||||
|
raise JogoBotMailError( "No recipient was provided!" )
|
||||||
|
|
||||||
|
# Send the message via exim
|
||||||
|
with Popen( ["/usr/sbin/exim", "-odf", "-i", "-t"],
|
||||||
|
stdin=PIPE, universal_newlines=True) as MTA:
|
||||||
|
MTA.communicate(msg.as_string())
|
||||||
|
|
||||||
|
# Try to get returncode of MTA
|
||||||
|
# Process is not terminated until timeout, set returncode to None
|
||||||
|
try:
|
||||||
|
returncode = MTA.wait(timeout=30)
|
||||||
|
except TimeoutExpired:
|
||||||
|
returncode = None
|
||||||
|
|
||||||
|
# Catch MTA errors
|
||||||
|
if returncode:
|
||||||
|
raise JogoBotMailError( "/usr/sbin/exim terminated with " +
|
||||||
|
"returncode != 0. Returncode was " +
|
||||||
|
str( returncode ) )
|
||||||
|
|
||||||
|
|
||||||
class JogoBot:
|
class JogoBot:
|
||||||
"""
|
"""
|
||||||
Basic bot framework
|
Basic bot framework
|
||||||
@@ -205,64 +263,6 @@ class JogoBot:
|
|||||||
with open(disable_file, 'a'):
|
with open(disable_file, 'a'):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def sendmail( Subject, Body, To=None, CC=None, BCC=None,
|
|
||||||
From="JogoBot <tools.jogobot@tools.wmflabs.org>" ):
|
|
||||||
"""
|
|
||||||
Provides a simple wrapper for exim (MTA) on tool labs
|
|
||||||
Params should be formated according related fields in RFC 5322
|
|
||||||
|
|
||||||
@param subject Mail subject
|
|
||||||
@type subject str
|
|
||||||
@param body Mail body as (formated) string
|
|
||||||
@type body unicode-str
|
|
||||||
@param to Mail-Recipiends (comma-separeded)
|
|
||||||
@type str
|
|
||||||
@param from Mail-Sender
|
|
||||||
@type str
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Create mail body as MIME-Object
|
|
||||||
msg = MIMEText(Body)
|
|
||||||
|
|
||||||
# Set up mail header
|
|
||||||
msg['Subject'] = Subject
|
|
||||||
|
|
||||||
msg['From'] = From
|
|
||||||
|
|
||||||
if To:
|
|
||||||
msg['To'] = To
|
|
||||||
|
|
||||||
if CC:
|
|
||||||
msg['CC'] = CC
|
|
||||||
|
|
||||||
if BCC:
|
|
||||||
msg['BCC'] = BCC
|
|
||||||
|
|
||||||
msg['Content-Type'] = 'text/plain; charset=utf-8'
|
|
||||||
|
|
||||||
# Make sure we have a recipient
|
|
||||||
if not( To or CC or BCC):
|
|
||||||
raise JogoBotMailError( "No recipient was provided!" )
|
|
||||||
|
|
||||||
# Send the message via exim
|
|
||||||
with Popen( ["/usr/sbin/exim", "-odf", "-i", "-t"],
|
|
||||||
stdin=PIPE, universal_newlines=True) as MTA:
|
|
||||||
MTA.communicate(msg.as_string())
|
|
||||||
|
|
||||||
# Try to get returncode of MTA
|
|
||||||
# Process is not terminated until timeout, set returncode to None
|
|
||||||
try:
|
|
||||||
returncode = MTA.wait(timeout=30)
|
|
||||||
except TimeoutExpired:
|
|
||||||
returncode = None
|
|
||||||
|
|
||||||
# Catch MTA errors
|
|
||||||
if returncode:
|
|
||||||
raise JogoBotMailError( "/usr/sbin/exim terminated with " +
|
|
||||||
"returncode != 0. Returncode was " +
|
|
||||||
str( returncode ) )
|
|
||||||
|
|
||||||
|
|
||||||
class JogoBotMailError( Exception ):
|
class JogoBotMailError( Exception ):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user