diff --git a/CHANGELOG.md b/CHANGELOG.md index a1a31ad16..9ccecad81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/icloudpd/base.py b/icloudpd/base.py index dbf9d7dab..2b2fba8d9 100755 --- a/icloudpd/base.py +++ b/icloudpd/base.py @@ -171,6 +171,12 @@ "Default: SMTP username", metavar="", ) +@click.option( + "--notification-email-from", + help="Email address from which you would like to receive email notifications. " + "Default: SMTP username or notification-email", + metavar="", +) @click.option( "--notification-script", type=click.Path(), @@ -234,6 +240,7 @@ def main( smtp_port, smtp_no_tls, notification_email, + notification_email_from, log_level, no_progress_bar, notification_script, @@ -290,6 +297,7 @@ def main( smtp_port, smtp_no_tls, notification_email, + notification_email_from, ) sys.exit(1) diff --git a/icloudpd/email_notifications.py b/icloudpd/email_notifications.py index 9eaac6201..6a53a67e8 100644 --- a/icloudpd/email_notifications.py +++ b/icloudpd/email_notifications.py @@ -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) @@ -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) diff --git a/tests/test_email_notifications.py b/tests/test_email_notifications.py index bf11590c9..ad41f9696 100644 --- a/tests/test_email_notifications.py +++ b/tests/test_email_notifications.py @@ -51,7 +51,7 @@ def test_2sa_required_email_notification(self): "jdoe+smtp@gmail.com", "password1" ) smtp_instance.sendmail.assert_called_once_with( - "jdoe+smtp@gmail.com", + "iCloud Photos Downloader ", "jdoe+notifications@gmail.com", "From: iCloud Photos Downloader \n" "To: jdoe+notifications@gmail.com\n" @@ -98,7 +98,7 @@ def test_2sa_notification_without_smtp_login_and_tls(self): smtp_instance.sendmail.assert_called_once_with( "jdoe+notifications@gmail.com", "jdoe+notifications@gmail.com", - "From: iCloud Photos Downloader \n" + "From: jdoe+notifications@gmail.com\n" "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" @@ -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", + "jdoe@gmail.com", + "--password", + "password1", + "--smtp-username", + "jdoe+smtp@gmail.com", + "--smtp-password", + "password1", + "--notification-email", + "JD ", + "--notification-email-from", + "JD ", + "-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( + "jdoe+smtp@gmail.com", "password1" + ) + smtp_instance.sendmail.assert_called_once_with( + "JD ", + "JD ", + "From: JD \n" + "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" + "Please log in to your server and run the script manually to update two-step " + "authentication.", + ) +