Sending email in utf-8?

M

morphex

Hi,

I have an email that's in the utf-8 encoding, and I'm getting this
error message when I try to send it using smtplib:

* Module smtplib, line 688, in sendmail
* Module smtplib, line 485, in data
* Module smtplib, line 312, in send
* Module socket, line 1, in sendall

UnicodeEncodeError: 'ascii' codec can't encode characters in position
263-264: ordinal not in range(128)

any suggestions on how I can approach this so that the email can be
sent without raising errors?
 
F

Fredrik Lundh

morphex said:
I have an email that's in the utf-8 encoding, and I'm getting this
error message when I try to send it using smtplib:

* Module smtplib, line 688, in sendmail
* Module smtplib, line 485, in data
* Module smtplib, line 312, in send
* Module socket, line 1, in sendall

UnicodeEncodeError: 'ascii' codec can't encode characters in position
263-264: ordinal not in range(128)

any suggestions on how I can approach this so that the email can be
sent without raising errors?

looks like you're confusing Unicode (character set) and UTF-8 (encoding).

smtplib only deals with 8-bit character streams; if you want to use a specific
encoding, you have to apply it yourself *before* you call the library:

HOST = "..."
FROM = "..."
TO = "..."
BODY = u"..."

server = smtplib.SMTP(HOST)
server.sendmail(FROM, [TO], BODY.encode("utf-8"))
server.quit()

</F>
 
M

morphex

That works, kinda. I get strange characters now like this

"""
Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
Message-Id: <[email protected]>
To: (e-mail address removed), (e-mail address removed)
From: (e-mail address removed)
Subject: Order confirmation
Content-Type: text/plain; charset="utf-8"
X-Bogosity: No, tests=bogofilter, spamicity=0.000000, version=0.92.8

Thank you for your order, it is copied here for your convenience, and
we will process it shortly.

Name: ø
Phone: ø
Email: (e-mail address removed)
Comments: asdf
"""

but at least it doesn't raise any errors.
 
M

Max M

morphex said:
That works, kinda. I get strange characters now like this

"""
Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
Message-Id: <[email protected]>
To: (e-mail address removed), (e-mail address removed)
From: (e-mail address removed)
Subject: Order confirmation
Content-Type: text/plain; charset="utf-8"
X-Bogosity: No, tests=bogofilter, spamicity=0.000000, version=0.92.8

Thank you for your order, it is copied here for your convenience, and
we will process it shortly.

Name: ø
Phone: ø
Email: (e-mail address removed)
Comments: asdf
"""

but at least it doesn't raise any errors.

This is a method I have clipped from one of my projects. It should
pretty much cover everything you would want to do while sending mails.


def Workgroup_mailFormAction(self, to=None, cc=None, bcc=None,
inReplyTo=None,
subject=None, body='',
attachments=None, mfrom=None,
REQUEST=None):
"""
Sends a message. Many of the input parameters are not currently
used,
but can be used for skinning the functionlity.
"""
site_encoding = self._site_encoding()
##################
# Create the message
msg = Message()
msg.set_payload(body, site_encoding)
#####################################
# if attachment, convert to multipart
# file fields are posted even if empty, so we need to remove
those :-s
if attachments is None:
attachments = []
attachments = [a for a in attachments if a]
if attachments:
mimeMsg = MIMEMultipart()
mimeMsg.attach(msg)
for attachment in attachments:
# Add the attachment
tmp = email.message_from_string(str(attachment.headers))
filename = tmp.get_param('filename', 'Attachment',
'Content-Disposition')
# clean up IE paths
filename = filename[max(filename.rfind('/'),
filename.rfind('\\'),
filename.rfind(':')
)+1:]
contentType = tmp['Content-Type']
attach_part = Message()
attach_part.add_header('Content-Type', contentType,
name=filename)
attach_part.add_header('Content-Disposition',
'attachment', filename=filename)
attach_part.set_payload(attachment.read())
Encoders.encode_base64(attach_part)
mimeMsg.attach(attach_part)
msg = mimeMsg
########################
# set headers on message
####
if to is None:
to = []
if mfrom:
to.append(mfrom)
msg['From'] = mfrom
msg['Reply-To'] = mfrom
to = ','.join(to)
if to: msg['To'] = to
####
if cc is None:
cc = []
cc = ','.join(cc)
if cc: msg['Cc'] = cc
####
if bcc is None:
bcc = []
bcc = ','.join(bcc)
if bcc: msg['Bcc'] = bcc
####
msg['Date'] = self.ZopeTime().rfc822() # needed by some servers
if inReplyTo:
msg['In-Reply-To'] = inReplyTo
msg['Subject'] = Header(subject, site_encoding)
##################
# Send the message
SMTPserver = self._mailhost()
success = 0
try:
cleaner = lambda adresses: [adress.strip() for adress in
adresses.split(',') if adress.strip()]
all_receivers = cleaner(to) + cleaner(cc) + cleaner(bcc)
all_receivers = list(set(all_receivers))
if all_receivers: # only send if any recipients
self._mailhost().send(str(msg), mto=all_receivers,
mfrom=mfrom)
success = 1
except:
pass


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 
F

Fredrik Lundh

morphex said:
"""
Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
Message-Id: <[email protected]>
To: (e-mail address removed), (e-mail address removed)
From: (e-mail address removed)
Subject: Order confirmation
Content-Type: text/plain; charset="utf-8"
X-Bogosity: No, tests=bogofilter, spamicity=0.000000, version=0.92.8

Thank you for your order, it is copied here for your convenience, and
we will process it shortly.

Name: ø
Phone: ø
Email: (e-mail address removed)
Comments: asdf
"""

that's 0xC3 0xB8 (at least according to my mail reader), which, translated
back from UTF-8, looks like a typically norsk character to me...
'LATIN SMALL LETTER O WITH STROKE'

try adding a "Mime-Version: 1.0" header to your mail.

a "Content-Transfer-Encoding: 8bit" might not hurt either (or run the body through
quopri.encodestring() after you've encoded it, and use "Content-Transfer-Encoding:
quoted-printable").

(also check the email package: http://docs.python.org/lib/module-email.html )

</F>
 
M

morphex

By turning everything into unicode objects (unicode(string)) and then
running body.encode('utf-8') and using quoted printable, it works.

Thanks for all the help, it's really appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,349
Members
48,035
Latest member
SamuelDieng

Latest Threads

Top