S
S
Stanislav2015-04-10 21:39:24
Python
Stanislav, 2015-04-10 21:39:24

How to edit mail header (mail headers) in Python?

Good afternoon!
I need to periodically send and receive special emails with different headers to test the system. To accomplish this task, I use smtplib and MIMEText with the encode_quopri encoder.

import smtplib
from email.mime.text import MIMEText
from email.encoders import encode_quopri
SERVER = 'mailserver.loc'
FROM = '[email protected]' + SERVER
TO = '[email protected]' + SERVER
msg = MIMEText('проверка проверка проверка', 'plain', _charset='windows-1251')
msg['Subject'] = 'subject'
msg['From'] = FROM
msg['To'] = TO
msg['X-Message-id'] = 'xmsg_id'
encode_quopri(msg)

print (msg)

At the output I get:
Content-Type: text/plain; charset="windows-1251"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Subject: subject
From: [email protected]
To: [email protected]
X-Message-id: xmsg_id
Content-Transfer-Encoding: quoted-printable

=EF=F0=EE=E2=E5=F0=EA=E0=20=EF=F0=EE=E2=E5=F0=EA=E0=20=EF=F0=EE=E2=E5=F0=EA=
=E0

But my title is doubled:
Content-Transfer-Encoding: base64
...
Content-Transfer-Encoding: quoted-printable

I tried to remove the first one, but then the quoted-printable breaks and the result is:
7/Du4uXw6uAg7/Du4uXw6uAg7/Du4uXw6uA=3D
My goal is to get this title:
Date: Wed, 1 Jun 2011 11:25:32 +0400
From: Billinfo <[email protected]>
X-Priority: 3 (Normal)
Message-ID: <878185321.20110601112532>
To: [email protected]
Subject: Just a subject
Resent-from: Billinfo <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----------115EA32B5A1C2"
Resent-Message-Id: <20110601072537.A8F1A3C14A>
Resent-Date: Wed,  1 Jun 2011 11:25:37 +0400 (MSD)

------------115EA32B5A1C2
Content-Type: text/plain; charset=windows-1251
Content-Transfer-Encoding: quoted-printable

=EF=F0=EE=E2=E5=F0=EA=E0=20=EF=F0=EE=E2=E5=F0=EA=E0=20=EF=F0=EE=E2=E5=F0=EA=
=E0
------------115EA32B5A1C2
Content-Type: APPLICATION/VND.MS-EXCEL;
name="doc.xls"
Content-transfer-encoding: base64
Content-Disposition: attachment;
filename="doc.xls"

some base64 content
------------115EA32B5A1C2--

I would be grateful for any help in resolving my issue!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav, 2015-04-11
@Amelinium

Decision:

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.charset import Charset
from email.encoders import encode_quopri

SERVER = 'mailserver.loc'
FROM = '[email protected]' + SERVER
TO = '[email protected]' + SERVER

msg = MIMEMultipart('mixed')
msg['Subject'] = 'subject'
msg['From'] = FROM
msg['To'] = TO
msg['X-Message-id'] = 'xmsg_id'


charset = Charset('windows-1251')

att1 = MIMEText('проверка111 проверка проверка', 'plain', _charset=None)
encode_quopri(att1)
att1.set_charset(charset)
del att1['Content-Transfer-Encoding']
del att1['MIME-Version']
att1['Content-Transfer-Encoding'] = 'quoted-printable'


msg.attach(att1)

print (msg.as_string())

################### OUTPUT ###################
# Content-Type: multipart/mixed; boundary="===============2009055196=="
# MIME-Version: 1.0
# Subject: subject
# From: [email protected]
# To: [email protected]
# X -Message-id: xmsg_id
# --===============2009055196==
# Content-Type: text/plain; charset="windows-1251"
# Content-Transfer-Encoding: quoted-printable
# =D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0111 =20=D0=BF=D1=80=D0=BE=D0=
# =B2=D0=B5=D1=80=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE =D0=B2=D0=B5=D1=80=D0=BA=D0=
# =B0
# --===============2009055196==--

A
Alexander, 2015-04-11
@NeiroNx

Use normal tcp sockets - and send whatever you want to the server. The library will only get in the way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question