Skip to content

Commit e21ea8f

Browse files
authored
feature: the icloud email username is now included in the email about 2sa authentication failing (#1095)
1 parent 225f2ff commit e21ea8f

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- 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.
6+
57
## 1.27.2 (2025-03-29)
68

79
- fix: dates prior 1970 do not work on non linux [#1045](https://github.com/icloud-photos-downloader/icloud_photos_downloader/issues/1045)

src/icloudpd/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ def core(
12301230
if smtp_username is not None or notification_email is not None:
12311231
send_2sa_notification(
12321232
logger,
1233+
username,
12331234
smtp_username,
12341235
smtp_password,
12351236
smtp_host,

src/icloudpd/email_notifications.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
def send_2sa_notification(
1010
logger: logging.Logger,
11+
username: str,
1112
smtp_email: Optional[str],
1213
smtp_password: Optional[str],
1314
smtp_host: str,
@@ -38,12 +39,15 @@ def send_2sa_notification(
3839
subj = "icloud_photos_downloader: Two step authentication has expired"
3940
date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
4041

41-
message_text = """Hello,
42+
message_text = f"""Hello,
4243
43-
Two-step authentication has expired for the icloud_photos_downloader script.
44+
{username}'s two-step authentication has expired for the icloud_photos_downloader script.
4445
Please log in to your server and run the script manually to update two-step authentication."""
4546

46-
msg = f"From: {from_addr}\n" + f"To: {to_addr}\nSubject: {subj}\nDate: {date}\n\n{message_text}"
47+
msg = (
48+
f"From: {from_addr}\n"
49+
+ f"To: {to_addr}\nSubject: {subj}\nDate: {date}\n\n{message_text}"
50+
)
4751

4852
smtp.sendmail(from_addr, to_addr, msg)
4953
smtp.quit()

tests/test_email_notifications.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def test_2sa_required_email_notification(self) -> None:
3434
with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")):
3535
with patch("smtplib.SMTP") as smtp:
3636
# Pass fixed client ID via environment variable
37-
runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"})
37+
runner = CliRunner(
38+
env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}
39+
)
3840
result = runner.invoke(
3941
main,
4042
[
@@ -59,15 +61,17 @@ def test_2sa_required_email_notification(self) -> None:
5961
smtp_instance = smtp()
6062
smtp_instance.connect.assert_called_once()
6163
smtp_instance.starttls.assert_called_once()
62-
smtp_instance.login.assert_called_once_with("jdoe+smtp@gmail.com", "password1")
64+
smtp_instance.login.assert_called_once_with(
65+
"jdoe+smtp@gmail.com", "password1"
66+
)
6367
smtp_instance.sendmail.assert_called_once_with(
6468
"iCloud Photos Downloader <jdoe+smtp@gmail.com>",
6569
"jdoe+notifications@gmail.com",
6670
"From: iCloud Photos Downloader <jdoe+smtp@gmail.com>\n"
6771
"To: jdoe+notifications@gmail.com\n"
6872
"Subject: icloud_photos_downloader: Two step authentication has expired\n"
6973
"Date: 01/01/2018 00:00\n\nHello,\n\n"
70-
"Two-step authentication has expired for the icloud_photos_downloader script.\n"
74+
"jdoe@gmail.com's two-step authentication has expired for the icloud_photos_downloader script.\n"
7175
"Please log in to your server and run the script manually to update two-step "
7276
"authentication.",
7377
)
@@ -84,7 +88,9 @@ def test_2sa_notification_without_smtp_login_and_tls(self) -> None:
8488
with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")):
8589
with patch("smtplib.SMTP") as smtp:
8690
# Pass fixed client ID via environment variable
87-
runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"})
91+
runner = CliRunner(
92+
env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}
93+
)
8894
result = runner.invoke(
8995
main,
9096
[
@@ -114,7 +120,7 @@ def test_2sa_notification_without_smtp_login_and_tls(self) -> None:
114120
"To: jdoe+notifications@gmail.com\n"
115121
"Subject: icloud_photos_downloader: Two step authentication has expired\n"
116122
"Date: 01/01/2018 00:00\n\nHello,\n\n"
117-
"Two-step authentication has expired for the icloud_photos_downloader script.\n"
123+
"jdoe@gmail.com's two-step authentication has expired for the icloud_photos_downloader script.\n"
118124
"Please log in to your server and run the script manually to update two-step "
119125
"authentication.",
120126
)
@@ -131,7 +137,9 @@ def test_2sa_required_notification_script(self) -> None:
131137
with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")):
132138
with patch("subprocess.call") as subprocess_patched:
133139
# Pass fixed client ID via environment variable
134-
runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"})
140+
runner = CliRunner(
141+
env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}
142+
)
135143
result = runner.invoke(
136144
main,
137145
[
@@ -163,7 +171,9 @@ def test_2sa_required_email_notification_from(self) -> None:
163171
with vcr.use_cassette(os.path.join(self.vcr_path, "auth_requires_2fa.yml")):
164172
with patch("smtplib.SMTP") as smtp:
165173
# Pass fixed client ID via environment variable
166-
runner = CliRunner(env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"})
174+
runner = CliRunner(
175+
env={"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"}
176+
)
167177
result = runner.invoke(
168178
main,
169179
[
@@ -190,15 +200,17 @@ def test_2sa_required_email_notification_from(self) -> None:
190200
smtp_instance = smtp()
191201
smtp_instance.connect.assert_called_once()
192202
smtp_instance.starttls.assert_called_once()
193-
smtp_instance.login.assert_called_once_with("jdoe+smtp@gmail.com", "password1")
203+
smtp_instance.login.assert_called_once_with(
204+
"jdoe+smtp@gmail.com", "password1"
205+
)
194206
smtp_instance.sendmail.assert_called_once_with(
195207
"JD <jdoe+notifications+from@gmail.com>",
196208
"JD <jdoe+notifications@gmail.com>",
197209
"From: JD <jdoe+notifications+from@gmail.com>\n"
198210
"To: JD <jdoe+notifications@gmail.com>\n"
199211
"Subject: icloud_photos_downloader: Two step authentication has expired\n"
200212
"Date: 01/01/2018 00:00\n\nHello,\n\n"
201-
"Two-step authentication has expired for the icloud_photos_downloader script.\n"
213+
"jdoe@gmail.com's two-step authentication has expired for the icloud_photos_downloader script.\n"
202214
"Please log in to your server and run the script manually to update two-step "
203215
"authentication.",
204216
)

0 commit comments

Comments
 (0)