Skip to content

Commit

Permalink
mq-sendmail: ability to enqueue message which could not be sent imm…
Browse files Browse the repository at this point in the history
…ediately
  • Loading branch information
FelixSchwarz committed Aug 5, 2024
1 parent ba25886 commit 20b5e06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
19 changes: 11 additions & 8 deletions schwarz/mailqueue/cli/mq_sendmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from schwarz.mailqueue.aliases_parser import _parse_aliases, lookup_adresses
from schwarz.mailqueue.app_helpers import guess_config_path, init_app, init_smtp_mailer
from schwarz.mailqueue.message_handler import InMemoryMsg, MessageHandler
from schwarz.mailqueue.queue_runner import MaildirBackend


__all__ = ['mq_sendmail_main']
Expand Down Expand Up @@ -54,13 +55,6 @@ def mq_sendmail_main(argv=sys.argv, return_rc_code=False):
smtp_sender_domain = platform.uname().node
msg_sender = f'{username}@{smtp_sender_domain}'

cli_options = {
'verbose': verbose,
'quiet' : not verbose,
}
settings = init_app(config_path, options=cli_options)
mailer = init_smtp_mailer(settings)

extra_header_lines = autogenerated_headers(
set_date_header,
set_from_header,
Expand All @@ -70,7 +64,16 @@ def mq_sendmail_main(argv=sys.argv, return_rc_code=False):
)
msg = InMemoryMsg(msg_sender, recipients, extra_header_lines + msg_bytes)

mh = MessageHandler(transports=(mailer,))
cli_options = {
'verbose': verbose,
'quiet' : not verbose,
}
settings = init_app(config_path, options=cli_options)
transports = [init_smtp_mailer(settings)]
queue_dir = settings.get('queue_dir')
if queue_dir:
transports.append(MaildirBackend(queue_dir))
mh = MessageHandler(transports=transports)
send_result = mh.send_message(msg)

if verbose:
Expand Down
4 changes: 3 additions & 1 deletion schwarz/mailqueue/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ def inject_example_message(queue_path, sender=b'[email protected]', recipient=Non
msg_path = move_message(msg_path, target_folder=target_folder, open_file=False)
return MaildirBackedMsg(msg_path)

def create_ini(hostname, port, dir_path):
def create_ini(hostname, port, dir_path, *, queue_dir=None):
config_str = '\n'.join([
'[mqrunner]',
'smtp_hostname = %s' % hostname,
'smtp_port = %d' % port,
])
if queue_dir:
config_str += f'\nqueue_dir = {queue_dir}'
if not dir_path:
return config_str
config_path = os.path.join(dir_path, 'config.ini')
Expand Down
34 changes: 31 additions & 3 deletions tests/mq_sendmail_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import pytest
from dotmap import DotMap
from pymta.test_util import SMTPTestHelper
from schwarz.log_utils import l_

from schwarz.mailqueue.queue_runner import MaildirBackedMsg, assemble_queue_with_new_messages
from schwarz.mailqueue.testutils import create_ini


Expand Down Expand Up @@ -110,9 +112,35 @@ def _create_alias_file(aliases, dir_path) -> str:
return str(aliases_path)


def _mq_sendmail(cli_params, msg, *, ctx):
cfg_dir = str(ctx.tmp_path)
config_path = create_ini(ctx.hostname, ctx.listen_port, dir_path=cfg_dir)
def test_mq_sendmail_with_queueing(ctx):
rfc_msg = _example_message(to='[email protected]')
unused_port = ctx.listen_port + 1
queue_dir = ctx.tmp_path / 'queue'
config_path = create_ini(
ctx.hostname,
port = unused_port,
dir_path = ctx.tmp_path,
queue_dir = queue_dir,
)
_mq_sendmail(['[email protected]'], msg=rfc_msg, config_path=config_path)

received_queue = ctx.mta.get_received_messages()
assert received_queue.qsize() == 0

fs_queue = assemble_queue_with_new_messages(queue_dir, log=l_(None))
assert fs_queue.qsize() == 1
path_queued_msg = fs_queue.get()
msg = MaildirBackedMsg(path_queued_msg)
assert msg.to_addrs == ('[email protected]',)
assert msg.msg_id is None # not added automatically
assert msg.retries == 0
assert msg.msg_bytes == rfc_msg.encode('utf-8')


def _mq_sendmail(cli_params, msg, *, ctx=None, config_path=None):
if config_path is None:
cfg_dir = str(ctx.tmp_path)
config_path = create_ini(ctx.hostname, ctx.listen_port, dir_path=cfg_dir)

cli_params = [f'--config={config_path}'] + cli_params
cmd = [sys.executable, '-m', 'schwarz.mailqueue.mq_sendmail'] + cli_params
Expand Down

0 comments on commit 20b5e06

Please sign in to comment.