473,938 Members | 38,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Email headers and non-ASCII characters

Hello, everyone...

I'm trying to send an email to people with non-ASCII characters in their
names. A recpient's address may look like:

"Jörg Nørgens" <joerg@nowher e>

My example code:

=============== =============== ===
def sendmail(sender , recipient, body, subject):
message = MIMEText(body)
message['Subject'] = Header(subject, 'iso-8859-1')
message['From'] = Header(sender, 'iso-8859-1')
message['To'] = Header(recipien t, 'iso-8859-1')

s = smtplib.SMTP()
s.connect()
s.sendmail(send er, recipient, message.as_stri ng())
s.close()
=============== =============== ===

However the Header() method encodes the whole expression in ISO-8859-1:

=?iso-8859-1?q?=22J=C3=B6r g_N=C3=B8rgens= 22_=3Cjoerg=40n owhere=3E?=

However I had expected something like:

"=?utf-8?q?J=C3=B6rg?= =?utf-8?q?_N=C3=B8rge ns?=" <joerg@nowher e>

Of course my mail transfer agent is not happy with the first string
although I see that Header() is just doing its job. I'm looking for a way
though to encode just the non-ASCII parts like any mail client does. Does
anyone have a recipe on how to do that? Or is there a method in
the "email" module of the standard library that does what I need? Or
should I split by regular expression to extract the email address
beforehand? Or a list comprehension to just look for non-ASCII character
and Header() them? Sounds dirty.

Hints welcome.

Regards
Christoph
Nov 23 '06 #1
4 2256
Christoph Haas skrev:
Hello, everyone...

I'm trying to send an email to people with non-ASCII characters in their
names. A recpient's address may look like:

"Jörg Nørgens" <joerg@nowher e>

My example code:

=============== =============== ===
def sendmail(sender , recipient, body, subject):
message = MIMEText(body)
message['Subject'] = Header(subject, 'iso-8859-1')
message['From'] = Header(sender, 'iso-8859-1')
message['To'] = Header(recipien t, 'iso-8859-1')

s = smtplib.SMTP()
s.connect()
s.sendmail(send er, recipient, message.as_stri ng())
s.close()
=============== =============== ===

However the Header() method encodes the whole expression in ISO-8859-1:

=?iso-8859-1?q?=22J=C3=B6r g_N=C3=B8rgens= 22_=3Cjoerg=40n owhere=3E?=

However I had expected something like:

"=?utf-8?q?J=C3=B6rg?= =?utf-8?q?_N=C3=B8rge ns?=" <joerg@nowher e>

Of course my mail transfer agent is not happy with the first string

Why offcourse? But it seems that you are passing the Header object a
utf-8 encoded string, not a latin-1 encoded.

You are telling the header the encoding. Not asking it to encode.

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Nov 23 '06 #2
On Thursday 23 November 2006 16:31, Max M wrote:
Christoph Haas skrev:
Hello, everyone...

I'm trying to send an email to people with non-ASCII characters in
their names. A recpient's address may look like:

"Jörg Nørgens" <joerg@nowher e>

My example code:

=============== =============== ===
def sendmail(sender , recipient, body, subject):
message = MIMEText(body)
message['Subject'] = Header(subject, 'iso-8859-1')
message['From'] = Header(sender, 'iso-8859-1')
message['To'] = Header(recipien t, 'iso-8859-1')

s = smtplib.SMTP()
s.connect()
s.sendmail(send er, recipient, message.as_stri ng())
s.close()
=============== =============== ===

However the Header() method encodes the whole expression in
ISO-8859-1:

=?iso-8859-1?q?=22J=C3=B6r g_N=C3=B8rgens= 22_=3Cjoerg=40n owhere=3E?=

However I had expected something like:

"=?utf-8?q?J=C3=B6rg?= =?utf-8?q?_N=C3=B8rge ns?=" <joerg@nowher e>

Of course my mail transfer agent is not happy with the first string

Why offcourse?
Because my MTA doesn't care about MIME. It just transports the email. And
it expects an email address in <...but doesn't decode =?iso...? strings.
But it seems that you are passing the Header object a
utf-8 encoded string, not a latin-1 encoded.
You are telling the header the encoding. Not asking it to encode.
Uhm, okay. Let's see:

u'"Jörg Nørgens" <joerg@nowhere> '.encode('latin-1')

