Unify send_msg, add more type hints

This commit is contained in:
Piotr F. Mieszkowski 2023-04-08 23:12:25 +02:00
parent 67e6df17fb
commit 5f5b374f84
2 changed files with 6 additions and 25 deletions

View file

@ -33,7 +33,7 @@ import GnuPG
import os
import smtplib
import asyncio
from typing import Tuple
from typing import Tuple, List, AnyStr
# imports for S/MIME
from M2Crypto import BIO, SMIME, X509
@ -567,26 +567,7 @@ def _get_first_payload(payloads):
return payloads
def send_msg(message: str, recipients, fromaddr=None):
"""Send MESSAGE to RECIPIENTS to the mail relay."""
global from_addr
if fromaddr is not None:
from_addr = fromaddr
recipients = [_f for _f in recipients if _f]
if recipients:
LOG.info(f"Sending email to: {recipients!r}")
relay = conf.relay_params()
smtp = smtplib.SMTP(relay[0], relay[1])
if conf.flag_enabled('relay', 'starttls'):
smtp.starttls()
smtp.sendmail(from_addr, recipients, message)
else:
LOG.info("No recipient found")
def send_msg_bytes(message: bytes, recipients, fromaddr=None):
def send_msg(message: AnyStr, recipients: List[str], fromaddr=None):
"""Send MESSAGE to RECIPIENTS to the mail relay."""
global from_addr
@ -618,14 +599,14 @@ def failover_delivery(message: EmailMessage, recipients):
LOG.debug('Flat text message, adjusting coding')
_recode(message)
b = message.as_bytes(policy=SMTPUTF8)
send_msg_bytes(b, recipients)
send_msg(b, recipients)
elif message.get_content_maintype() == 'multipart':
LOG.debug('Multipart message, adjusting coding of text entities')
for part in message.iter_parts():
if part.get_content_maintype() == 'text':
_recode(part)
b = message.as_bytes(policy=SMTPUTF8)
send_msg_bytes(b, recipients)
send_msg(b, recipients)
else:
LOG.warning('No failover strategy, giving up')

View file

@ -61,7 +61,7 @@ class MailEncryptionProxy:
LOG.debug(f"Sending mail via {operation!r}")
try:
new_message = operation.perform(message)
gate.send_msg_bytes(new_message, operation.recipients(), envelope.mail_from)
gate.send_msg(new_message, operation.recipients(), envelope.mail_from)
except EncryptionException:
# If the message can't be encrypted, deliver cleartext.
LOG.exception('Unable to encrypt message, delivering in cleartext')
@ -82,7 +82,7 @@ class MailEncryptionProxy:
def _send_unencrypted(self, operation, message, envelope):
keep = KeepIntact(operation.recipients())
new_message = keep.perform(message)
gate.send_msg_bytes(new_message, operation.recipients(), envelope.mail_from)
gate.send_msg(new_message, operation.recipients(), envelope.mail_from)
def _beginning(self, e: Envelope) -> bytes:
double_eol_pos = e.original_content.find(DOUBLE_EOL_BYTES)