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

Issue with smtp when running rcb alert #11

Closed
beastarman74 opened this issue Jan 20, 2025 · 5 comments
Closed

Issue with smtp when running rcb alert #11

beastarman74 opened this issue Jan 20, 2025 · 5 comments
Labels
bug Something isn't working

Comments

@beastarman74
Copy link

I am getting an error when attempting to test the smtp alert.

I have tried a simple unauthenticated smtp server that doesn't need a username and password.
Plus an smtp relay service that uses authentication and port 587, STARTSSL.

I receive an error stating: ERROR: [SSL: WRONG_VERSION_NUMBER]

EMAIL_HOST=relay.mailbaby.net
EMAIL_PORT=587
EMAIL_HOST_USER=REDACTED
EMAIL_HOST_PASSWORD=REDACTED
EMAIL_SEND_TO=stack-back@REDACTED

Unauthenticated smtp server.
Note: I had to add dummy username and password to the entry as without them it detected as not configured.

EMAIL_HOST=192.168.1.26
EMAIL_PORT=1026
EMAIL_HOST_USER=user
EMAIL_HOST_PASSWORD=pass
EMAIL_SEND_TO=stack-back@REDACTED

rcb alert command output:

/restic-compose-backup # rcb alert
2025-01-20 01:26:41,527 - INFO: Testing alerts
2025-01-20 01:26:41,528 - DEBUG: Getting alert backends
2025-01-20 01:26:41,528 - DEBUG: Alert backend 'smtp' configured: True
2025-01-20 01:26:41,528 - DEBUG: Alert backend 'discord_webhook' configured: False
2025-01-20 01:26:41,528 - INFO: Configured: smtp
2025-01-20 01:26:41,528 - INFO: Connecting to 192.168.1.26 port 1026
2025-01-20 01:26:41,534 - ERROR: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1000)
Traceback (most recent call last):
  File "/restic-compose-backup/restic_compose_backup/alerts/smtp.py", line 48, in send
    server = smtplib.SMTP_SSL(self.host, self.port)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/smtplib.py", line 1022, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/usr/lib/python3.12/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/smtplib.py", line 1029, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ssl.py", line 455, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ssl.py", line 1041, in _create
    self.do_handshake()
  File "/usr/lib/python3.12/ssl.py", line 1319, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1000)
2025-01-20 01:26:41,536 - ERROR: Exception raised when sending alert [smtp]: cannot access local variable 'server' where it is not associated with a value
2025-01-20 01:26:41,536 - ERROR: cannot access local variable 'server' where it is not associated with a value
Traceback (most recent call last):
  File "/restic-compose-backup/restic_compose_backup/alerts/__init__.py", line 20, in send
    instance.send(
  File "/restic-compose-backup/restic_compose_backup/alerts/smtp.py", line 56, in send
    server.close()
    ^^^^^^
UnboundLocalError: cannot access local variable 'server' where it is not associated with a value

/restic-compose-backup #

Please let me know if you need any more information.

@lawndoc
Copy link
Owner

lawndoc commented Jan 20, 2025

Thanks for reporting the issue. I actually haven't tested the SMTP functionality since taking over the repo, so this is probably something else that has broken over time that I missed in the initial release.

I should have some time tonight or tomorrow to investigate.

@lawndoc lawndoc added the bug Something isn't working label Jan 20, 2025
@lawndoc
Copy link
Owner

lawndoc commented Jan 22, 2025

Hi @beastarman74, I was able to reproduce this issue. It seems to just be a problem with port 587 (STARTTLS).

For now, you can specify port 465 (implicit TLS), which is working fine for me.

Image

Image

I'll look into why port 587 isn't working in the mean time.

@lawndoc
Copy link
Owner

lawndoc commented Jan 22, 2025

Aha, it looks like the original code was using smtplib.SMTP_SSL to send the emails.

server = smtplib.SMTP_SSL(self.host, self.port)

According to the smtplib documentation:

SMTP_SSL should be used for situations where SSL is required from the beginning of the connection and using starttls() is not appropriate.

So we'll need to add some code to check which SMTP port is configured and use the appropriate smtplib server class for each one.

@lawndoc
Copy link
Owner

lawndoc commented Jan 22, 2025

It's getting late here for me. This should be a quick fix tomorrow.

@lawndoc
Copy link
Owner

lawndoc commented Jan 24, 2025

This issue has been resolved by #13. Thanks for reporting!

465 SMTPS

└─$ docker compose exec -it backup rcb alert
2025-01-24 02:32:19,391 - INFO: Testing alerts
2025-01-24 02:32:19,391 - INFO: Configured: smtp
2025-01-24 02:32:19,391 - INFO: Connecting to smtp.mailgun.org port 465
2025-01-24 02:32:20,316 - INFO: Email sent

587 SMTP STARTTLS

└─$ docker compose exec -it backup rcb alert
2025-01-24 02:32:55,211 - INFO: Testing alerts
2025-01-24 02:32:55,211 - INFO: Configured: smtp
2025-01-24 02:32:55,211 - INFO: Connecting to smtp.mailgun.org port 587
2025-01-24 02:32:56,471 - INFO: Email sent

25 SMTP

└─$ docker compose exec -it backup rcb alert
2025-01-24 02:33:23,455 - INFO: Testing alerts
2025-01-24 02:33:23,456 - INFO: Configured: smtp
2025-01-24 02:33:23,456 - INFO: Connecting to smtp.mailgun.org port 25
2025-01-24 02:33:24,427 - INFO: Email sent

@lawndoc lawndoc closed this as completed Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants