mirror of
https://github.com/simple-login/app.git
synced 2025-09-05 22:24:43 +08:00
Allow to have several postfix hosts
This commit is contained in:
parent
4e9b2f5995
commit
3c9b7582da
2 changed files with 30 additions and 11 deletions
|
@ -100,6 +100,7 @@ class MailSender:
|
|||
def __init__(self):
|
||||
self._pool: Optional[ThreadPoolExecutor] = None
|
||||
self._store_emails = False
|
||||
self._randomize_smtp_hosts = True
|
||||
self._emails_sent: List[SendRequest] = []
|
||||
|
||||
def store_emails_instead_of_sending(self, store_emails: bool = True):
|
||||
|
@ -146,7 +147,8 @@ class MailSender:
|
|||
|
||||
def _send_to_smtp(self, send_request: SendRequest, retries: int) -> bool:
|
||||
servers_to_try = config.POSTFIX_SERVERS.copy()
|
||||
random.shuffle(servers_to_try)
|
||||
if self._randomize_smtp_hosts:
|
||||
random.shuffle(servers_to_try)
|
||||
if config.POSTFIX_BACKUP_SERVERS:
|
||||
servers_to_try.extend(config.POSTFIX_BACKUP_SERVERS)
|
||||
servers_tried = 0
|
||||
|
@ -177,22 +179,22 @@ class MailSender:
|
|||
return self._send_to_smtp(send_request, retries - 1)
|
||||
else:
|
||||
if send_request.ignore_smtp_errors:
|
||||
LOG.e("Ignore smtp error")
|
||||
LOG.w("Ignore smtp error and skip saving to fs email")
|
||||
return False
|
||||
LOG.e(
|
||||
f"Could not send message to smtp server {config.POSTFIX_SERVERS}:{config.POSTFIX_PORT}"
|
||||
)
|
||||
if config.SAVE_UNSENT_DIR:
|
||||
send_request.save_request_to_unsent_dir()
|
||||
return False
|
||||
|
||||
def __send_to_server(self, server_host: str, send_request: SendRequest):
|
||||
start = time.time()
|
||||
with SMTP(
|
||||
server_host,
|
||||
config.POSTFIX_PORT,
|
||||
timeout=config.POSTFIX_TIMEOUT,
|
||||
) as smtp:
|
||||
server_split = server_host.split(":")
|
||||
if len(server_split) == 1:
|
||||
server_host = server_split[0]
|
||||
server_port = config.POSTFIX_PORT
|
||||
else:
|
||||
server_host = server_split[0]
|
||||
server_port = server_split[1]
|
||||
with SMTP(server_host, server_port, timeout=config.POSTFIX_TIMEOUT) as smtp:
|
||||
if config.POSTFIX_SUBMISSION_TLS:
|
||||
smtp.starttls()
|
||||
|
||||
|
@ -219,7 +221,7 @@ class MailSender:
|
|||
send_request.mail_options,
|
||||
send_request.rcpt_options,
|
||||
)
|
||||
return True
|
||||
return True
|
||||
|
||||
|
||||
mail_sender = MailSender()
|
||||
|
|
|
@ -109,6 +109,7 @@ def test_mail_sender_save_unsent_to_disk(server_fn):
|
|||
config.POSTFIX_SERVERS = ["localhost"]
|
||||
config.NOT_SEND_EMAIL = False
|
||||
config.POSTFIX_SUBMISSION_TLS = False
|
||||
config.POSTFIX_TIMEOUT = 6000
|
||||
config.POSTFIX_PORT = server_fn()
|
||||
try:
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
|
@ -126,6 +127,22 @@ def test_mail_sender_save_unsent_to_disk(server_fn):
|
|||
config.NOT_SEND_EMAIL = True
|
||||
|
||||
|
||||
def test_mail_sender_try_several_servers():
|
||||
port_closed = closed_dummy_server()
|
||||
port_ok = smtp_response_server("250 Ok")()
|
||||
original_postfix_server = config.POSTFIX_SERVERS
|
||||
config.POSTFIX_SERVERS = [f"localhost:{port_closed}", f"localhost:{port_ok}"]
|
||||
config.NOT_SEND_EMAIL = False
|
||||
config.POSTFIX_SUBMISSION_TLS = False
|
||||
send_request = create_dummy_send_request()
|
||||
mail_sender._randomize_smtp_hosts = False
|
||||
try:
|
||||
assert mail_sender.send(send_request, 0)
|
||||
finally:
|
||||
config.POSTFIX_SERVERS = original_postfix_server
|
||||
config.NOT_SEND_EMAIL = True
|
||||
|
||||
|
||||
@mail_sender.store_emails_test_decorator
|
||||
def test_send_unsent_email_from_fs():
|
||||
original_postfix_server = config.POSTFIX_SERVERS
|
||||
|
|
Loading…
Add table
Reference in a new issue