-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathis_plaintext_example.py
More file actions
294 lines (220 loc) · 10.8 KB
/
Copy pathis_plaintext_example.py
File metadata and controls
294 lines (220 loc) · 10.8 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""
Nylas SDK Example: Using is_plaintext for Messages and Drafts
This example demonstrates how to use the new 'is_plaintext' property when sending
messages and creating drafts to control whether content is sent as plain text or HTML.
Required Environment Variables:
NYLAS_API_KEY: Your Nylas API key
NYLAS_GRANT_ID: Your Nylas grant ID
Usage:
First, install the SDK in development mode:
cd /path/to/nylas-python
pip install -e .
Then set environment variables and run:
export NYLAS_API_KEY="your_api_key"
export NYLAS_GRANT_ID="your_grant_id"
python examples/is_plaintext_demo/is_plaintext_example.py
"""
import os
import sys
from nylas import Client
def get_env_or_exit(var_name: str) -> str:
"""Get an environment variable or exit if not found."""
value = os.getenv(var_name)
if not value:
print(f"Error: {var_name} environment variable is required")
sys.exit(1)
return value
def print_separator(title: str) -> None:
"""Print a formatted section separator."""
print(f"\n=== {title} ===")
def demonstrate_plaintext_message(client: Client, grant_id: str) -> None:
"""Demonstrate sending a message with is_plaintext=True."""
print_separator("Sending Plain Text Message")
try:
print("Sending message with is_plaintext=True...")
# Example message content with HTML tags that will be sent as plain text
body_content = """Hello World!
This is a test message sent as plain text.
Even if this contained <strong>HTML tags</strong>, they would be sent as plain text.
Best regards,
The Nylas SDK Team"""
# Send message with is_plaintext=True
message_request = {
"to": [{"email": "test@example.com", "name": "Test Recipient"}],
"subject": "Plain Text Message Example",
"body": body_content,
"is_plaintext": True
}
print("✓ Message request prepared with is_plaintext=True")
print(f" Subject: {message_request['subject']}")
print(f" Body preview: {body_content[:100]}...")
print(f" is_plaintext: {message_request['is_plaintext']}")
# Note: Uncomment the following line to actually send the message
# response = client.messages.send(identifier=grant_id, request_body=message_request)
# print(f"✓ Message sent! ID: {response.data.id}")
print("📧 Message configured to be sent as plain text (MIME without HTML version)")
except Exception as e:
print(f"❌ Error sending plain text message: {e}")
def demonstrate_html_message(client: Client, grant_id: str) -> None:
"""Demonstrate sending a message with is_plaintext=False (HTML)."""
print_separator("Sending HTML Message")
try:
print("Sending message with is_plaintext=False (HTML)...")
# Example message content with HTML formatting
body_content = """<html>
<body>
<h1>Hello World!</h1>
<p>This is a test message sent as <strong>HTML</strong>.</p>
<p>The HTML tags will be properly rendered:</p>
<ul>
<li><em>Emphasized text</em></li>
<li><strong>Bold text</strong></li>
<li><a href="https://nylas.com">Links to Nylas</a></li>
</ul>
<p>Best regards,<br>
<strong>The Nylas SDK Team</strong></p>
</body>
</html>"""
# Send message with is_plaintext=False (default behavior)
message_request = {
"to": [{"email": "test@example.com", "name": "Test Recipient"}],
"subject": "HTML Message Example",
"body": body_content,
"is_plaintext": False
}
print("✓ Message request prepared with is_plaintext=False")
print(f" Subject: {message_request['subject']}")
print(f" HTML body includes formatting tags")
print(f" is_plaintext: {message_request['is_plaintext']}")
# Note: Uncomment the following line to actually send the message
# response = client.messages.send(identifier=grant_id, request_body=message_request)
# print(f"✓ Message sent! ID: {response.data.id}")
print("🌐 Message configured to be sent as HTML (MIME includes HTML version)")
except Exception as e:
print(f"❌ Error sending HTML message: {e}")
def demonstrate_backwards_compatibility(client: Client, grant_id: str) -> None:
"""Demonstrate that existing code without is_plaintext still works."""
print_separator("Backwards Compatibility (No is_plaintext specified)")
try:
print("Sending message without is_plaintext property...")
# Traditional message request without is_plaintext
message_request = {
"to": [{"email": "test@example.com", "name": "Test Recipient"}],
"subject": "Traditional Message Example",
"body": "This message doesn't specify is_plaintext, so it uses the default behavior."
}
print("✓ Message request prepared without is_plaintext property")
print(f" Subject: {message_request['subject']}")
print(f" Body: {message_request['body']}")
print(f" is_plaintext: Not specified (uses API default)")
# Note: Uncomment the following line to actually send the message
# response = client.messages.send(identifier=grant_id, request_body=message_request)
# print(f"✓ Message sent! ID: {response.data.id}")
print("✓ Existing code continues to work without modification")
except Exception as e:
print(f"❌ Error sending traditional message: {e}")
def demonstrate_plaintext_draft(client: Client, grant_id: str) -> None:
"""Demonstrate creating a draft with is_plaintext=True."""
print_separator("Creating Plain Text Draft")
try:
print("Creating draft with is_plaintext=True...")
draft_request = {
"to": [{"email": "test@example.com", "name": "Test Recipient"}],
"subject": "Plain Text Draft Example",
"body": "This is a draft that will be sent as plain text when sent.",
"is_plaintext": True
}
print("✓ Draft request prepared with is_plaintext=True")
print(f" Subject: {draft_request['subject']}")
print(f" Body: {draft_request['body']}")
print(f" is_plaintext: {draft_request['is_plaintext']}")
# Note: Uncomment the following lines to actually create the draft
# response = client.drafts.create(identifier=grant_id, request_body=draft_request)
# print(f"✓ Draft created! ID: {response.data.id}")
print("📝 Draft configured to be sent as plain text when sent")
except Exception as e:
print(f"❌ Error creating plain text draft: {e}")
def demonstrate_html_draft(client: Client, grant_id: str) -> None:
"""Demonstrate creating a draft with is_plaintext=False."""
print_separator("Creating HTML Draft")
try:
print("Creating draft with is_plaintext=False...")
html_body = """<html>
<body>
<h2>Draft Message</h2>
<p>This is a <strong>draft</strong> with <em>HTML formatting</em>.</p>
<p>It will include HTML in the MIME data when sent.</p>
</body>
</html>"""
draft_request = {
"to": [{"email": "test@example.com", "name": "Test Recipient"}],
"subject": "HTML Draft Example",
"body": html_body,
"is_plaintext": False
}
print("✓ Draft request prepared with is_plaintext=False")
print(f" Subject: {draft_request['subject']}")
print(f" HTML body includes formatting")
print(f" is_plaintext: {draft_request['is_plaintext']}")
# Note: Uncomment the following lines to actually create the draft
# response = client.drafts.create(identifier=grant_id, request_body=draft_request)
# print(f"✓ Draft created! ID: {response.data.id}")
print("📝 Draft configured to be sent as HTML when sent")
except Exception as e:
print(f"❌ Error creating HTML draft: {e}")
def demonstrate_draft_update(client: Client, grant_id: str) -> None:
"""Demonstrate updating a draft with is_plaintext property."""
print_separator("Updating Draft with is_plaintext")
try:
print("Demonstrating draft update with is_plaintext...")
# Example update request
update_request = {
"subject": "Updated Draft with Plain Text",
"body": "This draft has been updated to use plain text format.",
"is_plaintext": True
}
print("✓ Draft update request prepared with is_plaintext=True")
print(f" Updated subject: {update_request['subject']}")
print(f" Updated body: {update_request['body']}")
print(f" is_plaintext: {update_request['is_plaintext']}")
# Note: Uncomment the following lines to actually update a draft
# draft_id = "your_draft_id_here"
# response = client.drafts.update(
# identifier=grant_id,
# draft_id=draft_id,
# request_body=update_request
# )
# print(f"✓ Draft updated! ID: {response.data.id}")
print("📝 Draft update includes is_plaintext configuration")
except Exception as e:
print(f"❌ Error updating draft: {e}")
def main():
"""Main function demonstrating is_plaintext usage."""
# Get required environment variables
api_key = get_env_or_exit("NYLAS_API_KEY")
grant_id = get_env_or_exit("NYLAS_GRANT_ID")
# Initialize Nylas client
client = Client(api_key=api_key)
print("Demonstrating is_plaintext Property Usage")
print("=======================================")
print("This shows the new 'is_plaintext' property for messages and drafts")
print("Note: Actual API calls are commented out to prevent unintended sends")
# Demonstrate message sending with different is_plaintext values
demonstrate_plaintext_message(client, grant_id)
demonstrate_html_message(client, grant_id)
demonstrate_backwards_compatibility(client, grant_id)
# Demonstrate draft creation and updating with is_plaintext
demonstrate_plaintext_draft(client, grant_id)
demonstrate_html_draft(client, grant_id)
demonstrate_draft_update(client, grant_id)
print("\n" + "="*60)
print("Example completed successfully!")
print("="*60)
print("\n💡 Key Takeaways:")
print("• is_plaintext=True: Sends content as plain text (no HTML in MIME)")
print("• is_plaintext=False: Sends content as HTML (includes HTML in MIME)")
print("• Not specified: Uses API default behavior (backwards compatible)")
print("• Available in: messages.send(), drafts.create(), drafts.update()")
if __name__ == "__main__":
main()