How can I read email content in a cloudflare worker

I wish to read the contents of the “ForwardableEmailMessage” in a Cloudflare Email Worker.

I have tried with this simple function:

function readStream(stream: ReadableStream<Uint8Array>): Promise<string> {
  const reader = stream.getReader();
  const decoder = new TextDecoder('utf-8');
  let result = '';

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    if (value) {
      result += decoder.decode(value, { stream: true });
    }
  }

  // Ensure any remaining data is decoded
  result += decoder.decode();

  return result;
}

It provides readable content, but it does not look like the “original” message if I for instance check in my “sent” box in Gmail. The “original message” in gmail looks simpler. If I forward a message, and check the “email.raw” from cloudflare, I am not able to see “---------- Forwarded message ----------” like I am in gmail for instance.

This is the output I get on Worker:

Received: from mail-ej1-x633.google.com (2a10:1350:4764:10::613)\r\n        by cloudflare-email.net (unknown) id 4Ia1OciIupr9\r\n        for <[email protected]>; Fri, 31 May 2024 15:03:03 +0000\r\nARC-Seal: i=1; a=rsa-sha256; s=2023; d=cloudflare-email.net; cv=none;\r\n\tb=RKZC0GmplmZWi5WbW11+STC9zEnANgaZ78S40Q4qk7gTIkKZqkZM6fqUSMOTnYCvWBIXvlAcA\r\n\tHcBPUor5t3/SWFbf8HgP4PUbl8vCd84BcM7hsmoywNPc1USGDb1WfaYirzrUSGmqJyDHE0o6rDi\r\n\t7twIubp46Gasj10M3aLM6gfRKpmWAvCrfqCzjm42VVz6dexQLQ33uHyYF0efSOXb7uUOVSeO0Zw\r\n\tKD+W6/M1dd4PuSiGUVEIfUZ3wI3ll2vlqt0GK1jLE34HvE2F+9yXHABg7Ub2S6iZRwyEDmAvTqq\r\n\tuvEwlmZnoIDCX1Pa7Bo3UqZ7KxjKfW8kS3+BxpT8kHLA==;\r\nARC-Message-Signature: i=1; a=rsa-sha256; s=2023; d=cloudflare-email.net; c=relaxed/relaxed;\r\n\th=Subject:References:In-Reply-To:To:From:Date:reply-to:cc:resent-date\r\n\t:resent-from:resent-to:resent-cc:list-id:list-help:list-unsubscribe\r\n\t:list-subscribe:list-post:list-owner:list-archive; t=1717167783;\r\n\tx=1717772583; bh=zwW2waXIcXpbAvwT3nXEjJq+LMrbkllviAT/UmsoKoE=; b=AivZr4gwQV\r\n\tScN6MvQwXNJyXqsiyibgBlqevgf9w2ghFPRpwLkc8S1uDxBIGwYKgYE67hz4yxHsP3G66JZ7fV5\r\n\t3mkPYi7A8monarr74jB/jDrn+TrOqGR23Lrjl2kfg8CsTwHGpR+ucLlWygVCCVMxx67lNTHN7+B\r\n\t+f6XNmQ8CXdXvPtJZemt/JfrS0hUQrA97Ydp8kAaQ2A9p5VK+6kP5CTrsi8IEWQ8vGJDHjhoIg0\r\n\tixReQz6nTpeWGt8Chk5APg2fQFPKGlXSBNS8SkEPmKXdxspIY1q3vmnfbztO516bfrjFPCjqc8y\r\n\tBOgz4C1wiGIFtxrnt3gSM03WaJTxsl1A==;\r\nARC-Authentication-Results: i=1; mx.cloudflare.net;\r\n\tdkim=pass header.d=mydomain-dk.20230601.gappssmtp.com header.s=20230601 header.b=fBiU/QAD;\r\n\tdmarc=none header.from=mydomain.dk policy.dmarc=none;\r\n\tspf=none (mx.cloudflare.net: no SPF records found for [email protected]) smtp.helo=mail-ej1-x633.google.com;\r\n\tspf=softfail (mx.cloudflare.net: domain of [email protected] reports soft fail for 2a10:1350:4764:10::613) [email protected];\r\n\tarc=none smtp.remote-ip=2a10:1350:4764:10::613\r\nReceived-SPF: softfail (mx.cloudflare.net: domain of [email protected] reports soft fail for 2a10:1350:4764:10::613)\r\n\treceiver=mx.cloudflare.net; client-ip=2a10:1350:4764:10::613; envelope-from=\"[email protected]\"; helo=mail-ej1-x633.google.com;\r\nAuthentication-Results: mx.cloudflare.net;\r\n\tdkim=pass header.d=mydomain-dk.20230601.gappssmtp.com header.s=20230601 header.b=fBiU/QAD;\r\n\tdmarc=none header.from=mydomain.dk policy.dmarc=none;\r\n\tspf=none (mx.cloudflare.net: no SPF records found for [email protected]) smtp.helo=mail-ej1-x633.google.com;\r\n\tspf=softfail (mx.cloudflare.net: domain of [email protected] reports soft fail for 2a10:1350:4764:10::613) [email protected];\r\n\tarc=none smtp.remote-ip=2a10:1350:4764:10::613\r\nReceived: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-a68952bade2so38876966b.1\r\n        for <[email protected]>; Fri, 31 May 2024 08:03:03 -0700 (PDT)\r\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n        d=mydomain-dk.20230601.gappssmtp.com; s=20230601; t=1717167782; x=1717772582; darn=domain.dk;\r\n        h=mime-version:subject:references:in-reply-to:message-id:to:from:date\r\n         :from:to:cc:subject:date:message-id:reply-to;\r\n        bh=zwW2waXIcXpbAvwT3nXEjJq+LMrbkllviAT/UmsoKoE=;\r\n        b=fBiU/QADqteqioK7Dkh+e3iEkiU9oNqv5izgoCccfVaU3XmY2DmyXbOPIWXa4TrDyf\r\n         KEzwbVmBTjvbBjwkfZZUTCn/csDkCh0p94Lhv2mOlOcAdRjHEgCaWByC0Hdp/EW7IYIk\r\n         +bRn8xlObmHKZORlEUienfPBzcWI46nCxvhgsyQI3hTKfW9N41sF+HpvNIORg6/NgTdF\r\n         GDevI4dPqJ7TWbs8t9rcrHpuqvLQRKZtK1K0Bv0N8MllmnBfatfnwK18GVGT8s/YFlLD\r\n         4xcYqTyAF41lTOTS6M9Nkr6D+Ld1lW0gfNFL4rDfi+xR0a5z3qN/s7eqnzHu19ElQ/ow\r\n         A65A==\r\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n        d=1e100.net; s=20230601; t=1717167782; x=1717772582;\r\n        h=mime-version:subject:references:in-reply-to:message-id:to:from:date\r\n         :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;\r\n        bh=zwW2waXIcXpbAvwT3nXEjJq+LMrbkllviAT/UmsoKoE=;\r\n        b=qrBd73jf2jkKdo7mnaOWmfovhIc/mAcDATrT85F7CVtmQukYKvD4/ZeSPijkQZ2V10\r\n         vSjgTudeEF10qt1OUT8B/+tIzPb2uJU7W4lScabMQZMceX9UQTEiQboIBUDqmW4TsyRd\r\n         Rpb2jBqNjMFeIL2hwy0DW3/XtcVC+06BBKqWCAZxCz5aZDULIYMiBpFSBMrmzI5v7/Li\r\n         IgYGhZ7TeEmmSVYFcSMUsQbDGD7Idpq06jBDen4Z9q0Iah2EVuYW35R0FuFNAgjrHIvc\r\n         ir23UD+r6vVVRpUgngEY8ZiTQnDnpql7KSrbGVw/3A1+Z6496IkKwkFonxTCj9AzMDHz\r\n         fbNA==\r\nX-Gm-Message-State: AOJu0YyoVnPLlCbcc06MaOvgUyTKQwBxbyxI4yzlG+tjkGCMAddt8O6h\r\n\trj3lsHcTfHt2YO+rucIlknbhvIixOk+3hX+55emfr0ZCjHGFvY8ILqTSF47eROzwg4Q/cpdXxKC\r\n\tYwe0=\r\nX-Google-Smtp-Source: AGHT+IFfAfhje10Q6dl7yzw3JMb725P0k/WV3XphJovQq1v0AcxlSUTetf+4jlRMsH99rOgqE8Z0LA==\r\nX-Received: by 2002:a17:907:9957:b0:a59:bdb7:73ec with SMTP id a640c23a62f3a-a6822349583mr185293666b.72.1717167782328;\r\n        Fri, 31 May 2024 08:03:02 -0700 (PDT)\r\nReturn-Path: <[email protected]>\r\nReceived: from [192.168.39.136] (185-11-214-73.ip4.fiberby.net. [185.11.214.73])\r\n        by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a67e74f0931sm95799366b.81.2024.05.31.08.03.01\r\n        for <[email protected]>\r\n        (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);\r\n        Fri, 31 May 2024 08:03:01 -0700 (PDT)\r\nDate: Fri, 31 May 2024 17:02:54 +0200\r\nFrom: john erik <[email protected]>\r\nTo: [email protected]\r\nMessage-ID: <9f010e20-fe07-4733-a4c6-c7fb7320998d@Spark>\r\nIn-Reply-To: <c396e603-bbad-44a3-95a9-b21e22808cde@Spark>\r\nReferences: <c396e603-bbad-44a3-95a9-b21e22808cde@Spark>\r\nSubject: Re: Hejsa!\r\nX-Readdle-Message-ID: 9f010e20-fe07-4733-a4c6-c7fb7320998d@Spark\r\nMIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary=\"6659e6a5_37c3ab09_34b0\"\r\n\r\n--6659e6a5_37c3ab09_34b0\r\nContent-Type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: 7bit\r\nContent-Disposition: inline\r\n\r\nOn 31 May 2024 at 17.00 +0200, john erik <[email protected]>, wrote:\r\n> Hej med dig!\r\n\r\n--6659e6a5_37c3ab09_34b0\r\nContent-Type: text/html; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Disposition: inline\r\n\r\n<html xmlns=3D=22http://www.w3.org/1999/xhtml=22>\r\n<head>\r\n<title></title>\r\n</head>\r\n<body>\r\n<div name=3D=22messageReplySection=22>On 31 May 2024 at 17.00 +0200, Matt=\r\nias =46jellvang &lt;johnsioerik=40gmail.com&gt;, wrote:<br />\r\n<blockquote type=3D=22cite=22 style=3D=22border-left-color: grey; border-=\r\nleft-width: thin; border-left-style: solid; margin: 5px 5px;padding-left:=\r\n 10px;=22>\r\n<div name=3D=22messageBodySection=22>\r\n<div dir=3D=22auto=22>Hej med dig=21</div>\r\n</div>\r\n</blockquote>\r\n</div>\r\n</body>\r\n</html>\r\n\r\n--6659e6a5_37c3ab09_34b0--\r\n\r\n"

