diff --git a/send_email.py b/send_email.py
index c6236fd..197a135 100644
--- a/send_email.py
+++ b/send_email.py
@@ -4,24 +4,42 @@
import base64
import mimetypes
+import settings
+
def send_email(sendgrid_api_key, from_email, to_emails, subject, content, files):
+ # Read the HTML template
+ with open(settings.template_path, 'r', encoding='utf-8') as f:
+ html_template = f.read()
+
+ # Replace the {{logo_url}} tag with the actual URL
+ html_content = html_template.replace('{{logo_url}}', settings.logo_url)
+
+ # Handle line breaks and escaped sequences
+ content = content.replace('\\\\n', 'TEMP_PLACEHOLDER') # Replace \\n with a placeholder
+ content = content.replace('\\n', '
') # Replace newline with
+ content = content.replace('TEMP_PLACEHOLDER', '\\\\n') # Replace placeholder back with \\n
+
+ # Replace the {{content}} tag with the processed content
+ html_content = html_content.replace('{{content}}', content)
+
mail = Mail(
from_email=from_email,
subject=subject,
- plain_text_content=content
+ html_content=html_content # Using the modified HTML content
)
- # Create a Personalization object and add all the recipients to it
- personalization = Personalization()
+ # Create a separate Personalization object for each recipient
for to_email in to_emails:
+ personalization = Personalization()
+
# Ensure to_email is an instance of Email
if isinstance(to_email, str):
to_email = Email(to_email)
personalization.add_to(to_email)
- # Add the Personalization object to the Mail object
- mail.add_personalization(personalization)
+ # Add the Personalization object to the Mail object
+ mail.add_personalization(personalization)
# Handle multiple attachments
for file in files:
@@ -43,7 +61,11 @@ def send_email(sendgrid_api_key, from_email, to_emails, subject, content, files)
client = SendGridAPIClient(sendgrid_api_key)
try:
response = client.send(mail)
+ # Print success status
+ print(f"Email sent successfully to {len(to_emails)} recipient(s).")
except BadRequestsError as e:
+ # Print failure status
+ print(f"Email send failed.")
print(e.body)
raise
diff --git a/settings.example.py b/settings.example.py
index 87891c3..6b988f4 100644
--- a/settings.example.py
+++ b/settings.example.py
@@ -17,6 +17,7 @@
to_emails = ["recipient1@example.com", "recipient2@example.com"]
subject = "Summary Report for {{time_period}}"
content = "Please find attached the latest summary report for {{time_period}}."
+template_path = "template.html"
# Scheduler settings
schedule_string = "0 0 * * *" # Daily at midnight
diff --git a/settings.py b/settings.py
index f82786e..5eef6c8 100644
--- a/settings.py
+++ b/settings.py
@@ -16,6 +16,7 @@
to_emails = os.environ.get('TO_EMAILS', "recipient1@example.com,recipient2@example.com").split(',') # Converts "recipient1@example.com,recipient2@example.com" to ['recipient1@example.com', 'recipient2@example.com']
subject = os.environ.get('SUBJECT', "Summary Report for {{time_period}}")
content = os.environ.get('CONTENT', "Please find attached the latest summary report for {{time_period}}.")
+template_path = os.environ.get('TEMPLATE_PATH', "template.html")
# Scheduler settings
schedule_string = os.environ.get('SCHEDULE_STRING', "0 0 * * *") # Daily at midnight
diff --git a/template.html b/template.html
new file mode 100644
index 0000000..6b40d37
--- /dev/null
+++ b/template.html
@@ -0,0 +1,90 @@
+
+
+
+ |
+
+
+
|
++ |