fork download
  1. from email.parser import BytesParser
  2. from email.policy import default
  3.  
  4. mess = b'''\
  5. Delivered-To: ***@gmail.com
  6. Received: by 2002:ac9:1e03:0:0:0:0:0 with SMTP id r3csp1380999oci;
  7. Thu, 6 May 2021 03:24:14 -0700 (PDT)
  8. X-Google-Smtp-Source: ABdhPJxznI2eQK4UcAUk1vJbeKdYMovRwYMwxz4trgXWy7O+V1jklccpi92jvFWplswmqnBJfdpV
  9. X-Received: by 2002:a17:2:94:b9:ec:7fd5:193e with SMTP id w4-20020a1709029a84b02900ec7fd5193emr3638953plp.62.1620296654743;
  10. Thu, 06 May 2021 03:24:14 -0700 (PDT)
  11. ARC-Seal: i=1; a=rsa-sha256; t=1620296654; cv=none;
  12. d=google.com; s=arc-20160816;
  13. b=EH7EGZH8A3o9/LvvqIgO3KaZPU82Jn0iX0/kGV5W/tawujBF7y3qV3Er4lpFtX
  14. rm1jiy+cH3CPEHEiAyyd3XSuBZFA+AoE8xpoZxXaTxmqB6vBQXVWigVUUTKcsl71CSVs
  15. xLG7NHWsFABWEdemJY/cnibY85tpk1NpVISzDihAd4IShMKOGlYqoOlyWf06pdyIc2y6
  16. DZVYrlo/oWsnD2VT5nYiVqMeOwjUKIVg9ACyZIIRpmMQT/2/lutcsrLPMBBJbLK1vpgU
  17. jpZHu3s++EFPjmuTijNbyvv/5d5RrcsOwvLpWqk
  18. 8U1A==
  19. ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
  20. h=from:subject:sender:message-id:to:mime-version:date:dkim-signature;
  21. bh=oz3nVaiXvPhENQytolVf3ACAgfI2p8aslAq1BN/w55M=;
  22. b=fUeNvuOk3JjseXNpa+wFWtdmRjgG/Le5G62cV0ZMbelccGKi1H7GWx
  23. Exred4q9phvSSGV7ZuE+U5MXpwL1tXmPYZhHO+fj5uPEt6dY2x
  24. Yqg2/1IxDhcd/3NLH8CB19AolyRgAA8Qn+ThyBgpHs8mCVQ0f5XzxZvP/rKf
  25. WXxyQwA/1CcOPEcDlaOPAZNngacjvxeecjWWLrHUK1eH
  26. bETcDxabCPKXagRnP4xDXwTSqzj4Gtjsbc7 +v
  27. WGfw==
  28. ARC-Authentication-Results: i=1; mx.google.com;
  29. dkim=pass header.i=@mail.com header.s=mail header.b=LRDuMb9q;
  30. spf=pass (google.com: domain of ***@mail.com designates ****.137.**.*** as permitted sender) smtp.mailfrom=***@mail.com;
  31. dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=mail.com
  32. Return-Path: <***@mail.com>
  33. Received: from mail.com (f4mail-235-203. mail.com. [***.137.**.***])
  34. by mx.google.com with ESMTPS id l10si2328115pgb.331.2021.05.06.03.24.13
  35. for <xxxxx@gmail.com>
  36. (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);
  37. Thu, 06 May 2021 03:24:14 -0700 (PDT)
  38. Received-SPF: pass (google.com: domain of ***@mail.com designates ***.137.**.*** as permitted sender) client-ip=***.137.**.***;
  39. Authentication-Results: mx.google.com;
  40. dkim=pass header.i=@mail.com header.s=mail header.b=LRDuMb9q;
  41. spf=pass (google.com: domain of ***@mail.com designates ***.137.**.*** as permitted sender) smtp.mailfrom=***@mail.com;
  42. dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=mail.com
  43. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.com;
  44. s=mail; t=1620296652;
  45. bh=oz3nVaiXvPhENQytolVf3ACAgfI2p8aslAq1BN/w55M=;
  46. h=MIME-Version:From:Date:Message-ID:Subject:To:Content-Type;
  47. b=LRDuMb9qOWYq/u397M6T9zLkk1kInTolxD538xl5crHBsb3PL8eR5GiE0Deg7fTNe
  48. T8+whLVLTServKQLpxrEE3ob/6c5gr11SFYP8dIyzYU+qhbtxp6OJcAnBuxkJSRgRD
  49. JFQ/6oaHO49Jhz/2qkQ82USjrCi1fiAZe/mBKUGY=
  50. Received: (qmail 20965 invoked by uid 510); 6 May 2021 10:24:12 -0000
  51. x-m-msg: asd54ad564ad7aa6sd5as6d5; a6da7d6asas6dasd77; 5dad65ad5sd;
  52. X-OUT-VDRT-SpamState: 0\LEGIT
  53. X-OUT-VDRT-SpamScore: 0
  54. X-OUT-VDRT-SpamCause: gggruggvucftvghtrhhoucdtuddrgeduledrvdegtddgvdejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecufdftgfffkffhhfdpqfgfvfdfnecuuegrihhlohhuthemuceftddtnecunecujfgurhepffggvffkshfuhfgtsegrtderredttdejnecuhfhrohhmpedfrfhrihihrghnkhgrucffvghsrghifdcuoehpihihrgguvghsrghiudduuddusehrvgguihhffhhmrghilhdrtghomheqnecuggftrfgrthhtvghrnheptdefkeehkeduhfeljeelleehgefgffeutdeljedtiedtgeeigfdtjeettedvkedtnecukfhppedurddukeeirdduvdegrdduheeinecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmohguvgepshhmthhpohhuth
  55. X-Remote-IP: ***.137.**.***
  56. X-REDF-OSEN: ***@mail.com
  57. Date: 6 May 2021 10:24:12 -0000
  58. MIME-Version: 1.0
  59. To: "***" <***@gmail.com>
  60. Received: from unknown ***.137.**.*** by mail.com via HTTP; 06 May 2021 10:24:12 -0000
  61. X-Senderscore: D=0&S=0
  62. Message-ID: <1620296512.S.1386.3658.f4mail-***-13*.mail.com.1620296652.20941@webmail.mail.com>
  63. Sender: ***@mail.com
  64. Subject: =?utf-8?B?UmVxdWVzdGVkIGRvY3VtZW50cw==?=
  65. From: "Fisrtname Lastname" <***@mail.com>
  66. Content-Type: multipart/alternative;
  67. boundary="=_f619e79a5c2c1319e417d1bc96f343f8"
  68.  
  69. --=_f619e79a5c2c1319e417d1bc96f343f8
  70. Content-Transfer-Encoding: 7bit
  71. Content-Type: text/plain; charset="UTF-8"
  72.  
  73.  
  74.  
  75. Hey Name,
  76.  
  77. I hope you are fine and staying safe.
  78. &nbsp;Please find the attached document of some details for the whole process for this program.&nbsp; Once you go through it if you find interest to know then let&#39;s have a discussion.
  79.  
  80. Regards,
  81. Name LastName
  82. &nbsp;
  83. --=_f619e79a5c2c1319e417d1bc96f343f8
  84. Content-Transfer-Encoding: quoted-printable
  85. Content-Type: text/html; charset="UTF-8"
  86.  
  87. <br /><br />Hey Name,<br /><br />I hope you are fine and staying safe.<br /=
  88. >&nbsp;<div>Please find the attached document of some details for the whole=
  89. process for this program.&nbsp; Once you go through it if you find interes=
  90. t to know then let&#39;s have a discussion.<br /><br />Regards,<br />Name L=
  91. astName<br />&nbsp;</div><br>
  92. --=_f619e79a5c2c1319e417d1bc96f343f8--
  93. '''
  94.  
  95. headername = 'sender'
  96. headervalue = 'you need to show us your variables <failure@example.net>'
  97.  
  98. # you actually want 'rb' to read bytes
  99. # f = open('C:\\Users\\Sun\\Downloads\\Message.eml', 'r+')
  100. # header = Parser().parse(f)
  101. # ... you forgot to f.close() too
  102. # "headers" is misleading, this is the entire message
  103. message = BytesParser(policy=default).parsebytes(mess)
  104.  
  105. message.replace_header(headername, headervalue)
  106. message.replace_header('to', 'name@gmail.com')
  107.  
  108. message.add_attachment(b'\x00\x00\x00', 'attachment', 'pdf', filename='poop.pdf')
  109.  
  110. print(message.as_string())
  111.  
