Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion environment_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export FUNCTIONAL_TESTS_API_HOST=''
export MMG_INBOUND_SMS_USERNAME= # 'username' value for Development config in notifications-api
export MMG_INBOUND_SMS_AUTH='' # 'testkey' value for Development config in notifications-api
export SERVICE_API_KEY=''
export SEND_FILE_BY_EMAIL_TEMPLATE_ID=''
export SEND_SINGLE_FILE_BY_EMAIL_TEMPLATE_ID=''
export SEND_TWO_FILES_BY_EMAIL_TEMPLATE_ID=''
Binary file added second_file_to_send_by_email.docx
Binary file not shown.
3 changes: 2 additions & 1 deletion send_file_by_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from notifications_python_client.notifications import NotificationsAPIClient
from notifications_python_client import prepare_upload

# Before this will work, you need to enable "Send files by email", on your service, under "settings".

def send_file_by_email():
if len(sys.argv) == 1:
Expand All @@ -15,7 +16,7 @@ def send_file_by_email():
email_address= sys.argv[1]
api_key = os.environ["SERVICE_API_KEY"]
api_host = os.environ["FUNCTIONAL_TESTS_API_HOST"]
send_file_by_email_template_id = os.environ["SEND_FILE_BY_EMAIL_TEMPLATE_ID"]
send_file_by_email_template_id = os.environ["SEND_SINGLE_FILE_BY_EMAIL_TEMPLATE_ID"]

notifications_client = NotificationsAPIClient(api_key, base_url=api_host)

Expand Down
52 changes: 52 additions & 0 deletions send_multiple_files_by_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3

import os
Comment thread
tombye marked this conversation as resolved.
import sys

from notifications_python_client.notifications import NotificationsAPIClient
from notifications_python_client import prepare_upload

# Before this will work, you need to enable "Send files by email", on your service, under "settings".

def send_multiple_files_by_email():
if len(sys.argv) < 4:
print('Error: Incorrect number of arguments')
print('Usage: python send_multiple_files_by_email.py email@example.com file1 file2')
sys.exit(1)

email_address = sys.argv[1]
file_1 = sys.argv[2]
file_2 = sys.argv[3]
api_key = os.environ["SERVICE_API_KEY"]
api_host = os.environ["FUNCTIONAL_TESTS_API_HOST"]
send_file_by_email_template_id = os.environ["SEND_TWO_FILES_BY_EMAIL_TEMPLATE_ID"]

notifications_client = NotificationsAPIClient(api_key, base_url=api_host)

files_to_send = [file_1, file_2]

personalisation_data = {}

for index, file_path in enumerate(files_to_send, start=1):
print(file_path)
if os.path.exists(file_path):
with open(file_path, 'rb') as f:
# Creates keys like "link_to_file_1", "link_to_file_2" which need to be in the template.
placeholder_key = f"link_to_file_{index}"
personalisation_data[placeholder_key] = prepare_upload(f)
else:
print(f"Warning: {file_path} not found. Skipping.")

if personalisation_data:
response = notifications_client.send_email_notification(
email_address=email_address,
template_id=send_file_by_email_template_id,
personalisation=personalisation_data
)
print(response)
else:
print("Error: No files were successfully prepared. Email not sent.")


if __name__ == "__main__":
send_multiple_files_by_email()