-
Notifications
You must be signed in to change notification settings - Fork 2
docs: add inline and external image example #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
examples/outbound_messages/send_with_inline_and_external_images.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """ | ||
| Example: combining inline (base64) images and external image URLs in one email. | ||
|
|
||
| Use case: | ||
| - Inline images (content_id / cid:) are embedded in the email itself. | ||
| The recipient's email client displays them without making any external HTTP | ||
| request, which is ideal for logos or brand assets you always want visible. | ||
| - External image URLs are loaded from a remote server at open time. | ||
| This is the standard technique for tracking pixels and analytics because | ||
| the server can record who fetched the image and when. | ||
|
|
||
| Both techniques can appear in the same html_body. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import base64 | ||
| import os | ||
|
|
||
| import postmark | ||
| from postmark.models.outbound import Attachment, Email | ||
|
|
||
| try: | ||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
| except ImportError: | ||
| pass | ||
|
|
||
| SENDER = os.environ["POSTMARK_SENDER_EMAIL"] | ||
|
|
||
| # External tracking pixel URL — loaded by the recipient's email client at open | ||
| # time, so the server can record the open event. | ||
| TRACKING_PIXEL_URL = "https://track.example.com/pixel.png" | ||
|
|
||
|
|
||
| async def main(): | ||
| async with postmark.ServerClient(os.environ["POSTMARK_SERVER_TOKEN"]) as client: | ||
| # Inline logo — embedded in the email as base64, no external request. | ||
| with open("/path/to/logo.png", "rb") as f: | ||
| inline_logo = Attachment( | ||
| name="logo.png", | ||
| content=base64.b64encode(f.read()).decode("utf-8"), | ||
| content_type="image/png", | ||
| content_id="cid:logo", # referenced below as <img src="cid:logo"> | ||
| ) | ||
|
|
||
| html_body = f""" | ||
| <html><body> | ||
| <!-- Inline image: served from the email itself, always visible offline --> | ||
| <img src="cid:logo" alt="Logo" /> | ||
|
|
||
| <p>Hello! Thanks for reading.</p> | ||
|
|
||
| <!-- External image: fetched at open time for analytics --> | ||
| <img src="{TRACKING_PIXEL_URL}" width="1" height="1" alt="" /> | ||
| </body></html> | ||
| """ | ||
|
|
||
| response = await client.outbound.send( | ||
| Email( | ||
| sender=SENDER, | ||
| to="receiver@example.com", | ||
| subject="Hello — with inline logo and tracking", | ||
| text_body="Hello! Thanks for reading. (Open the HTML version to see the logo.)", | ||
| html_body=html_body, | ||
| attachments=[inline_logo], | ||
| ) | ||
| ) | ||
|
|
||
| print(f"Sent: {response.message_id}") | ||
|
|
||
|
|
||
| asyncio.run(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.