Success #stdin #stdout 0.08s 18424KB
stdin
Standard input is empty
stdout
Delivered-To: ***@gmail.com
Received: by 2002:ac9:1e03:0:0:0:0:0 with SMTP id r3csp1380999oci;
        Thu, 6 May 2021 03:24:14 -0700 (PDT)
X-Google-Smtp-Source:
 ABdhPJxznI2eQK4UcAUk1vJbeKdYMovRwYMwxz4trgXWy7O+V1jklccpi92jvFWplswmqnBJfdpV
X-Received: by 2002:a17:2:94:b9:ec:7fd5:193e with SMTP id
 w4-20020a1709029a84b02900ec7fd5193emr3638953plp.62.1620296654743;        Thu,
 06 May 2021 03:24:14 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1620296654; cv=none;
        d=google.com; s=arc-20160816;
        b=EH7EGZH8A3o9/LvvqIgO3KaZPU82Jn0iX0/kGV5W/tawujBF7y3qV3Er4lpFtX
         rm1jiy+cH3CPEHEiAyyd3XSuBZFA+AoE8xpoZxXaTxmqB6vBQXVWigVUUTKcsl71CSVs
         xLG7NHWsFABWEdemJY/cnibY85tpk1NpVISzDihAd4IShMKOGlYqoOlyWf06pdyIc2y6
         DZVYrlo/oWsnD2VT5nYiVqMeOwjUKIVg9ACyZIIRpmMQT/2/lutcsrLPMBBJbLK1vpgU
         jpZHu3s++EFPjmuTijNbyvv/5d5RrcsOwvLpWqk
         8U1A==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;
 s=arc-20160816;       
 h=from:subject:sender:message-id:to:mime-version:date:dkim-signature;       
 bh=oz3nVaiXvPhENQytolVf3ACAgfI2p8aslAq1BN/w55M=;       
 b=fUeNvuOk3JjseXNpa+wFWtdmRjgG/Le5G62cV0ZMbelccGKi1H7GWx        
 Exred4q9phvSSGV7ZuE+U5MXpwL1tXmPYZhHO+fj5uPEt6dY2x        
 Yqg2/1IxDhcd/3NLH8CB19AolyRgAA8Qn+ThyBgpHs8mCVQ0f5XzxZvP/rKf        
 WXxyQwA/1CcOPEcDlaOPAZNngacjvxeecjWWLrHUK1eH        
 bETcDxabCPKXagRnP4xDXwTSqzj4Gtjsbc7 +v         WGfw==
