Unify send_msg, add more type hints
This commit is contained in:
parent
67e6df17fb
commit
5f5b374f84
|
@ -33,7 +33,7 @@ import GnuPG
|
||||||
import os
|
import os
|
||||||
import smtplib
|
import smtplib
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import Tuple
|
from typing import Tuple, List, AnyStr
|
||||||
|
|
||||||
# imports for S/MIME
|
# imports for S/MIME
|
||||||
from M2Crypto import BIO, SMIME, X509
|
from M2Crypto import BIO, SMIME, X509
|
||||||
|
@ -567,26 +567,7 @@ def _get_first_payload(payloads):
|
||||||
return payloads
|
return payloads
|
||||||
|
|
||||||
|
|
||||||
def send_msg(message: str, recipients, fromaddr=None):
|
def send_msg(message: AnyStr, recipients: List[str], 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):
|
|
||||||
"""Send MESSAGE to RECIPIENTS to the mail relay."""
|
"""Send MESSAGE to RECIPIENTS to the mail relay."""
|
||||||
global from_addr
|
global from_addr
|
||||||
|
|
||||||
|
@ -618,14 +599,14 @@ def failover_delivery(message: EmailMessage, recipients):
|
||||||
LOG.debug('Flat text message, adjusting coding')
|
LOG.debug('Flat text message, adjusting coding')
|
||||||
_recode(message)
|
_recode(message)
|
||||||
b = message.as_bytes(policy=SMTPUTF8)
|
b = message.as_bytes(policy=SMTPUTF8)
|
||||||
send_msg_bytes(b, recipients)
|
send_msg(b, recipients)
|
||||||
elif message.get_content_maintype() == 'multipart':
|
elif message.get_content_maintype() == 'multipart':
|
||||||
LOG.debug('Multipart message, adjusting coding of text entities')
|
LOG.debug('Multipart message, adjusting coding of text entities')
|
||||||
for part in message.iter_parts():
|
for part in message.iter_parts():
|
||||||
if part.get_content_maintype() == 'text':
|
if part.get_content_maintype() == 'text':
|
||||||
_recode(part)
|
_recode(part)
|
||||||
b = message.as_bytes(policy=SMTPUTF8)
|
b = message.as_bytes(policy=SMTPUTF8)
|
||||||
send_msg_bytes(b, recipients)
|
send_msg(b, recipients)
|
||||||
else:
|
else:
|
||||||
LOG.warning('No failover strategy, giving up')
|
LOG.warning('No failover strategy, giving up')
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@ class MailEncryptionProxy:
|
||||||
LOG.debug(f"Sending mail via {operation!r}")
|
LOG.debug(f"Sending mail via {operation!r}")
|
||||||
try:
|
try:
|
||||||
new_message = operation.perform(message)
|
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:
|
except EncryptionException:
|
||||||
# If the message can't be encrypted, deliver cleartext.
|
# If the message can't be encrypted, deliver cleartext.
|
||||||
LOG.exception('Unable to encrypt message, delivering in cleartext')
|
LOG.exception('Unable to encrypt message, delivering in cleartext')
|
||||||
|
@ -82,7 +82,7 @@ class MailEncryptionProxy:
|
||||||
def _send_unencrypted(self, operation, message, envelope):
|
def _send_unencrypted(self, operation, message, envelope):
|
||||||
keep = KeepIntact(operation.recipients())
|
keep = KeepIntact(operation.recipients())
|
||||||
new_message = keep.perform(message)
|
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:
|
def _beginning(self, e: Envelope) -> bytes:
|
||||||
double_eol_pos = e.original_content.find(DOUBLE_EOL_BYTES)
|
double_eol_pos = e.original_content.find(DOUBLE_EOL_BYTES)
|
||||||
|
|
Loading…
Reference in New Issue