This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: UTF-8 Email Header
Type: Stage: resolved
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, loewis, msladek
Priority: normal Keywords:

Created on 2012-02-18 07:23 by msladek, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg153634 - (view) Author: Michal Sladek (msladek) Date: 2012-02-18 07:23
Hello!

I am not a programmer so I appologize if I just don't understand the docs properly. I need to wirte a function which sends emails with utf-8 encoded subject and body. I tried something like this:

def sendMail (fromAddr, toAddr, subject, body = '', attachment = ''):
    message = email.mime.multipart.MIMEMultipart()
    message.add_header('From',fromAddr)
    message.add_header('To',toAddr)
    message['Subject'] = email.header.Header(subject,'utf-8')
    if (body != ''):
        msgPart = email.mime.text.MIMEText(body,'plain','utf-8')
        message.attach(msgPart)
    if (attachment != ''):
        if os.path.exists(attachment) == True:
            filename = attachment.rpartition(os.sep)[2]
            fp = open(attachment,'rb')
            msgPart = email.mime.base.MIMEBase('application','octet-stream')
            msgPart.set_payload(fp.read())
            fp.close()
            email.encoders.encode_base64(msgPart)
            msgPart.add_header('Content-Disposition','attachment',filename=filename)
            message.attach(msgPart)
    if smtpPort == 25:
        smtpCon = smtplib.SMTP(smtpSrv,smtpPort)
    else:
        smtpCon = smtplib.SMTP_SSL(smtpSrv,smtpPort)
    if (smtpUser != '') and (smtpPass != ''):
        smtpCon.login(smtpUser,smtpPass)
    smtpCon.send_message(message,mail_options=['UTF8SMTP','8BITMIME'])
    logger.info (_('Sent mail to: {0} with subject: {1}').format(toAddr,subject))
    smtpCon.quit()

I realized that adding email subject this way somehow brokes the message, so that the plain text body of the message is not visible on receiving side. I had to chnage the code like this:

    base64Subject = base64.b64encode(subject.encode('utf-8')).decode()
    encodedSubject = '=?UTF-8?B?{0}?='.format(base64Subject)
    message.add_header('Subject',encodedSubject)

Am I doing something wrong?
msg153672 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-19 08:31
Hello Michal.  This bug tracker is used to collect bug reports and feature requests, not for general support.  Please use the python-list mailing list, also known as the comp.lang.python newgroup, or #python on IRC (Freenode), or Stack Overflow, or any other discussion venue.  If you do find that the Python docs are erroneous, misleading or incomplete, then feel free to say so and we’ll reopen this report to see how we can improve the docs.  Cheers!
msg153675 - (view) Author: Michal Sladek (msladek) Date: 2012-02-19 08:45
Hello Eric!

I believe that there is a bug which prevents adding UTF-8 encoded
suject to multipart message properly. But because I am not a
programmer, I admin I might be wrong. So if you are a programmer and
you don't see any obvious mistake in my example code, open that bug
report again. Because in my opinion that code should work properly and
it does not.

Best regards

Michal

Dne 19. února 2012 9:31 Éric Araujo <report@bugs.python.org> napsal(a):
>
> Éric Araujo <merwok@netwok.org> added the comment:
>
> Hello Michal.  This bug tracker is used to collect bug reports and feature requests, not for general support.  Please use the python-list mailing list, also known as the comp.lang.python newgroup, or #python on IRC (Freenode), or Stack Overflow, or any other discussion venue.  If you do find that the Python docs are erroneous, misleading or incomplete, then feel free to say so and we’ll reopen this report to see how we can improve the docs.  Cheers!
>
> ----------
> nosy: +eric.araujo
> resolution:  -> invalid
> stage:  -> committed/rejected
> status: open -> closed
> type: behavior ->
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue14047>
> _______________________________________
msg153676 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-19 08:52
Sorry, I cannot right now take the time to go through the doc and your code.  Please use any of the friendly venues I listed in my previous message to get help on your code.
msg153679 - (view) Author: Michal Sladek (msladek) Date: 2012-02-19 09:11
I guess you don't understand me.