ARC-Authentication-Results: i=1; mx.google.com;       dkim=pass
 header.i=@mail.com header.s=mail header.b=LRDuMb9q;       spf=pass
 (google.com: domain of ***@mail.com designates ****.137.**.*** as permitted
 sender) smtp.mailfrom=***@mail.com;       dmarc=pass (p=QUARANTINE sp=REJECT
 dis=NONE) header.from=mail.com
Return-Path: <***@mail.com>
Received: from mail.com (f4mail-235-203. mail.com. [***.137.**.***])        by
 mx.google.com with ESMTPS id l10si2328115pgb.331.2021.05.06.03.24.13       
 for <xxxxx@gmail.com>        (version=TLS1_2
 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128);        Thu, 06 May 2021
 03:24:14 -0700 (PDT)
Received-SPF: pass (google.com: domain of ***@mail.com designates
 ***.137.**.*** as permitted sender) client-ip=***.137.**.***;
Authentication-Results: mx.google.com;       dkim=pass header.i=@mail.com
 header.s=mail header.b=LRDuMb9q;       spf=pass (google.com: domain of
 ***@mail.com designates ***.137.**.*** as permitted sender)
 smtp.mailfrom=***@mail.com;       dmarc=pass (p=QUARANTINE sp=REJECT
 dis=NONE) header.from=mail.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.com;
    s=mail; t=1620296652;
    bh=oz3nVaiXvPhENQytolVf3ACAgfI2p8aslAq1BN/w55M=;
    h=MIME-Version:From:Date:Message-ID:Subject:To:Content-Type;
    b=LRDuMb9qOWYq/u397M6T9zLkk1kInTolxD538xl5crHBsb3PL8eR5GiE0Deg7fTNe
     T8+whLVLTServKQLpxrEE3ob/6c5gr11SFYP8dIyzYU+qhbtxp6OJcAnBuxkJSRgRD
     JFQ/6oaHO49Jhz/2qkQ82USjrCi1fiAZe/mBKUGY=
