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:
Stephan Kulow 2019-08-29 16:06:55 +02:00
parent 1eb5d07c09
commit 58089794f9
2 changed files with 44 additions and 41 deletions

View File

@ -6,14 +6,13 @@ import osc
import osc.core import osc.core
import osc.conf import osc.conf
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import smtplib
from email.mime.text import MIMEText
import cgi import cgi
import email.utils
import logging import logging
import argparse import argparse
import sys import sys
from collections import namedtuple from collections import namedtuple
from osclib.util import mail_send_with_details
import email.utils
# FIXME: compute from apiurl # FIXME: compute from apiurl
URL="https://build.opensuse.org/project/status/%s?ignore_pending=true&limit_to_fails=true&include_versions=false&format=json" 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): def SendMail(logger, project, sender, to, fullname, subject, text):
msg = MIMEText(text, _charset='UTF-8') try:
msg['Subject'] = subject xmailer = '{} - Failure Notification'.format(project)
msg['To'] = email.utils.formataddr((to, fullname)) to = email.utils.formataddr((to, fullname))
msg['From'] = sender mail_send_with_details(sender=sender, to=to,
msg['Date'] = email.utils.formatdate() subject=subject, text=text, xmailer=xmailer,
msg['Message-ID'] = email.utils.make_msgid() relay=args.relay, dry=args.dry)
msg.add_header('Precedence', 'bulk') except Exception as e:
msg.add_header('X-Mailer', '%s - Failure Notification' % project) print(e)
logger.info("%s: %s", msg['To'], msg['Subject']) logger.error("Failed to send an email to %s (%s)" % (fullname, to))
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
def main(args): def main(args):

View File

@ -1,9 +1,12 @@
import logging
from osc import conf from osc import conf
from osclib.conf import Config from osclib.conf import Config
from osclib.core import entity_email from osclib.core import entity_email
from osclib.core import project_list_prefix from osclib.core import project_list_prefix
from osclib.memoize import memoize from osclib.memoize import memoize
logger = logging.getLogger()
@memoize(session=True) @memoize(session=True)
def project_list_family(apiurl, project, include_update=False): def project_list_family(apiurl, project, include_update=False):
@ -123,36 +126,47 @@ def project_version(project):
return 0 return 0
def mail_send(apiurl, project, to, subject, body, from_key='maintainer', def mail_send_with_details(relay, sender, subject, to, text, xmailer=None, followup_to=None, dry=True):
followup_to_key='release-list', dry=False): import smtplib
from email.mime.text import MIMEText from email.mime.text import MIMEText
import email.utils import email.utils
import smtplib msg = MIMEText(text, _charset='UTF-8')
msg['Subject'] = subject
config = Config.get(apiurl, project)
msg = MIMEText(body)
msg['Message-ID'] = email.utils.make_msgid() msg['Message-ID'] = email.utils.make_msgid()
msg['Date'] = email.utils.formatdate(localtime=1) msg['Date'] = email.utils.formatdate(localtime=1)
if from_key is None: msg['From'] = sender
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['To'] = to msg['To'] = to
followup_to = config.get('mail-{}'.format(followup_to_key))
if followup_to: if followup_to:
msg['Mail-Followup-To'] = 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: if dry:
print(msg.as_string()) logger.debug(msg.as_string())
return return
logger.info("%s: %s", msg['To'], msg['Subject'])
s = smtplib.SMTP(config.get('mail-relay', 'relay.suse.de')) s = smtplib.SMTP(relay)
s.sendmail(msg['From'], [msg['To']], msg.as_string()) s.sendmail(msg['From'], {msg['To'], sender }, msg.as_string())
s.quit() 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): def sha1_short(data):
import hashlib import hashlib