Skip to content

No order number retrieved from message #31

Description

@thirstycoda

Apologies for raising another issue. So far, all of the sensors state Unavailable.

In the logs, I'm getting the error "No order number retrieved from message"

The email I received contains:
Order reference: <13 digit number>

I had a quick look at the code at

raw = re.search(r"(?:Order\sref(?:\.|erence):\s)?(?:Order\sis\s)?(?P<order_number>\d{10,14})",message)

I'm not familiar with python but the regex looks like it should work for the given order reference text.

I gave Gemini the function and the email as html to see if it had any ideas. It may have been talking rubbish, but here's what it said:

Your code is failing because the email body is Quoted-Printable encoded. 
Notice the 3D and = characters in your HTML snippet (e.g., content=3D"text/html"). In this encoding, a single = at the end of a line is a soft line break, and 3D represents a literal =. If your order number or its label is split across these soft breaks, your regex will never find a continuous match.

How to Fix It
You must decode the Quoted-Printable text before running your regex. Use Python's built-in quopri or email modules. 

import quopri
import re

def get_order_number(message: str) -> str:
    # 1. Decode Quoted-Printable to get clean text/HTML
    # Note: quopri.decodestring expects bytes
    decoded_bytes = quopri.decodestring(message.encode('utf-8'))
    clean_message = decoded_bytes.decode('utf-8')

    # 2. Run your existing regex on the clean message
    pattern = r"(?:Order\sref(?:\.|erence):\s)?(?:Order\sis\s)?(?P<order_number>\d{10,14})"
    raw = re.search(pattern, clean_message)
    
    if raw:
        return raw.group('order_number')
    
    raise ValueError(f"No order number retrieved from: {clean_message[:50]}...")


Why this happens:
Soft Line Breaks: Quoted-Printable limits lines to 76 characters. It inserts = to break long lines. If your email says Order ref: 1234567890, it might actually be stored as Order ref: 12345= \r\n67890.
Hidden Characters: Characters like = are encoded as =3D, and spaces at the end of lines are encoded as =20. Your regex looks for \s (space), but it sees =20 or a newline instead.

It is true that the email html has:

<head>
    <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF-8=
"/>

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions