Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7223: Support sending to smtp server without authentication. #7225

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions sirepo/smtp.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
"""SMTP connection to send emails

:copyright: Copyright (c) 2018-2019 RadiaSoft LLC. All Rights Reserved.
:copyright: Copyright (c) 2018-2024 RadiaSoft LLC. All Rights Reserved.
:license: http://www.apache.org/licenses/LICENSE-2.0.html
"""

from __future__ import absolute_import, division, print_function
from pykern.pkdebug import pkdp, pkdlog
from pykern import pkconfig
from pykern.pkcollections import PKDict
Expand Down Expand Up @@ -68,11 +66,12 @@ def _send_directly(msg):
s.send_message(msg)


def _send_with_auth(msg):
def _send_via_relay_server(msg):
with smtplib.SMTP(_cfg.server, _cfg.port) as s:
s.starttls()
s.ehlo()
s.login(_cfg.user, _cfg.password)
if _cfg.user and _cfg.password:
s.login(_cfg.user, _cfg.password)
s.send_message(msg)


Expand All @@ -94,14 +93,18 @@ def _init():
_cfg.server = "not " + _DEV_SMTP_SERVER
_SEND = _send_directly
return
_SEND = _send_with_auth
_SEND = _send_via_relay_server
if pkconfig.in_dev_mode():
if _cfg.server is None:
_cfg.server = _DEV_SMTP_SERVER
return
if _cfg.server is None or _cfg.user is None or _cfg.password is None:
if _cfg.server is None:
e-carlin marked this conversation as resolved.
Show resolved Hide resolved
pkconfig.raise_error(
f"server={_cfg.server}, user={_cfg.user}, and password={_cfg.password} must be defined",
f"server={_cfg.server} must be defined",
)
if bool(_cfg.user) != bool(_cfg.password):
pkconfig.raise_error(
f"user={_cfg.user} and password={_cfg.password} must be both set or not"
)


Expand Down