1

I'm trying to parse an incomming email from sendgrid

I get the email string with:

 data = request.stream.read()

In data I have this string. Now I would like to parse this string.

data = '--xYzZY\r\nContent-Disposition: form-data; name="dkim"\r\n\r\n{@gmail.com : pass}\r\n--xYzZY\r\nContent-Disposition: form-data; name="email"\r\n\r\nReceived: by mx0054p1mdw1.sendgrid.net with SMTP id B3Fk46AV4U Sun, 02 Feb 2020 13:10:26 +0000 (UTC)\nReceived: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by mx0054p1mdw1.sendgrid.net (Postfix) with ESMTPS id E90096E2923 for <test@test.lst-beboerforening.dk>; Sun,  2 Feb 2020 13:10:25 +0000 (UTC)\nReceived: by mail-qt1-f170.google.com with SMTP id l19so9301147qtq.8 for <test@test.lst-beboerforening.dk>; Sun, 02 Feb 2020 05:10:26 -0800 (PST)\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=/D69TJlEa2pkNFb2oWCUsdUIyscZhErPeGaknGwI23w=; b=jz0dG7/84of+e/mle7rItB8YQdmF0Y5ed2iMspiF142UwWygN0NczEiM3uQjXDJ7+R ZxkpqAinhdv9A+iWhkpPJ+1/Rxw/CORd0pkTc6mdtR15t5zj7OBPLIHskAx29R53t8J2 XSQv+zlEXy1jn3/H2C+4iytI9NVf0/3gBdB4ybU2EyxDFM9pTuLnEmmax95/t8NBcfly aUgPGwCi60yNoVeP/rSOLsSLKKGeMA3yD3LmTNhKk+QCVzo1Cth6ycVgZ7Coby+eL9tz JpxJFxf+/pCym6zOaEgn6d7HiyqNAbF8r8CQd27jrnpHiTBjJ0Y7B1oy914gnkQagEI7 9sYw==\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=/D69TJlEa2pkNFb2oWCUsdUIyscZhErPeGaknGwI23w=; b=s8VehBi0IIfndNJp8Shn/pWNI/Ny80s0b+a51OUkuOIFUe6wiUXNxb2bH9NfcSQkpn MwUIDpOkb0FAFZzQLfqAeIWlasF3OfSKzAPrac3Ec99DO74Kl88mI+eGXxCbthqi4jHA KX/QVRlq5FIXgT8LEDAlO+z0gvOhLDE/Glh6OLGcF21//tM7mPVUBteotreLKgUmXiKx hmBjqc/1GdO7l9t3PNvv5JrBRcFT4Kruq+eMAyjiI0UkEY4zv0CkN7BcezrOh0kgn4CN PtxRsC7OIP62y+ZkYfuSpQrpIp6ygd0IgFEWGTIaRDKyi/8b4JUeyDzuQmpDA98+DA3q T1kg==\nX-Gm-Message-State: APjAAAUkAwiBFAqcpNKc+cPDtGpzS9ydOtqLhAhr1SPeXYczFyMLiql8 BvT64tY8KNBLoQS2Snnz8hfvKe8TpkxgRPoP2N2VUvid\nX-Google-Smtp-Source: APXvYqyntGIMp229ZSSPrcmhlsGAdWuEVI+k6H/L0LgcxGuVI8uKjN7sBvYxEvFlbzYPLAKzf00JF+mhC4KHNUjDJWo=\nX-Received: by 2002:ac8:4257:: with SMTP id r23mr19290100qtm.126.1580649025317; Sun, 02 Feb 2020 05:10:25 -0800 (PST)\nMIME-Version: 1.0\nFrom: Kresten Skovsted Buch <krestensb@gmail.com>\nDate: Sun, 2 Feb 2020 14:10:17 +0100\nMessage-ID: <CAJY4yqJdXQfVd3CdoWuVTQR27gFw3WGXgFJWV-GXpdygbeDZpQ@mail.gmail.com>\nSubject: fsdf\nTo: test@test.lst-beboerforening.dk\nContent-Type: multipart/alternative; boundary="000000000000ab2e1f059d978757"\n\n--000000000000ab2e1f059d978757\nContent-Type: text/plain; charset="UTF-8"\nContent-Transfer-Encoding: quoted-printable\n\nVenlig hilsen\nKresten Skovsted Buch\nTelefon 61 67 26 55\nSe min profil p=C3=A5 Linkedin <https://www.linkedin.com/in/krestenskovsted=\nbuch/>\n\n--000000000000ab2e1f059d978757\nContent-Type: text/html; charset="UTF-8"\nContent-Transfer-Encoding: quoted-printable\n\n<div dir=3D"ltr"><br clear=3D"all"><div><div dir=3D"ltr" class=3D"gmail_sig=\nnature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=\n=3D"ltr"><div><div dir=3D"ltr"><div>Venlig hilsen<br>Kresten Skovsted Buch<=\n/div><div>Telefon 61 67 26 55<br></div><div><a href=3D"https://www.linkedin=\n.com/in/krestenskovstedbuch/" target=3D"_blank">Se min profil p=C3=A5 Linke=\ndin</a></div></div></div></div></div></div></div></div></div>\n\n--000000000000ab2e1f059d978757--\n\r\n--xYzZY\r\nContent-Disposition: form-data; name="to"\r\n\r\ntest@test.lst-beboerforening.dk\r\n--xYzZY\r\nContent-Disposition: form-data; name="from"\r\n\r\nKresten Skovsted Buch <krestensb@gmail.com>\r\n--xYzZY\r\nContent-Disposition: form-data; name="sender_ip"\r\n\r\n209.85.160.170\r\n--xYzZY\r\nContent-Disposition: form-data; name="envelope"\r\n\r\n{"to":["test@test.lst-beboerforening.dk"],"from":"krestensb@gmail.com"}\r\n--xYzZY\r\nContent-Disposition: form-data; name="subject"\r\n\r\nfsdf\r\n--xYzZY\r\nContent-Disposition: form-data; name="charsets"\r\n\r\n{"to":"UTF-8","subject":"UTF-8","from":"UTF-8"}\r\n--xYzZY\r\nContent-Disposition: form-data; name="SPF"\r\n\r\npass\r\n--xYzZY--\r\n'

So I do:

import email
x = email.message_from_string(data)
f = x['from']
t =x['to']

But I get nothing, f and t are empty.

Kresten
  • 810
  • 13
  • 36

2 Answers2

1

The data variable contains a MIME structure with multiple parts. The second of these is the actual email source.

The other parts contain metainformation about the message delivery etc, presumably Sendgrid has documentation about this format and how you should handle it.

https://stackoverflow.com/a/58646441/874188 suggests adding a faux MIME header up top and using the email library to unwrap the form data. This feels a bit silly, but should not be hard to do.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

It looks like you have your SendGrid Parse API configured for "raw". If you want the parsed body, you should get it parsed instead.

jacobmovingfwd
  • 2,185
  • 14
  • 11