By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Sending email in utf-8?

P: n/a
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?

Nov 7 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"morphex" <mo*****@gmail.com> wrote:
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>

Nov 7 '05 #2

P: n/a
That works, kinda. I get strange characters now like this

"""
Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
Message-Id: <20*************************@thinkering.com>
To: mo****@nidelven-it.no, mo****@nidelven-it.no
From: mo****@nidelven-it.no
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: mo****@nidelven-it.no
Comments: asdf
"""

but at least it doesn't raise any errors.

Nov 7 '05 #3

P: n/a
morphex wrote:
That works, kinda. I get strange characters now like this

"""
Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
Message-Id: <20*************************@thinkering.com>
To: mo****@nidelven-it.no, mo****@nidelven-it.no
From: mo****@nidelven-it.no
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: mo****@nidelven-it.no
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
Nov 8 '05 #4

P: n/a
"morphex" <mo*****@gmail.com> wrote:
"""
Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
Message-Id: <20*************************@thinkering.com>
To: mo****@nidelven-it.no, mo****@nidelven-it.no
From: mo****@nidelven-it.no
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: mo****@nidelven-it.no
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...
name = "\xc3\xb8".decode("utf-8") print name
import unicodedata
unicodedata.name(name)

'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>

Nov 8 '05 #5

P: n/a
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!

Nov 10 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.