-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_email.py
More file actions
93 lines (81 loc) · 2.65 KB
/
send_email.py
File metadata and controls
93 lines (81 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from postmarker.core import PostmarkClient
from typing import List, Optional
import os
def send_email(
to_email: str,
subject: str,
html_content: str,
from_email: Optional[str] = None,
cc: Optional[List[str]] = None,
bcc: Optional[List[str]] = None,
reply_to: Optional[str] = None,
track_opens: bool = True,
) -> dict:
"""
Send an email using the Postmark API.
Args:
to_email (str): Recipient email address
subject (str): Email subject
html_content (str): HTML content of the email
from_email (str, optional): Sender email address. Defaults to env variable.
cc (List[str], optional): List of CC recipients
bcc (List[str], optional): List of BCC recipients
reply_to (str, optional): Reply-to email address
track_opens (bool): Whether to track email opens. Defaults to True.
Returns:
dict: Response from Postmark API
Raises:
Exception: If POSTMARK_API_TOKEN is not set or if the API call fails
"""
# Get API token from environment variable
api_token = os.getenv('POSTMARK_API_TOKEN')
if not api_token:
raise Exception("POSTMARK_API_TOKEN environment variable is not set")
# Initialize Postmark client
postmark = PostmarkClient(server_token=api_token)
# Use default from_email if not provided
if not from_email:
from_email = os.getenv('DEFAULT_FROM_EMAIL', 'no-reply@yourdomain.com')
# Prepare email payload
email_data = {
"From": from_email,
"To": to_email,
"Subject": subject,
"HtmlBody": html_content,
"TrackOpens": track_opens,
}
# Add optional parameters if provided
if cc:
email_data["Cc"] = ",".join(cc)
if bcc:
email_data["Bcc"] = ",".join(bcc)
if reply_to:
email_data["ReplyTo"] = reply_to
# Send email
try:
response = postmark.emails.send(**email_data)
return response
except Exception as e:
raise Exception(f"Failed to send email: {str(e)}")
# Example usage
if __name__ == "__main__":
# Example HTML content
html_content = """
<html>
<body>
<h1>Hello!</h1>
<p>This is a test email sent using Postmark.</p>
</body>
</html>
"""
try:
response = send_email(
to_email="jason@fluencyai.net",
from_email="jason@fluencyai.net",
subject="Test Email",
html_content=html_content,
reply_to="jason@fluencyai.net"
)
print("Email sent successfully:", response)
except Exception as e:
print("Error:", str(e))