Skip to content

Commit

Permalink
add from-addr support (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNikiforov authored Feb 12, 2023
1 parent 2062267 commit 1598054
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- feature: add `--notification-email-from` parameter [#496](https://github.com/icloud-photos-downloader/icloud_photos_downloader/issues/496)

## 1.9.0 (2023-02-10)

- fix: replace invalid chars in filenames with '_' [#378](https://github.com/icloud-photos-downloader/icloud_photos_downloader/issues/378)
Expand Down
8 changes: 8 additions & 0 deletions icloudpd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
"Default: SMTP username",
metavar="<notification_email>",
)
@click.option(
"--notification-email-from",
help="Email address from which you would like to receive email notifications. "
"Default: SMTP username or notification-email",
metavar="<notification_email_from>",
)
@click.option(
"--notification-script",
type=click.Path(),
Expand Down Expand Up @@ -234,6 +240,7 @@ def main(
smtp_port,
smtp_no_tls,
notification_email,
notification_email_from,
log_level,
no_progress_bar,
notification_script,
Expand Down Expand Up @@ -290,6 +297,7 @@ def main(
smtp_port,
smtp_no_tls,
notification_email,
notification_email_from,
)
sys.exit(1)

Expand Down
6 changes: 3 additions & 3 deletions icloudpd/email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


def send_2sa_notification(
smtp_email, smtp_password, smtp_host, smtp_port, smtp_no_tls, to_addr
smtp_email, smtp_password, smtp_host, smtp_port, smtp_no_tls, to_addr, from_addr = None
):
"""Send an email notification when 2SA is expired"""
to_addr = to_addr if to_addr else smtp_email
from_addr = smtp_email if smtp_email else to_addr
from_addr = from_addr or (f"iCloud Photos Downloader <{smtp_email}>" if smtp_email else to_addr)
logger = setup_logger()
logger.info("Sending 'two-step expired' notification via email...")
smtp = smtplib.SMTP(smtp_host, smtp_port)
Expand All @@ -34,7 +34,7 @@ def send_2sa_notification(
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: iCloud Photos Downloader <{from_addr}>\n" + \
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)
Expand Down
57 changes: 55 additions & 2 deletions tests/test_email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_2sa_required_email_notification(self):
"[email protected]", "password1"
)
smtp_instance.sendmail.assert_called_once_with(
"[email protected]",
"iCloud Photos Downloader <[email protected]>",
"[email protected]",
"From: iCloud Photos Downloader <[email protected]>\n"
"To: [email protected]\n"
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_2sa_notification_without_smtp_login_and_tls(self):
smtp_instance.sendmail.assert_called_once_with(
"[email protected]",
"[email protected]",
"From: iCloud Photos Downloader <[email protected]>\n"
"From: [email protected]\n"
"To: [email protected]\n"
"Subject: icloud_photos_downloader: Two step authentication has expired\n"
"Date: 01/01/2018 00:00\n\nHello,\n\n"
Expand Down Expand Up @@ -136,3 +136,56 @@ def test_2sa_required_notification_script(self):
print(result.output)
assert result.exit_code == 1
subprocess_patched.assert_called_once_with(["./test_script.sh"])

@freeze_time("2018-01-01")
def test_2sa_required_email_notification_from(self):
base_dir = os.path.normpath(f"tests/fixtures/Photos/{inspect.stack()[0][3]}")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

with vcr.use_cassette("tests/vcr_cassettes/auth_requires_2sa.yml"):
with patch("smtplib.SMTP") as smtp:
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "EC5646DE-9423-11E8-BF21-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--smtp-username",
"[email protected]",
"--smtp-password",
"password1",
"--notification-email",
"JD <[email protected]>",
"--notification-email-from",
"JD <[email protected]>",
"-d",
base_dir,
],
)
print(result.output)
assert result.exit_code == 1
smtp_instance = smtp()
smtp_instance.connect.assert_called_once()
smtp_instance.starttls.assert_called_once()
smtp_instance.login.assert_called_once_with(
"[email protected]", "password1"
)
smtp_instance.sendmail.assert_called_once_with(
"JD <[email protected]>",
"JD <[email protected]>",
"From: JD <[email protected]>\n"
"To: JD <[email protected]>\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"
"Please log in to your server and run the script manually to update two-step "
"authentication.",
)

0 comments on commit 1598054

Please sign in to comment.