='"J\xc3\xb6r g N\xc3\xb8rgens" <joerg@nowhere> '

So far so good. Now run Header() on it:

='=?utf-8?b?IkrDtnJnIE7 DuHJnZW5zIiA8am 9lcmdAbm93aGVyZ T4=?='

Still nothing like <...in it and my MTA is unhappy again. What am I
missing? Doesn't anyone know how mail clients handle that encoding?

Desperately,
Christoph
Nov 24 '06 #3

Christoph Haas wrote:
Hello, everyone...

I'm trying to send an email to people with non-ASCII characters in their
names. A recpient's address may look like:

"Jörg Nørgens" <joerg@nowher e>

My example code:

=============== =============== ===
def sendmail(sender , recipient, body, subject):
message = MIMEText(body)
message['Subject'] = Header(subject, 'iso-8859-1')
message['From'] = Header(sender, 'iso-8859-1')
message['To'] = Header(recipien t, 'iso-8859-1')

s = smtplib.SMTP()
s.connect()
s.sendmail(send er, recipient, message.as_stri ng())
s.close()
=============== =============== ===

However the Header() method encodes the whole expression in ISO-8859-1:

=?iso-8859-1?q?=22J=C3=B6r g_N=C3=B8rgens= 22_=3Cjoerg=40n owhere=3E?=

However I had expected something like:

"=?utf-8?q?J=C3=B6rg?= =?utf-8?q?_N=C3=B8rge ns?=" <joerg@nowher e>

Of course my mail transfer agent is not happy with the first string
although I see that Header() is just doing its job. I'm looking for a way
though to encode just the non-ASCII parts like any mail client does. Does
anyone have a recipe on how to do that? Or is there a method in
the "email" module of the standard library that does what I need? Or
should I split by regular expression to extract the email address
beforehand? Or a list comprehension to just look for non-ASCII character
and Header() them? Sounds dirty.
Why dirty?

from email.Header import Header
from itertools import groupby
h = Header()
addr = u'"Jörg Nørgens" <joerg@nowhere> '
def is_ascii(char):
return ord(char) < 128
for ascii, group in groupby(addr, is_ascii):
h.append(''.joi n(group),"latin-1")

print h
=>
"J =?iso-8859-1?q?=F6?= rg N =?iso-8859-1?q?=F8?= rgens"
<joerg@nowher e>

-- Leo

Nov 24 '06 #4
Christoph Haas skrev:
On Thursday 23 November 2006 16:31, Max M wrote:
>Christoph Haas skrev:
>>Hello, everyone...

I'm trying to send an email to people with non-ASCII characters in
their names. A recpient's address may look like:

"Jörg Nørgens" <joerg@nowher e>

My example code:

============= =============== =====
def sendmail(sender , recipient, body, subject):
message = MIMEText(body)
message['Subject'] = Header(subject, 'iso-8859-1')
message['From'] = Header(sender, 'iso-8859-1')
message['To'] = Header(recipien t, 'iso-8859-1')

s = smtplib.SMTP()
s.connect()
s.sendmail(send er, recipient, message.as_stri ng())
s.close()
============= =============== =====

However the Header() method encodes the whole expression in
ISO-8859-1:

=?iso-8859-1?q?=22J=C3=B6r g_N=C3=B8rgens= 22_=3Cjoerg=40n owhere=3E?=

However I had expected something like:

"=?utf-8?q?J=C3=B6rg?= =?utf-8?q?_N=C3=B8rge ns?=" <joerg@nowher e>

Of course my mail transfer agent is not happy with the first string
Why offcourse?

Because my MTA doesn't care about MIME. It just transports the email. And
it expects an email address in <...but doesn't decode =?iso...? strings.
>But it seems that you are passing the Header object a
utf-8 encoded string, not a latin-1 encoded.
You are telling the header the encoding. Not asking it to encode.

Uhm, okay. Let's see:

u'"Jörg Nørgens" <joerg@nowhere> '.encode('latin-1')

='"J\xc3\xb6r g N\xc3\xb8rgens" <joerg@nowhere> '

So far so good. Now run Header() on it:

='=?utf-8?b?IkrDtnJnIE7 DuHJnZW5zIiA8am 9lcmdAbm93aGVyZ T4=?='

