ARTICLE AD BOX
Welcome to Day 8 of 12 Days of DigitalOcean! Yesterday, you deployed a Flask app connected DigitalOcean to commencement building an Email-Based Receipt Processor. This app will fto you nonstop receipts to an email reside and personification them automatically processed by your app.
Today, you’ll group up Postmark to grip incoming emails. Postmark receives emails, turns them into easy-to-handle JSON data, and sends it to your app. This intends you don’t personification to liking astir managing email servers aliases decoding earthy email formats—Postmark takes attraction of each of that for you.
By nan extremity of this tutorial, you’ll personification a setup wherever emails sent to a dedicated reside are automatically forwarded to your Flask app, caller to beryllium logged, stored, aliases analyzed. Let’s get started!
✨ How It Works
Here’s really everything fits together:
- A personification sends an email (like a receipt) to a Postmark-provided address.
- Postmark receives nan email, processes its contented into JSON.
- Postmark sends nan strategy accusation to your Flask app hosted connected DigitalOcean utilizing a webhook URL.
- Your app processes nan data, extracting nan accusation it needs, for illustration nan sender, subject, and body.
With this setup, Postmark handles nan dense lifting of email parsing truthful your app tin attraction connected utilizing nan data—whether it’s storing it successful a database, cleaning it up, aliases preparing it for analysis.
Step-by-Step Guide
You’ll commencement by updating nan Flask app to grip incoming emails. Then, you’ll configure Postmark to nonstop email accusation to your app and proceedings nan setup to guarantee it’s each working.
Step 1 - Update Your Flask App
Your app needs a measurement wherever Postmark tin nonstop nan email data. Let’s group that up.
-
Open your app.py grounds and adhd this code:
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/inbound', methods=['POST']) def inbound(): email_data = request.get_json() taxable = email_data.get('Subject', 'No subject') from_email = email_data.get('FromFull', {}).get('Email', 'Unknown sender') assemblage = email_data.get('TextBody', '') print(f"Received email from {from_email} pinch subject: {subject}") print(f"Body: {body}") return jsonify({"status": "success"}), 200This codification sets up a caller /inbound measurement that does 3 things:
- Listens for POST requests from Postmark.
- Extracts cardinal specifications for illustration nan subject, sender email, and assemblage from nan JSON payload.
- Logs these specifications to your app’s console for now. We’ll widen this to analyse nan accusation and shop it successful a database arsenic a adjacent step.
-
Save nan changes to app.py.
-
Commit and push nan changes to GitHub to redeploy nan app connected DigitalOcean:
git add app.py git perpetrate -m "Add inbound measurement for Postmark emails" git push guidelines main -
Now your app is caller to personification email accusation from Postmark. Head complete to your DigitalOcean App Platform dashboard and cheque nan position of your app. Once it’s marked arsenic live, drawback nan nationalist URL for your app. Postmark will nonstop email accusation to this URL.
Quick Tip: If you’d for illustration to proceedings locally instead, you tin usage Ngrok to expose your Flask app to nan nett temporarily. Run:
ngrok http 5000
Ngrok will springiness you a URL for illustration https://abcd1234.ngrok.io. You tin usage this arsenic your webhook URL erstwhile mounting up Postmark.
Step 2 - Set Up Postmark
Postmark will grip nan business of parsing emails and forwarding nan strategy accusation to your app.
-
Sign Up aliases Log In to Postmark:
- If you already personification an account, log successful to Postmark, aliases motion up for free (the free strategy is cleanable for testing and experimenting).
-
Create a Server:
-
Once logged in, spell to nan Servers tab and click Create Server.
-
Name nan server point for illustration Receipt Processor
-
-
Use nan Default Inbound Stream:
- Every caller server includes a less built-in relationship streams. One of them is nan Default Inbound Stream, which is what we’ll usage for this setup.
- Click connected Default Inbound Stream to position its details.
Quick Note: Postmark organizes emails utilizing streams, which are designed to grip different types of email traffic. The Default Inbound Stream is wherever incoming emails are processed.
- Postmark will proviso you pinch an email address, for illustration [email protected].
Quick Note: Save this email address! You’ll usage it to nonstop proceedings emails later and verify your setup.
Step 3 - Connect Postmark to Your App
Next, you’ll group up nan webhook URL, truthful Postmark knows wherever to nonstop email data.
-
Set Your Webhook URL successful Postmark:
-
On nan Default Inbound Stream settings screen, find nan conception branded Set your server’s inbound webhook URL.
-
Paste your DigitalOcean app URL, adding /inbound astatine nan end. Postmark will usage this URL to nonstop nan email data. For example:
hammerhead-xyz.ondigitalocean.app/inbound
- If you’re testing locally pinch Ngrok, paste your Ngrok nationalist URL coming and adhd /inbound astatine nan end.
Quick Tip: If you update your webhook URL later (for example, erstwhile switching from Ngrok to a unrecorded app URL), you tin return to this conception successful Postmark’s Default Inbound Stream settings and update nan URL.
-
Step 4: Test nan Setup
Let’s make judge everything is moving arsenic expected.
-
Send a Test Email:
-
Use nan Postmark-provided email reside (e.g., [email protected]).
-
Send an email pinch a taxable and assemblage for testing.
-
For example:
Subject: Receipt for Order Body: Thank you for your purchase! Your bid Total: $50.00 Date: December 29, 2024
Once nan email is sent to your Postmark-provided email address, Postmark will guardant nan email accusation to your Flask app.
-
-
-
Check Postmark Activity:
-
Navigate to nan Activity tab successful your Postmark dashboard to corroborate nan email was received and forwarded correctly.
-
-
Verify Your Runtime Logs:
-
Head to your DigitalOcean App Platform dashboard, premier your app, and click connected nan Logs tab to entree nan Runtime Logs.
-
You should spot point for illustration this:
Received email from [email protected] pinch subject: Receipt for Order Body: Thank you for your purchase! Your bid Total: $50.00 Date: December 29, 2024digitalocean_runtime_logs_screenshot
-
Troubleshooting
If point isn’t working, coming are a less things to check:
- Make judge nan /inbound measurement is group up successful your Flask app and it’s moving (either connected DigitalOcean aliases locally pinch Ngrok)
- Verify that nan webhook URL successful Postmark includes /inbound and matches your app’s nationalist URL.
- Double-check nan email reside you’re sending to—it should lucifer nan 1 from Postmark’s default inbound stream.
- Check Postmark’s Activity Logs to corroborate emails are being received.
- Look astatine nan Runtime Logs successful DigitalOcean to spot if nan Flask app is logging incoming data.
- Ensure Flask’s group statements successful nan /inbound measurement are progressive and haven’t been commented retired aliases removed. These logs are basal for debugging during testing.
Tip: If you’re testing locally pinch Ngrok, make judge your Ngrok URL is progressive and points to nan correct port.
Wrapping Up
Here’s what you accomplished today:
- You added an inbound measurement successful your Flask app to grip emails from Postmark.
- Configured Postmark’s default inbound stream, grabbed nan provided email address, and connected it to your app utilizing your DigitalOcean URL.
- Sent a proceedings email and checked immoderate nan Runtime Logs connected DigitalOcean and nan Activity Logs connected Postmark to corroborate everything is moving arsenic expected.
Here is nan erstwhile tutorial from this bid connected Day 7: Building and Deploying nan Email-Based Receipt Processor.
Your Email-Based Receipt Processor is now caller to personification and process emails automatically! In nan adjacent tutorial, you’ll return this accusation and make it moreover overmuch useful by integrating AI devices to extract and style receipt details. See you for Day 9! 🚀