This is what I would expect:

Return-Path: <[email protected]>
Received: from [192.168.39.136] (185-11-214-73.ip4.fiberby.net. [185.11.214.73])
        by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a67e74f0931sm95799366b.81.2024.05.31.08.03.01
        for <[email protected]>
        (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
        Fri, 31 May 2024 08:03:01 -0700 (PDT)
Date: Fri, 31 May 2024 17:02:54 +0200
From: john erik <[email protected]>
To: [email protected]
Message-ID: <9f010e20-fe07-4733-a4c6-c7fb7320998d@Spark>
In-Reply-To: <c396e603-bbad-44a3-95a9-b21e22808cde@Spark>
References: <c396e603-bbad-44a3-95a9-b21e22808cde@Spark>
Subject: Re: Hejsa!
X-Readdle-Message-ID: 9f010e20-fe07-4733-a4c6-c7fb7320998d@Spark
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="6659e6a5_37c3ab09_34b0"

--6659e6a5_37c3ab09_34b0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 31 May 2024 at 17.00 +0200, john erik <[email protected]>, wrote:
> Hej med dig!

--6659e6a5_37c3ab09_34b0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

<html xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name=3D"messageReplySection">On 31 May 2024 at 17.00 +0200, john Fj=
ellvang &lt;[email protected]&gt;, wrote:<br />
<blockquote type=3D"cite" style=3D"border-left-color: grey; border-left-wid=
th: thin; border-left-style: solid; margin: 5px 5px;padding-left: 10px;">
<div name=3D"messageBodySection">
<div dir=3D"auto">Hej med dig!</div>
</div>
</blockquote>
</div>
</body>
</html>

--6659e6a5_37c3ab09_34b0--

Found: Email workers access to attachments, body or raw message - #11 by andris1