Still nothing like <...in it and my MTA is unhappy again. What am I
missing? Doesn't anyone know how mail clients handle that encoding?
>>address = u'"Jörg Nørgens" <joerg@nowhere> '.encode('latin-1')
address
'"J\xf6rg N\xf8rgens" <joerg@nowhere> '
>>from email.Header import Header
hdr = str(Header(addr ess, 'latin-1'))
hdr
'=?iso-8859-1?q?=22J=F6rg_N =F8rgens=22_=3C joerg=40nowhere =3E?='

Is this not correct?

At least roundtripping works:
>>from email.Header import decode_header
encoded, coding = decode_header(h dr)[0]
encoded, coding
('"J\xf6rg N\xf8rgens" <joerg@nowhere> ', 'iso-8859-1')
>>encoded.decod e(coding)
u'"J\xf6rg N\xf8rgens" <joerg@nowhere> '

And parsing the address works too.
>>from email.Utils import parseaddr
parseaddr(enc oded.decode(cod ing))
(u'J\xf6rg N\xf8rgens', u'joerg@nowhere ')
>>>
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Nov 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4352
by: Ann | last post by:
Hi, I am trying to send a html email from a php script. This script emails a common information to all the members in the database. The only problem is I cannot specify colors, hyperlinks etc..Html tags like <h1></h1>, <br/>, <b> etc works though.. Could any one tell me what i might be doing wrong? Any help will be greatly appreciated.
3
1687
by: Dave Smithz | last post by:
Hi there, Previously in my PHP code I was using Sendmail to send out emails to people in the MySQL DB. The requirement cam along to be able to send attachments and so I dug around for an easy bit of code that did not need classes and I could plug easily into my PHP code. I thought I found one (see bottom of email), however I have since discovered that as it is, this script has the following disadvantages when compared to using...
71
4264
by: Christopher Benson-Manica | last post by:
At what point was the .h dropped from the STL headers? I just had a discussion yesterday with my boss, who said he wanted .h on all the STL includes, despite me protesting that it was not standard... -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
11
1921
by: BilfFord X | last post by:
If I # include "text.h" , where text.h consists of the following: # define NOTHING_IMPORTANT # include <stdio.h> , am I writing c++? cordially, bfx
0
874
by: alanwo | last post by:
Hi Experts, I am trying to send out email and bounce back to different from "FROM" email address: Dim webmail As New System.Net.Mail.MailMessage("B@FROM.com", "ghyddfhg@gmail.com", "hello", "return errors to") 'webmail.Headers.Add("Errors-To", "a1@a1.com") 'webmail.Headers.Add("X-Sender", "a1@a1.com") webmail.Headers.Add("Sender", "a1@a1.com")
4
449
by: lucavilla | last post by:
If you go to http://europe.nokia.com/A4305060, fill the "Enter your product code:" field with the value "0523183" and press "Go" (the ending page URL varies because there's a variable session-ID in the URL-link associated to "Go") you will obtain this string: "Version: RM43_V1.10.030" Is it possible to have a string.php page that just display this string? how can I do it?
7
3573
by: geek7 | last post by:
OK, I'm trying to create an email and when it sends the email, it mostly works except that there are '!' inserted at some spots. Checking the html source of the email, looks like they are placed at the end of long lines. I'm assuming there is some issue with the encoding, but searching through groups, nothing I've tried has worked. Can someone let me know if I should be doing something differently with my headers for an html email?...
5
3957
by: Dave | last post by:
Hi All, I'm experiencing problems with sending mail using mail() to forwarded email accounts. The problem seems to revolve around the optional 4th argument of mail(), namely 'additional headers'. It seems that when I populate this 4th argument of mail(), any emails sent to an email forwarding service email address (ukreg/fasthosts actually) fail to reach their destination. Direct email addresses seem to work fine.
4
1866
by: sadieslc | last post by:
I'm working on a PHP script, and the info from the form shows up in the headers of the email that I receive, but it doesn't show up in the body of the email. Can you please help me figure out what I'm doing wrong? Here is the script: <? function send_mail($emailaddress, $fromaddress, $emailsubject, $body) { $eol="\r\n"; $mime_boundary=md5(time()); # Common Headers
2
4305
by: Erik Witkop | last post by:
So I have been trying to get this to work all day. I can't get a local file on my web server to attach to an email. Right now I have it printing out in the body of the email. Please help me with any thouhgts on how to get it in as an attachment. CODE: <?php ini_set(SMTP, "172.18.1.65");
0
10131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10652
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9854
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6072
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.