Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion docs/guide/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,19 @@ Alerts can be tested using the ``rcb alerts`` command.
This will send a test message to all configured alert
backends.

EMAIL_SEND_FROM
~~~~~~~~~~~~~

The email address to send alerts from. If not set, the ``EMAIL_HOST_USER`` will be used as the sender address.

Alerts can be tested using the ``rcb alerts`` command.
This will send a test message to all configured alert
backends.

EMAIL_SEND_TO
~~~~~~~~~~~~~

The email address to send alerts
The email address to send alerts to. Multiple addresses can be separated by a space.

Alerts can be tested using the ``rcb alerts`` command.
This will send a test message to all configured alert
Expand Down
15 changes: 8 additions & 7 deletions src/restic_compose_backup/alerts/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
class SMTPAlert(BaseAlert):
name = "smtp"

def __init__(self, host, port, user, password, to):
def __init__(self, host, port, user, password, from_addr, to_addr):
self.host = host
self.port = port
self.user = user
self.password = password or ""
self.to = to
self.from_addr = from_addr
self.to_addr = to_addr

@classmethod
def create_from_env(cls):
Expand All @@ -25,6 +26,7 @@ def create_from_env(cls):
os.environ.get("EMAIL_PORT"),
os.environ.get("EMAIL_HOST_USER"),
os.environ.get("EMAIL_HOST_PASSWORD"),
(os.environ.get("EMAIL_SEND_FROM") or os.environ.get("EMAIL_HOST_USER")),
(os.environ.get("EMAIL_SEND_TO") or "").split(","),
)
if instance.properly_configured:
Expand All @@ -34,13 +36,12 @@ def create_from_env(cls):

@property
def properly_configured(self) -> bool:
return self.host and self.port and self.user and len(self.to) > 0

return self.host and self.port and self.user and self.from_addr and self.to_addr
def send(self, subject: str = None, body: str = None, alert_type: str = "INFO"):
msg = MIMEText(body)
msg["Subject"] = f"[{alert_type}] {subject}"
msg["From"] = self.user
msg["To"] = ", ".join(self.to)
msg["From"] = self.from_addr
msg["To"] = ", ".join(self.to_addr)

try:
logger.info("Connecting to %s port %s", self.host, self.port)
Expand All @@ -61,7 +62,7 @@ def send(self, subject: str = None, body: str = None, alert_type: str = "INFO"):
return
server.ehlo()
server.login(self.user, self.password)
server.sendmail(self.user, self.to, msg.as_string())
server.sendmail(self.from_addr, self.to_addr, msg.as_string())
logger.info("Email sent")
except Exception as ex:
logger.exception(ex)
Expand Down
1 change: 1 addition & 0 deletions stack-back.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CRON_SCHEDULE=0 2 * * *
# EMAIL_PORT=
# EMAIL_HOST_USER=
# EMAIL_HOST_PASSWORD=
# EMAIL_SEND_FROM=
# EMAIL_SEND_TO=

# DISCORD_WEBHOOK=
Expand Down