Home App-Builder Apps ProcScript-Code Code Automation-Flows Flows Functions-API Functions Plans login

« Back to Blog

Email Invoices Into Podio and Match Them to POs

Email Invoices Into Podio and Match Them to POs

Take a 14-person electrical contractor. Every Monday morning, the bookkeeper opens the accounts inbox and finds around 35 supplier invoices from the week before. Each one gets the same treatment: download the PDF, read the PO number, find that purchase order in Podio, create a bill, upload the PDF, link it to the PO. At four minutes an invoice, that is more than two hours, and it is the dullest two hours of the week.

The firm already runs its jobs and purchase orders in Podio. The invoices just need to get there by themselves, already attached to the right PO.

What they tried first

Podio's email-to-app address. Every Podio app has one. Send an email to it and Podio creates an item, with the attachments. That got the PDFs into Podio, but every bill arrived unlinked. The bookkeeper still had to read each one, search for the PO and set the relationship by hand. The slow part was untouched.

Zapier with an email parser. This could pull a PO number out of an email, but it meant a parser template per supplier layout, a Podio search step and a paid plan priced per task. Three steps per invoice, 140 invoices a month, and a break every time a supplier changed their email template.

What they needed was one small piece of code that reads the email, finds anything that looks like a PO number and acts on it. ProcFu runs ProcScript straight from an incoming email, so that is the build.

The two Podio apps

Purchase Orders already existed. The automation only needs one field from it:

Field Type Example
PO number (po-number) Text PO-2417

Supplier Bills is new. One item per invoice email:

Field Type Purpose
Title (title) Text The email subject
From (from) Text Who sent it
Purchase order (purchase-order) Relationship Link to Purchase Orders
Status (status) Category Matched, Needs PO, Approved, Paid

The PDFs go into the item's files, so they show up in the sidebar like any other Podio attachment.

Sketch: supplier email goes to the ProcFu catch address, the script looks for a PO number, then either links the bill to the PO or marks it Needs PO

Step 1: Create the script and give it an email address

In ProcFu, open Code and create a new script. Call it something you will recognise in the logs, such as supplier-invoices. In File Properties, set Mode to Email - from email and save.

ProcFu then shows the script's own address underneath, a random string at catch.procfu.com. Any email sent to that address runs this script. The other two modes are Direct - run here only, the default, and HTTP - via URL, which comes up at the end of this post.

ProcFu code editor with the supplier-invoices script, File Properties panel showing Mode set to Email - from email and the script's catch.procfu.com address

Step 2: The script

When an email arrives, the script receives it in $payload:

  • $payload["POST"]["headers"]: the email headers, with lowercase keys such as subject, from and to
  • $payload["POST"]["text"] and $payload["POST"]["html"]: the body, in both forms
  • $payload["FILES"]: one entry per attachment, with name, type and base64data

Here is the whole script:

// Supplier invoices by email: match to a PO, attach the PDFs
$bills_app = 30112233;
$po_app = 30112244;

$headers = $payload["POST"]["headers"];
$subject = $headers["subject"];
$from = $headers["from"];
$text = $payload["POST"]["text"];

// Matches PO-2417, PO 2417 and PO#2417 in the subject or body
$num = preg_match_gf('/PO[\s#-]*(\d{4,})/i', $subject + " " + $text, 1);

$po = [];
if ($num != "") {
    $po = flow_app_search($po_app, "po-number", "PO-" + $num, "E", 1);
}

$fields = ["title" => $subject, "from" => $from, "status" => "Needs PO"];
if (count($po) > 0) {
    $fields["purchase-order"] = $po[0]["item_id"];
    $fields["status"] = "Matched";
}
$bill_id = podio_item_create($bills_app, json_encode($fields), true, false);

// Attach PDFs only, so signature logos stay out of Podio
$pdfs = 0;
$files = $payload["FILES"];
foreach ($files as $file) {
    if (strtolower(substr($file["name"], -4)) == ".pdf") {
        podio_file_from_base64($bill_id, $file["name"], $file["base64data"]);
        $pdfs = $pdfs + 1;
    }
}

