Skip to content

Commit

Permalink
2.2.0 (#9)
Browse files Browse the repository at this point in the history
* Fix personalization

* email status logging

* html email templates
  • Loading branch information
justinh-rahb authored Oct 6, 2023
1 parent 4975438 commit 7e28515
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
32 changes: 27 additions & 5 deletions send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<br>') # Replace newline with <br>
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:
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions settings.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html lang="en-CA">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Realty Shop</title>
<style type="text/css">@media screen and (max-width: 600px){#header_wrapper{padding: 27px 36px !important; font-size: 24px;}#body_content table > tbody > tr > td{padding: 10px !important;}#body_content_inner{font-size: 10px !important;}}</style>
</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" style="background-color: #f7f7f7; padding: 0; text-align: center;" bgcolor="#f7f7f7">
<table width="100%" id="outer_wrapper" style="background-color: #f7f7f7;" bgcolor="#f7f7f7">
<tr>
<td><!-- Deliberately empty to support consistent sizing and layout across multiple email clients. --></td>
<td width="600">
<div id="wrapper" dir="ltr" style="margin: 0 auto; padding: 70px 0; width: 100%; max-width: 600px; -webkit-text-size-adjust: none;" width="100%">
<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
<tr>
<td align="center" valign="top">
<div id="template_header_image">
<p style="margin-top: 0;"><img src="{{logo_url}}" alt="Logo" style="border: none; display: inline-block; font-size: 14px; font-weight: bold; height: auto; outline: none; text-decoration: none; text-transform: capitalize; vertical-align: middle; max-width: 100%; margin-left: 0; margin-right: 0;" border="0"></p> </div>
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="template_container" style="background-color: #fff; border: 1px solid #dedede; box-shadow: 0 1px 4px rgba(0,0,0,.1); border-radius: 3px;" bgcolor="#fff">
<tr>
<td align="center" valign="top">
<!-- Header -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="template_header" style='background-color: #28628a; color: #fff; border-bottom: 0; font-weight: bold; line-height: 100%; vertical-align: middle; font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif; border-radius: 3px 3px 0 0;' bgcolor="#28628a">
<tr>
<td id="header_wrapper" style="padding: 36px 48px; display: block;">
<h1 style='font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif; font-size: 30px; font-weight: 300; line-height: 150%; margin: 0; text-align: left; text-shadow: 0 1px 0 #5381a1; color: #fff; background-color: inherit;' bgcolor="inherit">Redash Report</h1>
</td>
</tr>
</table>
<!-- End Header -->
</td>
</tr>
<tr>
<td align="center" valign="top">
<!-- Body -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="template_body">
<tr>
<td valign="top" id="body_content" style="background-color: #fff;" bgcolor="#fff">
<!-- Content -->
<table border="0" cellpadding="20" cellspacing="0" width="100%">
<tr>
<td valign="top" style="padding: 48px 48px 32px;">
<div id="body_content_inner" style='color: #636363; font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif; font-size: 14px; line-height: 150%; text-align: left;' align="left">

<h3 style='color: #1a1a1c; display: block; font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif; font-size: 16px; font-weight: bold; line-height: 130%; margin: 0 0 18px; text-align: left;'>
{{content}}
</h3>

</div>
</td>
</tr>
</table>
<!-- End Content -->
</td>
</tr>
</table>
<!-- End Body -->
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" valign="top">
<!-- Footer -->
<table border="0" cellpadding="10" cellspacing="0" width="100%" id="template_footer">
<tr>
<td valign="top" style="padding: 0; border-radius: 6px;">
<table border="0" cellpadding="10" cellspacing="0" width="100%">
<tr>
<td colspan="2" valign="middle" id="credit" style='border-radius: 6px; border: 0; color: #8a8a8a; font-family: "Helvetica Neue",Helvetica,Roboto,Arial,sans-serif; font-size: 12px; line-height: 150%; text-align: center; padding: 24px 0;' align="center">
<p>Generated by <a href="https://github.com/RAHB-REALTORS-Association/redash-report" target="_blank" style='color: #8a8a8a; text-decoration: underline;'>Redash Report</a>.</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- End Footer -->
</td>
</tr>
</table>
</div>
</td>
<td><!-- Deliberately empty to support consistent sizing and layout across multiple email clients. --></td>
</tr>
</table>
</body>
</html>

0 comments on commit 7e28515

Please sign in to comment.