Welcome to Day 8 of 12 Days of DigitalOcean! Yesterday, you deployed a Flask app on DigitalOcean to start building an Email-Based Receipt Processor. This app will let you send receipts to an email address and have them automatically processed by your app.
Today, you’ll set up Postmark to handle incoming emails. Postmark receives emails, turns them into easy-to-handle JSON data, and sends it to your app. This means you don’t have to worry about managing email servers or decoding raw email formats—Postmark takes care of all of that for you.
By the end of this tutorial, you’ll have a setup where emails sent to a dedicated address are automatically forwarded to your Flask app, ready to be logged, stored, or analyzed. Let’s get started!
Here’s how everything fits together:
With this setup, Postmark handles the heavy lifting of email parsing so your app can focus on using the data—whether it’s storing it in a database, cleaning it up, or preparing it for analysis.
You’ll start by updating the Flask app to handle incoming emails. Then, you’ll configure Postmark to send email data to your app and test the setup to ensure it’s all working.
Your app needs a route where Postmark can send the email data. Let’s set that up.
Open your app.py
file and add this code:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/inbound', methods=['POST'])
def inbound():
# Parse the JSON data sent by Postmark
email_data = request.get_json()
# Extract useful information
subject = email_data.get('Subject', 'No subject')
from_email = email_data.get('FromFull', {}).get('Email', 'Unknown sender')
body = email_data.get('TextBody', '')
# Log the details (or process them)
print(f"Received email from {from_email} with subject: {subject}")
print(f"Body: {body}")
return jsonify({"status": "success"}), 200
This code sets up a new /inbound
route that does three things:
Save the changes to app.py
.
Commit and push the changes to GitHub to redeploy the app on DigitalOcean:
git add app.py
git commit -m "Add inbound route for Postmark emails"
git push origin main
Now your app is ready to receive email data from Postmark. Head over to your DigitalOcean App Platform dashboard and check the status of your app. Once it’s marked as live, grab the public URL for your app. Postmark will send email data to this URL.
Quick Tip: If you’d like to test locally instead, you can use Ngrok to expose your Flask app to the internet temporarily. Run:
ngrok http 5000
Ngrok will give you a URL like https://abcd1234.ngrok.io
. You can use this as your webhook URL when setting up Postmark.
Postmark will handle the job of parsing emails and forwarding the structured data to your app.
Sign Up or Log In to Postmark:
Create a Server:
Once logged in, go to the Servers tab and click Create Server.
Name the server something like Receipt Processor
Use the Default Inbound Stream:
Quick Note: Postmark organizes emails using streams, which are designed to handle different types of email traffic. The Default Inbound Stream is where incoming emails are processed.
123456abcdefgh@inbound.postmarkapp.com
.Quick Note: Save this email address! You’ll use it to send test emails later and verify your setup.
Next, you’ll set up the webhook URL, so Postmark knows where to send email data.
Set Your Webhook URL in Postmark:
On the Default Inbound Stream settings screen, find the field labeled Set your server’s inbound webhook URL.
Paste your DigitalOcean app URL, adding /inbound
at the end. Postmark will use this URL to send the email data. For example:
hammerhead-xyz.ondigitalocean.app/inbound
Ngrok
, paste your Ngrok public URL here and add /inbound
at the end.Quick Tip: If you update your webhook URL later (for example, when switching from Ngrok to a live app URL), you can return to this field in Postmark’s Default Inbound Stream settings and update the URL.
Let’s make sure everything is working as expected.
Send a Test Email:
Use the Postmark-provided email address (e.g., 123456abcdefgh@inbound.postmarkapp.com
).
Send an email with a subject and body for testing.
For example:
Subject: Receipt for Order #12345
Body: Thank you for your purchase!
Your order #12345 has been processed.
Total: $50.00
Date: December 29, 2024
Once the email is sent to your Postmark-provided email address, Postmark will forward the email data to your Flask app.
Check Postmark Activity:
Navigate to the Activity tab in your Postmark dashboard to confirm the email was received and forwarded correctly.
Verify Your Runtime Logs:
Head to your DigitalOcean App Platform dashboard, select your app, and click on the Logs tab to access the Runtime Logs.
You should see something like this:
Received email from sender@example.com with subject: Receipt for Order #12345
Body:
Thank you for your purchase!
Your order #12345 has been processed.
Total: $50.00
Date: December 29, 2024
digitalocean_runtime_logs_screenshot
If something isn’t working, here are a few things to check:
/inbound
route is set up in your Flask app and it’s running (either on DigitalOcean or locally with Ngrok)/inbound
and matches your app’s public URL./inbound
route are active and haven’t been commented out or removed. These logs are essential for debugging during testing.Tip: If you’re testing locally with Ngrok, make sure your Ngrok URL is active and points to the correct port.
Here’s what you accomplished today:
Here is the previous tutorial from this series on Day 7: Building and Deploying the Email-Based Receipt Processor.
Your Email-Based Receipt Processor is now ready to receive and process emails automatically! In the next tutorial, you’ll take this data and make it even more useful by integrating AI tools to extract and organize receipt details. See you for Day 9! 🚀
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!