Received: (qmail 20965 invoked by uid 510); 6 May 2021 10:24:12 -0000
x-m-msg: asd54ad564ad7aa6sd5as6d5; a6da7d6asas6dasd77; 5dad65ad5sd;
X-OUT-VDRT-SpamState: 0\LEGIT
X-OUT-VDRT-SpamScore: 0
X-OUT-VDRT-SpamCause: =?utf-8?q?gggruggvucftvghtrhhoucdtuddrgeduledrvdegtddg?=
 =?utf-8?q?vdejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecufdftgfffkffhhfdpq?=
 =?utf-8?q?fgfvfdfnecuuegrihhlohhuthemuceftddtnecunecujfgurhepffggvffkshfuhf?=
 =?utf-8?q?gtsegrtderredttdejnecuhfhrohhmpedfrfhrihihrghnkhgrucffvghsrghifdc?=
 =?utf-8?q?uoehpihihrgguvghsrghiudduuddusehrvgguihhffhhmrghilhdrtghomheqnecu?=
 =?utf-8?q?ggftrfgrthhtvghrnheptdefkeehkeduhfeljeelleehgefgffeutdeljedtiedtg?=
 =?utf-8?q?eeigfdtjeettedvkedtnecukfhppedurddukeeirdduvdegrdduheeinecuvehluh?=
 =?utf-8?q?hsthgvrhfuihiivgeptdenucfrrghrrghmpehmohguvgepshhmthhpohhuth?=
X-Remote-IP: ***.137.**.***
X-REDF-OSEN: ***@mail.com
Date: 6 May 2021 10:24:12 -0000
MIME-Version: 1.0
To: name@gmail.com
Received:
 from unknown ***.137.**.*** by mail.com via HTTP; 06 May 2021 10:24:12 -0000
X-Senderscore: D=0&S=0
Message-ID: =?utf-8?q?=3C1620296512=2ES=2E1386=2E3658=2Ef4mail-***-13*?=
 =?utf-8?q?=2Email=2Ecom=2E1620296652=2E20941=40webmail=2Email=2Ecom=3E?=
Sender: you need to show us your variables <failure@example.net>
Subject: =?utf-8?B?UmVxdWVzdGVkIGRvY3VtZW50cw==?=
From: "Fisrtname Lastname" <***@mail.com>
Content-Type: multipart/mixed; boundary="===============0343070542810758595=="

--===============0343070542810758595==
Content-Type: multipart/alternative;
    boundary="=_f619e79a5c2c1319e417d1bc96f343f8"

--=_f619e79a5c2c1319e417d1bc96f343f8
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="UTF-8"



Hey Name,

I hope you are fine and staying safe.
&nbsp;Please find the attached document of some details for the whole process for this program.&nbsp; Once you go through it if you find interest to know then let&#39;s have a discussion.

Regards,
Name LastName
&nbsp;
--=_f619e79a5c2c1319e417d1bc96f343f8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="UTF-8"

<br /><br />Hey Name,<br /><br />I hope you are fine and staying safe.<br /=
>&nbsp;<div>Please find the attached document of some details for the whole=
 process for this program.&nbsp; Once you go through it if you find interes=
t to know then let&#39;s have a discussion.<br /><br />Regards,<br />Name L=
astName<br />&nbsp;</div><br>
--=_f619e79a5c2c1319e417d1bc96f343f8--

--===============0343070542810758595==
Content-Type: attachment/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="poop.pdf"
MIME-Version: 1.0

AAAA

--===============0343070542810758595==--