Markdown
Markdown formatter returns message blocks formatted using classic Markdown syntax. This format is widely adopted by messenger apps and wikis.
Also, it can be used to format messages for emails.
Example
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from markdown import markdown
from smtplib import SMTP
from typing import List
def send_email_markdown_message(
smtp_host: str,
smtp_port: int,
smtp_user: str,
smtp_password: str,
sender_name: str,
sender_email: str,
receiver_emails: List[str],
message_subject: str,
message_blocks: List[str],
):
"""
Basic utility function to send email messages from SnowKill examples
"""
multipart_msg = MIMEMultipart("alternative")
multipart_msg["Subject"] = message_subject
multipart_msg["From"] = f"{sender_name} <{sender_email}>"
multipart_msg["To"] = ",".join(receiver_emails)
message_markdown_content = "\n\n".join(message_blocks)
message_html_content = email_template.replace("{{ content }}", markdown(message_markdown_content, extensions=["fenced_code"]))
multipart_msg.attach(MIMEText(message_markdown_content, "plain"))
multipart_msg.attach(MIMEText(message_html_content, "html"))
with SMTP(host=smtp_host, port=smtp_port) as server:
server.login(smtp_user, smtp_password)
response = server.send_message(multipart_msg, sender_email, receiver_emails)
return response
...
snowkill_formatter = MarkdownFormatter(getenv("SNOWSIGHT_BASE_URL"))
# Send notification for each new check result
for r in check_results:
message_blocks = snowkill_formatter.format(r)
response = send_email_markdown_message(
smtp_host=getenv("SMTP_HOST"),
smtp_port=int(getenv("SMTP_PORT")),
smtp_user=getenv("SMTP_USER"),
smtp_password=getenv("SMTP_PASSWORD"),
sender_name="Snowflake Monitor",
sender_email=getenv("SENDER_EMAIL"),
receiver_emails=[getenv("RECEIVER_EMAIL")],
message_subject=message_blocks[0][3:],
message_blocks=message_blocks,
)
Last updated