if (count($po) > 0) {
    podio_comment_create($po[0]["item_id"], "Invoice received from " + $from + " with " + $pdfs + " PDF(s). It is linked under Supplier Bills.", false, true);
}

What each part does:

  • preg_match_gf() runs a regular expression and returns one capture group, or an empty string when nothing matches. Suppliers write PO numbers in a dozen ways. This pattern accepts a space, a hash or a hyphen after "PO", in either case, anywhere in the subject or body.
  • flow_app_search() looks for a Purchase Orders item whose po-number equals PO-2417 exactly (E). It returns items in ProcFu's simplified format, so the item ID is $po[0]["item_id"].
  • podio_item_create() takes field values as JSON keyed by external ID. A relationship field takes the item ID of the linked item, and a category takes the option text. The third argument is true, so Podio fires its webhooks for the new bill. Any Podio Workflow Automation flow or ProcFu flow on Supplier Bills runs as normal.
  • podio_file_from_base64() turns each PDF attachment into a Podio file on the bill. Email signatures often carry logos as image attachments. Checking the extension keeps them out.
  • The comment on the PO tells whoever raised it that the invoice is in. Its last argument is true, which posts it silently, so nobody gets a notification.

No PO number found, or no PO with that number? The bill is still created, with Status Needs PO. Nothing gets lost. It waits in a view for a person.

Step 3: Get the invoices to the address

The script finds itself by the first address in the email's To line. That decides how you route mail to it:

  • Best: give suppliers the catch address, or put it on your PO template as "Send invoices to".
  • Fine: a mail rule that forwards invoice emails to the catch address. A forward is a new message, addressed to the catch address.
  • Will not work: a server-side redirect or an automatic forward that keeps the original headers. The To line still says accounts@, so ProcFu can't match the email to the script.

Our contractor keeps accounts@ on its letterhead and adds one Outlook rule: anything with "invoice" in the subject from a known supplier is forwarded to the catch address. Forwarding wraps the original text in the body, which is fine, because the script searches the body too.

Testing without sending 20 emails

Send one real invoice email to the address, then open the script in the code editor. The console shows Most recent payload: the headers, the body and the attachments exactly as ProcFu received them.

That payload stays loaded. Click Run and the script runs against it, so you can adjust the regular expression and run again as often as you like without another email. Each run creates a real bill, so delete the test items when you are done.

The script also logs every execution with the sender and the run time, and any error with its line number. A supplier whose emails keep landing in Needs PO usually writes the PO number somewhere the pattern does not expect. Their last payload shows you where.

The result

A matched bill looks like this in Podio. The subject became the title, the PO is linked, and the status is already set:

Podio Supplier Bills item "Invoice INV-58213 for PO-2417" from Ridgeway Electrical Supply, linked to purchase order PO-2417 with Status Matched, and a bookkeeper comment approving it for payment

Picture a typical month for this contractor:

  • 140 supplier invoices come in by email.
  • Most carry a PO number in the subject or body. Those bills arrive matched, with the PDF attached and the link set.
  • The rest land in Needs PO. Usually they come from a handful of suppliers who put the PO number only inside the PDF. Ask them to add it to the subject line and the queue shrinks.

The Monday inbox session drops from two hours to the time it takes to clear Needs PO and approve matched bills. Each email costs one ProcFu action. Email and HTTP scripts are available on paid plans and during the free trial.

The same script, triggered by a URL

Set Mode to HTTP - via URL instead, and the script gets a URL at procfu.com/widgets/code/.... Any system that can send a webhook can call it: a supplier portal, a payment provider, a website form. The request arrives in $payload["GET"], $payload["POST"] and $payload["FILES"], and the raw request body is in $payload["STDIN"], which is where JSON webhooks put their data. Whatever the script prints becomes the HTTP response. It has 30 seconds to reply.

The pattern holds either way. Something outside Podio sends data. A few lines of ProcScript decide where it belongs. Podio gets a linked, complete item instead of another thing to file by hand.

Start a free ProcFu trial and give your first script an email address.