From 6a212b5524903c170d420763c9c08a5444745281 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Tue, 19 Oct 2021 17:34:17 -0400 Subject: Initial implementation of native mail sending I'm felling comfortable that "b4 ty" is sufficiently mature at this point to implement sending thank-yous directly. This is only the initial implementation that covers only the very basic parts of git's sendemail configuration options, but this should actually cover 90% of cases if not more. One important caveat -- I moved the "b4 ty -s" flag to be "b4 ty -t" in order to disambiguate it from the capital -S (that actually does the sending). Since "b4 ty" is still marked as an experimental feature, I feel we can do this without much impact. Signed-off-by: Konstantin Ryabitsev --- b4/__init__.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'b4/__init__.py') diff --git a/b4/__init__.py b/b4/__init__.py index f8766d5..adbcf7d 100644 --- a/b4/__init__.py +++ b/b4/__init__.py @@ -130,6 +130,8 @@ DEFAULT_CONFIG = { # git-config for gpg.program, and if that's not set, # we'll use "gpg" and hope for the better 'gpgbin': None, + # When sending mail, use this sendemail identity configuration + 'sendemail-identity': None, } # This is where we store actual config @@ -2480,3 +2482,54 @@ def read_template(tptfile): continue tpt += line return tpt + + +def get_smtp(identity: Optional[str] = None): + import smtplib + if identity: + sconfig = get_config_from_git(rf'sendemail\.{identity}\..*') + sectname = f'sendemail.{identity}' + else: + sconfig = get_config_from_git(rf'sendemail\..*') + sectname = 'sendemail' + if not len(sconfig): + raise smtplib.SMTPException('Unable to find %s settings in any applicable git config' % sectname) + + # Limited support for smtp settings to begin with, but should cover the vast majority of cases + fromaddr = sconfig.get('from') + server = sconfig.get('smtpserver', 'localhost') + port = sconfig.get('smtpserverport', 0) + try: + port = int(port) + except ValueError: + raise smtplib.SMTPException('Invalid smtpport entry in %s' % sectname) + + encryption = sconfig.get('smtpencryption') + # We only authenticate if we have encryption + if encryption: + if encryption in ('tls', 'starttls'): + # We do startssl + smtp = smtplib.SMTP(server, port) + # Introduce ourselves + smtp.ehlo() + # Start encryption + smtp.starttls() + # Introduce ourselves again to get new criteria + smtp.ehlo() + elif encryption in ('ssl', 'smtps'): + # We do TLS from the get-go + smtp = smtplib.SMTP_SSL(server, port) + else: + raise smtplib.SMTPException('Unclear what to do with smtpencryption=%s' % encryption) + + # If we got to this point, we should do authentication. + auser = sconfig.get('smtpuser') + apass = sconfig.get('smtppass') + if auser and apass: + # Let any exceptions bubble up + smtp.login(auser, apass) + else: + # We assume you know what you're doing if you don't need encryption + smtp = smtplib.SMTP(server, port) + + return smtp, fromaddr -- cgit v1.2.3