diff --git a/CHANGELOG.md b/CHANGELOG.md index c565d930c..e62566566 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- feature: the icloud email username is now included in the email about 2sa authentication failing, for when an installation is configured for multiple icloud accounts. + ## 1.27.2 (2025-03-29) - fix: dates prior 1970 do not work on non linux [#1045](https://github.com/icloud-photos-downloader/icloud_photos_downloader/issues/1045) diff --git a/src/icloudpd/base.py b/src/icloudpd/base.py index 281513fab..f59173294 100644 --- a/src/icloudpd/base.py +++ b/src/icloudpd/base.py @@ -1230,6 +1230,7 @@ def core( if smtp_username is not None or notification_email is not None: send_2sa_notification( logger, + username, smtp_username, smtp_password, smtp_host, diff --git a/src/icloudpd/email_notifications.py b/src/icloudpd/email_notifications.py index 8f716a04a..4ee5263b8 100644 --- a/src/icloudpd/email_notifications.py +++ b/src/icloudpd/email_notifications.py @@ -8,6 +8,7 @@ def send_2sa_notification( logger: logging.Logger, + username: str, smtp_email: Optional[str], smtp_password: Optional[str], smtp_host: str, @@ -38,12 +39,15 @@ def send_2sa_notification( subj = "icloud_photos_downloader: Two step authentication has expired" date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M") - message_text = """Hello, + message_text = f"""Hello, -Two-step authentication has expired for the icloud_photos_downloader script. +{username}'s two-step authentication has expired for the icloud_photos_downloader script. Please log in to your server and run the script manually to update two-step authentication.""" - msg = f"From: {from_addr}\n" + f"To: {to_addr}\nSubject: {subj}\nDate: {date}\n\n{message_text}" + msg = ( + f"From: {from_addr}\n" + + f"To: {to_addr}\nSubject: {subj}\nDate: {date}\n\n{message_text}" + ) smtp.sendmail(from_addr, to_addr, msg) smtp.quit() diff --git a/tests/test_email_notifications.py b/tests/test_email_notifications.py index 268ba371c..eec7b654c 100644 --- a/tests/test_email_notifications.py +++ b/tests/test_email_notifications.py @@ -34,7 +34,9 @@ def test_2sa_required_email_notification(self) -> None: with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")): with patch("smtplib.SMTP") as smtp: # Pass fixed client ID via environment variable - runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}) + runner = CliRunner( + env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"} + ) result = runner.invoke( main, [ @@ -59,7 +61,9 @@ def test_2sa_required_email_notification(self) -> None: smtp_instance = smtp() smtp_instance.connect.assert_called_once() smtp_instance.starttls.assert_called_once() - smtp_instance.login.assert_called_once_with("jdoe+smtp@gmail.com", "password1") + smtp_instance.login.assert_called_once_with( + "jdoe+smtp@gmail.com", "password1" + ) smtp_instance.sendmail.assert_called_once_with( "iCloud Photos Downloader ", "jdoe+notifications@gmail.com", @@ -67,7 +71,7 @@ def test_2sa_required_email_notification(self) -> None: "To: jdoe+notifications@gmail.com\n" "Subject: icloud_photos_downloader: Two step authentication has expired\n" "Date: 01/01/2018 00:00\n\nHello,\n\n" - "Two-step authentication has expired for the icloud_photos_downloader script.\n" + "jdoe@gmail.com's two-step authentication has expired for the icloud_photos_downloader script.\n" "Please log in to your server and run the script manually to update two-step " "authentication.", ) @@ -84,7 +88,9 @@ def test_2sa_notification_without_smtp_login_and_tls(self) -> None: with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")): with patch("smtplib.SMTP") as smtp: # Pass fixed client ID via environment variable - runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}) + runner = CliRunner( + env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"} + ) result = runner.invoke( main, [ @@ -114,7 +120,7 @@ def test_2sa_notification_without_smtp_login_and_tls(self) -> None: "To: jdoe+notifications@gmail.com\n" "Subject: icloud_photos_downloader: Two step authentication has expired\n" "Date: 01/01/2018 00:00\n\nHello,\n\n" - "Two-step authentication has expired for the icloud_photos_downloader script.\n" + "jdoe@gmail.com's two-step authentication has expired for the icloud_photos_downloader script.\n" "Please log in to your server and run the script manually to update two-step " "authentication.", ) @@ -131,7 +137,9 @@ def test_2sa_required_notification_script(self) -> None: with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")): with patch("subprocess.call") as subprocess_patched: # Pass fixed client ID via environment variable - runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}) + runner = CliRunner( + env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"} + ) result = runner.invoke( main, [ @@ -163,7 +171,9 @@ def test_2sa_required_email_notification_from(self) -> None: with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")): with patch("smtplib.SMTP") as smtp: # Pass fixed client ID via environment variable - runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}) + runner = CliRunner( + env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"} + ) result = runner.invoke( main, [ @@ -190,7 +200,9 @@ def test_2sa_required_email_notification_from(self) -> None: smtp_instance = smtp() smtp_instance.connect.assert_called_once() smtp_instance.starttls.assert_called_once() - smtp_instance.login.assert_called_once_with("jdoe+smtp@gmail.com", "password1") + smtp_instance.login.assert_called_once_with( + "jdoe+smtp@gmail.com", "password1" + ) smtp_instance.sendmail.assert_called_once_with( "JD ", "JD ", @@ -198,7 +210,7 @@ def test_2sa_required_email_notification_from(self) -> None: "To: JD \n" "Subject: icloud_photos_downloader: Two step authentication has expired\n" "Date: 01/01/2018 00:00\n\nHello,\n\n" - "Two-step authentication has expired for the icloud_photos_downloader script.\n" + "jdoe@gmail.com's two-step authentication has expired for the icloud_photos_downloader script.\n" "Please log in to your server and run the script manually to update two-step " "authentication.", )