Created
August 22, 2023 07:21
-
-
Save iqiancheng/dbcbaf24029dbcc6b406d0b4d1eb129a to your computer and use it in GitHub Desktop.
用Python发送模版邮件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
class ConfigTool: | |
def __init__(self): | |
# Load configurations from environment variables into the global dictionary. | |
self._config_dict = {key: os.environ[key] for key in os.environ} | |
def get(self, key, default=None): | |
"""Retrieve value by key from the configuration dictionary.""" | |
return self._config_dict.get(key, default) | |
# Usage example: | |
config = ConfigTool() | |
db_host = config.get('DB_HOST', 'default_host') # This tries to get the 'DB_HOST' value, and if it doesn't exist, returns 'default_host'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Alice", | |
"task": "Finish the report", | |
"status": "Completed" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
from email.message import EmailMessage | |
import json | |
class EmailNotifier: | |
def __init__(self, smtp_server, smtp_port, email, password, sender_alias=None): | |
self.smtp_server = smtp_server | |
self.smtp_port = smtp_port | |
self.email = email | |
self.password = password | |
self.sender_alias = sender_alias | |
def send_email(self, to_email): | |
# Load data from data.json | |
with open("data.json", "r") as f: | |
data = json.load(f) | |
# Load email template | |
with open("email_template.txt", "r") as f: | |
template = f.read() | |
# Replace placeholders with actual data | |
email_content = template.format(**data) | |
# Create email message | |
msg = EmailMessage() | |
msg.set_content(email_content) | |
msg["Subject"] = "TODO List Task Status Update" | |
# Check if a sender alias is provided | |
from_header = f"{self.sender_alias} <{self.email}>" if self.sender_alias else self.email | |
msg["From"] = from_header | |
msg["To"] = to_email | |
# Send email | |
with smtplib.SMTP_SSL(self.smtp_server, self.smtp_port) as server: | |
server.login(self.email, self.password) | |
server.send_message(msg) | |
# Usage | |
notifier = EmailNotifier("smtp.example.com", 465, "youremail@example.com", "yourpassword", "TodoApp") | |
notifier.send_email("recipient@example.com") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello {name}, | |
Your task "{task}" has changed its status to {status}. | |
Best, | |
Your TODO List App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment