cache smtp server and remove POSTFIX_PORT_FORWARD

This commit is contained in:
Son 2021-12-29 16:26:37 +01:00
parent 9e019ae98a
commit f439e39580
3 changed files with 17 additions and 15 deletions

View File

@ -272,7 +272,7 @@ def auth_facebook():
@api_bp.route("/auth/google", methods=["POST"]) @api_bp.route("/auth/google", methods=["POST"])
def auth_google(): def auth_google():
""" """
Authenticate user with Facebook Authenticate user with Google
Input: Input:
google_token: Google access token google_token: Google access token
device: to create an ApiKey associated with this device device: to create an ApiKey associated with this device

View File

@ -117,11 +117,6 @@ POSTFIX_PORT = 25
if "POSTFIX_PORT" in os.environ: if "POSTFIX_PORT" in os.environ:
POSTFIX_PORT = int(os.environ["POSTFIX_PORT"]) POSTFIX_PORT = int(os.environ["POSTFIX_PORT"])
# postfix port to use during the forward phase
POSTFIX_PORT_FORWARD = POSTFIX_PORT
if "POSTFIX_PORT_FORWARD" in os.environ:
POSTFIX_PORT_FORWARD = int(os.environ["POSTFIX_PORT_FORWARD"])
# Use port 587 instead of 25 when sending emails through Postfix # Use port 587 instead of 25 when sending emails through Postfix
# Useful when calling Postfix from an external network # Useful when calling Postfix from an external network
POSTFIX_SUBMISSION_TLS = "POSTFIX_SUBMISSION_TLS" in os.environ POSTFIX_SUBMISSION_TLS = "POSTFIX_SUBMISSION_TLS" in os.environ

View File

@ -19,6 +19,7 @@ import arrow
import dkim import dkim
import re2 as re import re2 as re
import spf import spf
from cachetools import cached, TTLCache
from email_validator import ( from email_validator import (
validate_email, validate_email,
EmailNotValidError, EmailNotValidError,
@ -49,7 +50,6 @@ from app.config import (
ALERT_DIRECTORY_DISABLED_ALIAS_CREATION, ALERT_DIRECTORY_DISABLED_ALIAS_CREATION,
TRANSACTIONAL_BOUNCE_EMAIL, TRANSACTIONAL_BOUNCE_EMAIL,
ALERT_SPF, ALERT_SPF,
POSTFIX_PORT_FORWARD,
TEMP_DIR, TEMP_DIR,
ALIAS_AUTOMATIC_DISABLE, ALIAS_AUTOMATIC_DISABLE,
RSPAMD_SIGN_DKIM, RSPAMD_SIGN_DKIM,
@ -1264,6 +1264,19 @@ def spf_pass(
return True return True
# cache the smtp server for 20 seconds
@cached(cache=TTLCache(maxsize=2, ttl=20))
def get_smtp_server():
LOG.d("get a smtp server")
if POSTFIX_SUBMISSION_TLS:
smtp = SMTP(POSTFIX_SERVER, 587)
smtp.starttls()
else:
smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT)
return smtp
def sl_sendmail( def sl_sendmail(
from_addr, from_addr,
to_addr, to_addr,
@ -1284,14 +1297,8 @@ def sl_sendmail(
return return
try: try:
if POSTFIX_SUBMISSION_TLS: # to avoid creating SMTP server every time
smtp = SMTP(POSTFIX_SERVER, 587) smtp = get_smtp_server()
smtp.starttls()
else:
if is_forward:
smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT_FORWARD)
else:
smtp = SMTP(POSTFIX_SERVER, POSTFIX_PORT)
# smtp.send_message has UnicodeEncodeError # smtp.send_message has UnicodeEncodeError
# encode message raw directly instead # encode message raw directly instead