|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import base64 |
| 4 | +import io |
| 5 | +import os |
| 6 | +from nylas import Client |
| 7 | + |
| 8 | + |
| 9 | +def send_message_with_inline_attachment(): |
| 10 | + """ |
| 11 | + This example demonstrates how to send a message with an inline attachment |
| 12 | + that uses a content_id for referencing in HTML email bodies. |
| 13 | + |
| 14 | + This is particularly useful for embedding images directly in HTML emails |
| 15 | + where the image is referenced using 'cid:' in the src attribute. |
| 16 | + """ |
| 17 | + |
| 18 | + # Initialize the Nylas client |
| 19 | + nylas = Client( |
| 20 | + api_key=os.environ.get("NYLAS_API_KEY"), # Replace with your API key |
| 21 | + ) |
| 22 | + |
| 23 | + # Get test email |
| 24 | + test_email = os.environ.get("TEST_EMAIL") |
| 25 | + |
| 26 | + # Get grant |
| 27 | + grant_id = os.environ.get("NYLAS_GRANT_ID") |
| 28 | + |
| 29 | + # Create a sample image content using base64 decoded data |
| 30 | + # This is a small PNG image that can be used for demonstration |
| 31 | + base64_image = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEQUlEQVRYhe2WW2wUVRyHv5nZS7vb3XbLdrfXjSZ9AxOqDRANGjDRSKLRKoIYNLENpEgJRQIKKvSi0tI28oImhtISqg9cTEzUhIgmSkAQY4UWQ2yALi3d7S4UaPcyO7MzPkwlJXG3S02sD5yH8zLJ//vO7/zPmSMsqR0MMIvDBHhnU0CcTfh9gf+FgOnfFlCTIKs6mg6iABaTgFn6DwQ0DUYnNBaVW3iyMpvcHImJmMaPvTG+75PxOkSkDPKdkUBC1bFZRb76sIhyn/WubyufyePaqEJdW5DAWBKrWUhb6557QFZ13E6Jb/eUUe6zcuzkOMu3DlG0/DLPbrrKl8dvUewxc6S1lJI5EqqWvp6wpHZQzxiu6HjyJI60lCKKsLkjwI1xjfdr3PiKLYyMKnQcvE7oVpIDjSWEx1QW1w1R5Ey9zowTkBUd7xR4fVsAm1Wgc0cxD5RY6P8zRrHHTPtbhZS6TXzxzU3cLhNVC7NJqKnXmJGArOgUuiQOtxrwjbsDOG0ijW96kRM6T2+4yorGIAuqB1FUnc2vu9lz9BYAC+ZmkVBT155WQFZ0ilwSh1tKEQXY0DqCK0ekYZ2HeEJn6Xo/t6MahU6RuKJztj9Kfq7EwA1j8y1mkXR7nFZASeoU50scai1FEKCuZQS3U2JHrYe4rLGgZpDmmjn4CiRkVcdmFaica2PsdpLyfKN0QtFIdw5SHkNdB6tZ4FBLKQDrd43gdUm8t9ZDTNaorPbzycYCHq+0Y8sSWdMe4sTeMswmgd1dYTZU5QJwpj+OJc1hT5uApsGAX6Zu1wiF+ZPwuMbD1X4+rTfgv5yPUtUU5MTeMhx2kY4DYQaDKquW5REeUzl6OobFlDqDlAKCYKSwuP4aZV4T767xEI1rVFT7+WxTAYsfsXPmfJTnGoL07fPhsIu0d4c5e1Gm54MSANa1BCiwp2+ztDfheFxny/MO6le7icY0Kmr8dG4u4LEKO6fPRXmhKciFTh8up8TurjC9AzKfT8Jf3T7M0PUkWdPchCkFlCQ8Mc9K/Wo3kZix8q4tBTw6387Pv0d4sXmUC50+8hwSrftDnLuUoKfZgK/aPszlUZXsaeCQZgskEa4EVH76NcKitX66txrwU70RXpoCb+kM0XcpwcEmA/7KtmGuZAhPm4AoQHhcY1lTkO+aCln4kI2Tv0VY8dEo/ft95OZI7NoX4o/BBAcm4Su3DeMPqdPGnpEAQHhC44fmQirnGfA32kL0d/pw5hixX7qm0N1owFe8M8TV8PR7nrFARNZpeM1lwHsjPLUzyLGdXpw5Em3dYT7+eoLjrUUAvPz2UEYN908j5d8woeosnZ/N3ActNPTcxOMQybOJlHkkTl1M4MgSUJNGryQ1HbN07/C0An9LKEmwW43img5JjTtPLn1yEmbGBqbpAYtJuOsaFQUQp7z3hDvTzMesv4rvC8y6gAmIzKbAX+u0pDGsEb6KAAAAAElFTkSuQmCC" |
| 32 | + image_content = base64.b64decode(base64_image) |
| 33 | + |
| 34 | + # Create the message with inline attachment |
| 35 | + message_request = { |
| 36 | + "to": [{"email": test_email, "name": "Recipient Name"}], |
| 37 | + "from": [{"email": test_email, "name": "Sender Name"}], |
| 38 | + "subject": "Message with Inline Image", |
| 39 | + "body": """ |
| 40 | + <html> |
| 41 | + <body> |
| 42 | + <h1>Hello!</h1> |
| 43 | + <p>This email contains an inline image:</p> |
| 44 | + <img src="cid:my-inline-image" alt="Inline Image" style="max-width: 200px;"> |
| 45 | + <p>The image above is embedded directly in the email using content_id.</p> |
| 46 | + </body> |
| 47 | + </html> |
| 48 | + """, |
| 49 | + "attachments": [ |
| 50 | + { |
| 51 | + "filename": "inline-image.png", |
| 52 | + "content_type": "image/png", |
| 53 | + "content": io.BytesIO(image_content), |
| 54 | + "size": len(image_content), |
| 55 | + "content_id": "my-inline-image", # This is the key for inline attachments |
| 56 | + "is_inline": True, |
| 57 | + "content_disposition": "inline" |
| 58 | + }, |
| 59 | + { |
| 60 | + # Regular attachment without content_id for comparison |
| 61 | + "filename": "regular-attachment.txt", |
| 62 | + "content_type": "text/plain", |
| 63 | + "content": io.BytesIO(b"This is a regular attachment"), |
| 64 | + "size": 28, |
| 65 | + # No content_id - this will use the default file{index} naming |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
| 69 | + |
| 70 | + try: |
| 71 | + # Send the message |
| 72 | + response = nylas.messages.send( |
| 73 | + identifier=grant_id, # Replace with your grant ID |
| 74 | + request_body=message_request |
| 75 | + ) |
| 76 | + |
| 77 | + print("Message sent successfully!") |
| 78 | + print(f"Message ID: {response.data.id}") |
| 79 | + print(f"Thread ID: {response.data.thread_id}") |
| 80 | + |
| 81 | + # The inline attachment will be referenced by its content_id in the form data |
| 82 | + # instead of a generic file{index} name, allowing proper inline display |
| 83 | + |
| 84 | + except Exception as e: |
| 85 | + print(f"Error sending message: {e}") |
| 86 | + |
| 87 | + |
| 88 | +def send_draft_with_inline_attachment(): |
| 89 | + """ |
| 90 | + This example demonstrates how to create and send a draft with an inline attachment. |
| 91 | + """ |
| 92 | + |
| 93 | + # Initialize the Nylas client |
| 94 | + nylas = Client( |
| 95 | + api_key=os.environ.get("NYLAS_API_KEY"), # Replace with your API key |
| 96 | + ) |
| 97 | + |
| 98 | + # Get test email |
| 99 | + test_email = os.environ.get("TEST_EMAIL") |
| 100 | + |
| 101 | + # Get grant |
| 102 | + grant_id = os.environ.get("NYLAS_GRANT_ID") |
| 103 | + |
| 104 | + # Create a larger image content to trigger form data usage (>3MB threshold) |
| 105 | + # For demo purposes, we'll replicate the same image data multiple times |
| 106 | + # In real usage, large images would automatically use the content_id functionality |
| 107 | + base64_image = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEQUlEQVRYhe2WW2wUVRyHv5nZS7vb3XbLdrfXjSZ9AxOqDRANGjDRSKLRKoIYNLENpEgJRQIKKvSi0tI28oImhtISqg9cTEzUhIgmSkAQY4UWQ2yALi3d7S4UaPcyO7MzPkwlJXG3S02sD5yH8zLJ//vO7/zPmSMsqR0MMIvDBHhnU0CcTfh9gf+FgOnfFlCTIKs6mg6iABaTgFn6DwQ0DUYnNBaVW3iyMpvcHImJmMaPvTG+75PxOkSkDPKdkUBC1bFZRb76sIhyn/WubyufyePaqEJdW5DAWBKrWUhb6557QFZ13E6Jb/eUUe6zcuzkOMu3DlG0/DLPbrrKl8dvUewxc6S1lJI5EqqWvp6wpHZQzxiu6HjyJI60lCKKsLkjwI1xjfdr3PiKLYyMKnQcvE7oVpIDjSWEx1QW1w1R5Ey9zowTkBUd7xR4fVsAm1Wgc0cxD5RY6P8zRrHHTPtbhZS6TXzxzU3cLhNVC7NJqKnXmJGArOgUuiQOtxrwjbsDOG0ijW96kRM6T2+4yorGIAuqB1FUnc2vu9lz9BYAC+ZmkVBT155WQFZ0ilwSh1tKEQXY0DqCK0ekYZ2HeEJn6Xo/t6MahU6RuKJztj9Kfq7EwA1j8y1mkXR7nFZASeoU50scai1FEKCuZQS3U2JHrYe4rLGgZpDmmjn4CiRkVcdmFaica2PsdpLyfKN0QtFIdw5SHkNdB6tZ4FBLKQDrd43gdUm8t9ZDTNaorPbzycYCHq+0Y8sSWdMe4sTeMswmgd1dYTZU5QJwpj+OJc1hT5uApsGAX6Zu1wiF+ZPwuMbD1X4+rTfgv5yPUtUU5MTeMhx2kY4DYQaDKquW5REeUzl6OobFlDqDlAKCYKSwuP4aZV4T767xEI1rVFT7+WxTAYsfsXPmfJTnGoL07fPhsIu0d4c5e1Gm54MSANa1BCiwp2+ztDfheFxny/MO6le7icY0Kmr8dG4u4LEKO6fPRXmhKciFTh8up8TurjC9AzKfT8Jf3T7M0PUkWdPchCkFlCQ8Mc9K/Wo3kZix8q4tBTw6387Pv0d4sXmUC50+8hwSrftDnLuUoKfZgK/aPszlUZXsaeCQZgskEa4EVH76NcKitX66txrwU70RXpoCb+kM0XcpwcEmA/7KtmGuZAhPm4AoQHhcY1lTkO+aCln4kI2Tv0VY8dEo/ft95OZI7NoX4o/BBAcm4Su3DeMPqdPGnpEAQHhC44fmQirnGfA32kL0d/pw5hixX7qm0N1owFe8M8TV8PR7nrFARNZpeM1lwHsjPLUzyLGdXpw5Em3dYT7+eoLjrUUAvPz2UEYN908j5d8woeosnZ/N3ActNPTcxOMQybOJlHkkTl1M4MgSUJNGryQ1HbN07/C0An9LKEmwW43img5JjTtPLn1yEmbGBqbpAYtJuOsaFQUQp7z3hDvTzMesv4rvC8y6gAmIzKbAX+u0pDGsEb6KAAAAAElFTkSuQmCC" |
| 108 | + large_image_content = base64.b64decode(base64_image) * 1000 # Replicated to make it large |
| 109 | + |
| 110 | + # Create the draft with inline attachment |
| 111 | + draft_request = { |
| 112 | + "to": [{"email": test_email, "name": "Recipient Name"}], |
| 113 | + "from": [{"email": test_email, "name": "Sender Name"}], |
| 114 | + "subject": "Draft with Inline Image", |
| 115 | + "body": """ |
| 116 | + <html> |
| 117 | + <body> |
| 118 | + <h1>Draft Email</h1> |
| 119 | + <p>This draft contains an inline image:</p> |
| 120 | + <img src="cid:logo-image" alt="Company Logo" style="max-width: 300px;"> |
| 121 | + <p>Best regards,<br>Your Team</p> |
| 122 | + </body> |
| 123 | + </html> |
| 124 | + """, |
| 125 | + "attachments": [ |
| 126 | + { |
| 127 | + "filename": "company-logo.png", |
| 128 | + "content_type": "image/png", |
| 129 | + "content": io.BytesIO(large_image_content), |
| 130 | + "size": len(large_image_content), |
| 131 | + "content_id": "logo-image", # Content ID for inline reference |
| 132 | + "is_inline": True, |
| 133 | + "content_disposition": "inline" |
| 134 | + } |
| 135 | + ] |
| 136 | + } |
| 137 | + |
| 138 | + try: |
| 139 | + # Create the draft |
| 140 | + draft_response = nylas.drafts.create( |
| 141 | + identifier=grant_id, # Replace with your grant ID |
| 142 | + request_body=draft_request |
| 143 | + ) |
| 144 | + |
| 145 | + print("Draft created successfully!") |
| 146 | + print(f"Draft ID: {draft_response.data.id}") |
| 147 | + |
| 148 | + # Send the draft |
| 149 | + send_response = nylas.drafts.send( |
| 150 | + identifier=grant_id, # Replace with your grant ID |
| 151 | + draft_id=draft_response.data.id |
| 152 | + ) |
| 153 | + |
| 154 | + print("Draft sent successfully!") |
| 155 | + print(f"Message ID: {send_response.data.id}") |
| 156 | + |
| 157 | + except Exception as e: |
| 158 | + print(f"Error with draft: {e}") |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + print("Inline Attachment Example") |
| 163 | + print("=" * 50) |
| 164 | + print() |
| 165 | + |
| 166 | + # Check if API key is set |
| 167 | + if not os.environ.get("NYLAS_API_KEY"): |
| 168 | + print("Please set the NYLAS_API_KEY environment variable") |
| 169 | + print("export NYLAS_API_KEY='your-api-key-here'") |
| 170 | + exit(1) |
| 171 | + |
| 172 | + # Check if grant ID is set |
| 173 | + if not os.environ.get("NYLAS_GRANT_ID"): |
| 174 | + print("Please set the NYLAS_GRANT_ID environment variable") |
| 175 | + print("export NYLAS_GRANT_ID='your-grant-id-here'") |
| 176 | + exit(1) |
| 177 | + |
| 178 | + # Check if test email is set |
| 179 | + if not os.environ.get("TEST_EMAIL"): |
| 180 | + print("Please set the TEST_EMAIL environment variable") |
| 181 | + print("export TEST_EMAIL='your-test-email-here'") |
| 182 | + exit(1) |
| 183 | + |
| 184 | + print("1. Sending message with inline attachment...") |
| 185 | + send_message_with_inline_attachment() |
| 186 | + |
| 187 | + print("\n2. Creating and sending draft with inline attachment...") |
| 188 | + send_draft_with_inline_attachment() |
| 189 | + |
| 190 | + print("\nNote: The content_id field ensures that large inline attachments") |
| 191 | + print("are properly referenced in the multipart form data, allowing") |
| 192 | + print("email clients to display them inline correctly.") |
0 commit comments