Re: Sending email in utf-8?
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! -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending email in utf-8?
"morphex" <[EMAIL PROTECTED]> wrote: > """ > Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST) > Message-Id: <[EMAIL PROTECTED]> > To: [EMAIL PROTECTED], [EMAIL PROTECTED] > From: [EMAIL PROTECTED] > Subject: Order confirmation > Content-Type: text/plain; charset="utf-8" > X-Bogosity: No, tests=bogofilter, spamicity=0.00, 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: [EMAIL PROTECTED] > 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 ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending email in utf-8?
morphex wrote: > 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: [EMAIL PROTECTED], [EMAIL PROTECTED] > From: [EMAIL PROTECTED] > Subject: Order confirmation > Content-Type: text/plain; charset="utf-8" > X-Bogosity: No, tests=bogofilter, spamicity=0.00, 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: [EMAIL PROTECTED] > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending email in utf-8?
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: [EMAIL PROTECTED], [EMAIL PROTECTED] From: [EMAIL PROTECTED] Subject: Order confirmation Content-Type: text/plain; charset="utf-8" X-Bogosity: No, tests=bogofilter, spamicity=0.00, 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: [EMAIL PROTECTED] Comments: asdf """ but at least it doesn't raise any errors. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending email in utf-8?
"morphex" <[EMAIL PROTECTED]> 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() -- http://mail.python.org/mailman/listinfo/python-list
Sending email in utf-8?
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? -- http://mail.python.org/mailman/listinfo/python-list