-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathinline_attachment_example.py
More file actions
192 lines (159 loc) · 9.45 KB
/
inline_attachment_example.py
File metadata and controls
192 lines (159 loc) · 9.45 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
#!/usr/bin/env python3
import base64
import io
import os
from nylas import Client
def send_message_with_inline_attachment():
"""
This example demonstrates how to send a message with an inline attachment
that uses a content_id for referencing in HTML email bodies.
This is particularly useful for embedding images directly in HTML emails
where the image is referenced using 'cid:' in the src attribute.
"""
# Initialize the Nylas client
nylas = Client(
api_key=os.environ.get("NYLAS_API_KEY"), # Replace with your API key
)
# Get test email
test_email = os.environ.get("TEST_EMAIL")
# Get grant
grant_id = os.environ.get("NYLAS_GRANT_ID")
# Create a sample image content using base64 decoded data
# This is a small PNG image that can be used for demonstration
base64_image = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEQUlEQVRYhe2WW2wUVRyHv5nZS7vb3XbLdrfXjSZ9AxOqDRANGjDRSKLRKoIYNLENpEgJRQIKKvSi0tI28oImhtISqg9cTEzUhIgmSkAQY4UWQ2yALi3d7S4UaPcyO7MzPkwlJXG3S02sD5yH8zLJ//vO7/zPmSMsqR0MMIvDBHhnU0CcTfh9gf+FgOnfFlCTI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M8TV8PR7nrFARNZpeM1lwHsjPLUzyLGdXpw5Em3dYT7+eoLjrUUAvPz2UEYN908j5d8woeosnZ/N3ActNPTcxOMQybOJlHkkTl1M4MgSUJNGryQ1HbN07/C0An9LKEmwW43img5JjTtPLn1yEmbGBqbpAYtJuOsaFQUQp7z3hDvTzMesv4rvC8y6gAmIzKbAX+u0pDGsEb6KAAAAAElFTkSuQmCC"
image_content = base64.b64decode(base64_image)
# Create the message with inline attachment
message_request = {
"to": [{"email": test_email, "name": "Recipient Name"}],
"from": [{"email": test_email, "name": "Sender Name"}],
"subject": "Message with Inline Image",
"body": """
<html>
<body>
<h1>Hello!</h1>
<p>This email contains an inline image:</p>
<img src="cid:my-inline-image" alt="Inline Image" style="max-width: 200px;">
<p>The image above is embedded directly in the email using content_id.</p>
</body>
</html>
""",
"attachments": [
{
"filename": "inline-image.png",
"content_type": "image/png",
"content": io.BytesIO(image_content),
"size": len(image_content),
"content_id": "my-inline-image", # This is the key for inline attachments
"is_inline": True,
"content_disposition": "inline"
},
{
# Regular attachment without content_id for comparison
"filename": "regular-attachment.txt",
"content_type": "text/plain",
"content": io.BytesIO(b"This is a regular attachment"),
"size": 28,
# No content_id - this will use the default file{index} naming
}
]
}
try:
# Send the message
response = nylas.messages.send(
identifier=grant_id, # Replace with your grant ID
request_body=message_request
)
print("Message sent successfully!")
print(f"Message ID: {response.data.id}")
print(f"Thread ID: {response.data.thread_id}")
# The inline attachment will be referenced by its content_id in the form data
# instead of a generic file{index} name, allowing proper inline display
except Exception as e:
print(f"Error sending message: {e}")
def send_draft_with_inline_attachment():
"""
This example demonstrates how to create and send a draft with an inline attachment.
"""
# Initialize the Nylas client
nylas = Client(
api_key=os.environ.get("NYLAS_API_KEY"), # Replace with your API key
)
# Get test email
test_email = os.environ.get("TEST_EMAIL")
# Get grant
grant_id = os.environ.get("NYLAS_GRANT_ID")
# Create a larger image content to trigger form data usage (>3MB threshold)
# For demo purposes, we'll replicate the same image data multiple times
# In real usage, large images would automatically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AzKfT8Jf3T7M0PUkWdPchCkFlCQ8Mc9K/Wo3kZix8q4tBTw6387Pv0d4sXmUC50+8hwSrftDnLuUoKfZgK/aPszlUZXsaeCQZgskEa4EVH76NcKitX66txrwU70RXpoCb+kM0XcpwcEmA/7KtmGuZAhPm4AoQHhcY1lTkO+aCln4kI2Tv0VY8dEo/ft95OZI7NoX4o/BBAcm4Su3DeMPqdPGnpEAQHhC44fmQirnGfA32kL0d/pw5hixX7qm0N1owFe8M8TV8PR7nrFARNZpeM1lwHsjPLUzyLGdXpw5Em3dYT7+eoLjrUUAvPz2UEYN908j5d8woeosnZ/N3ActNPTcxOMQybOJlHkkTl1M4MgSUJNGryQ1HbN07/C0An9LKEmwW43img5JjTtPLn1yEmbGBqbpAYtJuOsaFQUQp7z3hDvTzMesv4rvC8y6gAmIzKbAX+u0pDGsEb6KAAAAAElFTkSuQmCC"
large_image_content = base64.b64decode(base64_image) * 1000 # Replicated to make it large
# Create the draft with inline attachment
draft_request = {
"to": [{"email": test_email, "name": "Recipient Name"}],
"from": [{"email": test_email, "name": "Sender Name"}],
"subject": "Draft with Inline Image",
"body": """
<html>
<body>
<h1>Draft Email</h1>
<p>This draft contains an inline image:</p>
<img src="cid:logo-image" alt="Company Logo" style="max-width: 300px;">
<p>Best regards,<br>Your Team</p>
</body>
</html>
""",
"attachments": [
{
"filename": "company-logo.png",
"content_type": "image/png",
"content": io.BytesIO(large_image_content),
"size": len(large_image_content),
"content_id": "logo-image", # Content ID for inline reference
"is_inline": True,
"content_disposition": "inline"
}
]
}
try:
# Create the draft
draft_response = nylas.drafts.create(
identifier=grant_id, # Replace with your grant ID
request_body=draft_request
)
print("Draft created successfully!")
print(f"Draft ID: {draft_response.data.id}")
# Send the draft
send_response = nylas.drafts.send(
identifier=grant_id, # Replace with your grant ID
draft_id=draft_response.data.id
)
print("Draft sent successfully!")
print(f"Message ID: {send_response.data.id}")
except Exception as e:
print(f"Error with draft: {e}")
if __name__ == "__main__":
print("Inline Attachment Example")
print("=" * 50)
print()
# Check if API key is set
if not os.environ.get("NYLAS_API_KEY"):
print("Please set the NYLAS_API_KEY environment variable")
print("export NYLAS_API_KEY='your-api-key-here'")
exit(1)
# Check if grant ID is set
if not os.environ.get("NYLAS_GRANT_ID"):
print("Please set the NYLAS_GRANT_ID environment variable")
print("export NYLAS_GRANT_ID='your-grant-id-here'")
exit(1)
# Check if test email is set
if not os.environ.get("TEST_EMAIL"):
print("Please set the TEST_EMAIL environment variable")
print("export TEST_EMAIL='your-test-email-here'")
exit(1)
print("1. Sending message with inline attachment...")
send_message_with_inline_attachment()
print("\n2. Creating and sending draft with inline attachment...")
send_draft_with_inline_attachment()
print("\nNote: The content_id field ensures that large inline attachments")
print("are properly referenced in the multipart form data, allowing")
print("email clients to display them inline correctly.")