Deduplicate the 2 mail sending functions
I went with introducing mail_send_with_details and make mail_send a wrapper around it
This commit is contained in:
parent
1eb5d07c09
commit
58089794f9
@ -6,14 +6,13 @@ import osc
|
||||
import osc.core
|
||||
import osc.conf
|
||||
import xml.etree.ElementTree as ET
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
import cgi
|
||||
import email.utils
|
||||
import logging
|
||||
import argparse
|
||||
import sys
|
||||
from collections import namedtuple
|
||||
from osclib.util import mail_send_with_details
|
||||
import email.utils
|
||||
|
||||
# FIXME: compute from apiurl
|
||||
URL="https://build.opensuse.org/project/status/%s?ignore_pending=true&limit_to_fails=true&include_versions=false&format=json"
|
||||
@ -98,25 +97,15 @@ Kind regards,
|
||||
|
||||
|
||||
def SendMail(logger, project, sender, to, fullname, subject, text):
|
||||
msg = MIMEText(text, _charset='UTF-8')
|
||||
msg['Subject'] = subject
|
||||
msg['To'] = email.utils.formataddr((to, fullname))
|
||||
msg['From'] = sender
|
||||
msg['Date'] = email.utils.formatdate()
|
||||
msg['Message-ID'] = email.utils.make_msgid()
|
||||
msg.add_header('Precedence', 'bulk')
|
||||
msg.add_header('X-Mailer', '%s - Failure Notification' % project)
|
||||
logger.info("%s: %s", msg['To'], msg['Subject'])
|
||||
if args.dry:
|
||||
logger.debug(msg.as_string())
|
||||
else:
|
||||
try:
|
||||
s = smtplib.SMTP(args.relay)
|
||||
s.sendmail(msg['From'], {msg['To'], sender }, msg.as_string())
|
||||
s.quit()
|
||||
except:
|
||||
logger.error("Failed to send an email to %s (%s)" % (fullname, to))
|
||||
pass
|
||||
try:
|
||||
xmailer = '{} - Failure Notification'.format(project)
|
||||
to = email.utils.formataddr((to, fullname))
|
||||
mail_send_with_details(sender=sender, to=to,
|
||||
subject=subject, text=text, xmailer=xmailer,
|
||||
relay=args.relay, dry=args.dry)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
logger.error("Failed to send an email to %s (%s)" % (fullname, to))
|
||||
|
||||
def main(args):
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
import logging
|
||||
|
||||
from osc import conf
|
||||
from osclib.conf import Config
|
||||
from osclib.core import entity_email
|
||||
from osclib.core import project_list_prefix
|
||||
from osclib.memoize import memoize
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
@memoize(session=True)
|
||||
def project_list_family(apiurl, project, include_update=False):
|
||||
@ -123,36 +126,47 @@ def project_version(project):
|
||||
|
||||
return 0
|
||||
|
||||
def mail_send(apiurl, project, to, subject, body, from_key='maintainer',
|
||||
followup_to_key='release-list', dry=False):
|
||||
def mail_send_with_details(relay, sender, subject, to, text, xmailer=None, followup_to=None, dry=True):
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
import email.utils
|
||||
import smtplib
|
||||
|
||||
config = Config.get(apiurl, project)
|
||||
msg = MIMEText(body)
|
||||
msg = MIMEText(text, _charset='UTF-8')
|
||||
msg['Subject'] = subject
|
||||
msg['Message-ID'] = email.utils.make_msgid()
|
||||
msg['Date'] = email.utils.formatdate(localtime=1)
|
||||
if from_key is None:
|
||||
msg['From'] = entity_email(apiurl, conf.get_apiurl_usr(apiurl), include_name=True)
|
||||
else:
|
||||
msg['From'] = config['mail-{}'.format(from_key)]
|
||||
if '@' not in to:
|
||||
to = config['mail-{}'.format(to)]
|
||||
msg['From'] = sender
|
||||
msg['To'] = to
|
||||
followup_to = config.get('mail-{}'.format(followup_to_key))
|
||||
if followup_to:
|
||||
msg['Mail-Followup-To'] = followup_to
|
||||
msg['Subject'] = subject
|
||||
|
||||
if xmailer:
|
||||
msg.add_header('X-Mailer', xmailer)
|
||||
msg.add_header('Precedence', 'bulk')
|
||||
if dry:
|
||||
print(msg.as_string())
|
||||
logger.debug(msg.as_string())
|
||||
return
|
||||
|
||||
s = smtplib.SMTP(config.get('mail-relay', 'relay.suse.de'))
|
||||
s.sendmail(msg['From'], [msg['To']], msg.as_string())
|
||||
logger.info("%s: %s", msg['To'], msg['Subject'])
|
||||
s = smtplib.SMTP(relay)
|
||||
s.sendmail(msg['From'], {msg['To'], sender }, msg.as_string())
|
||||
s.quit()
|
||||
|
||||
def mail_send(apiurl, project, to, subject, body, from_key='maintainer',
|
||||
followup_to_key='release-list', dry=False):
|
||||
|
||||
config = Config.get(apiurl, project)
|
||||
if from_key is None:
|
||||
sender = entity_email(apiurl, conf.get_apiurl_usr(apiurl), include_name=True)
|
||||
else:
|
||||
sender = config['mail-{}'.format(from_key)]
|
||||
|
||||
if '@' not in to:
|
||||
to = config['mail-{}'.format(to)]
|
||||
|
||||
followup_to = config.get('mail-{}'.format(followup_to_key))
|
||||
relay = config.get('mail-relay', 'relay.suse.de')
|
||||
|
||||
mail_send_with_details(text=body, subject=subject, relay=relay, sender=sender,
|
||||
followup_to=followup_to, to=to, dry=dry)
|
||||
|
||||
def sha1_short(data):
|
||||
import hashlib
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user