My script works properly. I don't need any help with it. OK? I have
found a workaround and I am happy with it for now. But I think, there
is a problem in current version of Python language.

Now, what should I do to report the bug with adding UTF-8 encoded
header? I described the problem, I submitted the example code. How can
I draw developers attention to the problem when you close the bug?

Dne 19. února 2012 9:52 Éric Araujo <report@bugs.python.org> napsal(a):
>
> Éric Araujo <merwok@netwok.org> added the comment:
>
> Sorry, I cannot right now take the time to go through the doc and your code.  Please use any of the friendly venues I listed in my previous message to get help on your code.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue14047>
> _______________________________________
msg153717 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-02-19 18:59
Michal, it's your wording of your report, and also some of the contents that caused Eric's reaction. When you conclude with "Am I doing something wrong?", then your message is *not* a bug report, but a question for help (namely, you are asking somebody to find out whether you did something wrong).

Please structure bug reports as follows:
1. this is what I did
2. this is what happened
3. this is what I expected to happen instead

Your report gives hints at 1, but neither discusses 2 or 3. I.e. you say what code you write, but you did not say (AFAICT) what parameters you passed to the function. And you did not say what happened when you passed the parameters.

This is important information, as the first step of us is to reproduce the bug report, i.e. try out all your steps and see whether the same happens also when we do it (which often enough is not the case).

Since this bug report is already filled with unrelated meta-discussion, please submit a new bug report in the style I explain above.
msg153767 - (view) Author: Michal Sladek (msladek) Date: 2012-02-20 08:08
Hello Martin!

Thanks for your kind words and for giving me hints how to fill the bug
report properly. I hope this time it will be accepted (ID 14062).

Best regards

Michal

Dne 19. února 2012 19:59 Martin v. Löwis <report@bugs.python.org> napsal(a):
>
> Martin v. Löwis <martin@v.loewis.de> added the comment:
>
> Michal, it's your wording of your report, and also some of the contents that caused Eric's reaction. When you conclude with "Am I doing something wrong?", then your message is *not* a bug report, but a question for help (namely, you are asking somebody to find out whether you did something wrong).
>
> Please structure bug reports as follows:
> 1. this is what I did
> 2. this is what happened
> 3. this is what I expected to happen instead
>
> Your report gives hints at 1, but neither discusses 2 or 3. I.e. you say what code you write, but you did not say (AFAICT) what parameters you passed to the function. And you did not say what happened when you passed the parameters.
>
> This is important information, as the first step of us is to reproduce the bug report, i.e. try out all your steps and see whether the same happens also when we do it (which often enough is not the case).
>
> Since this bug report is already filled with unrelated meta-discussion, please submit a new bug report in the style I explain above.
>
> ----------
> nosy: +loewis
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue14047>
> _______________________________________
msg153819 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-20 23:58
I had written a message which read much like Martin’s and wanted to reopen this report, but apparently my flaky connection did not let it go through and now it’s lost.  My apologies for not understanding your report for what it was, and thanks for persevering.
History
Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58255
2012-02-20 23:58:32eric.araujosetmessages: + msg153819
2012-02-20 08:08:50msladeksetmessages: + msg153767
2012-02-19 18:59:43loewissetnosy: + loewis
messages: + msg153717
2012-02-19 09:11:13msladeksetmessages: + msg153679
2012-02-19 08:52:04eric.araujosetmessages: + msg153676
2012-02-19 08:45:40msladeksetmessages: + msg153675
2012-02-19 08:31:41eric.araujosetstatus: open -> closed

type: behavior ->

nosy: + eric.araujo
messages: + msg153672
resolution: not a bug
stage: resolved
2012-02-18 07:23:28msladekcreate