Sending email in utf-8?

Max M maxm at mxm.dk
Tue Nov 8 05:06:38 EST 2005


morphex wrote:
> That works, kinda.  I get strange characters now like this
> 
> """
> Date: Mon, 7 Nov 2005 11:38:29 -0700 (MST)
> Message-Id: <200511071838.jA7IcTKR095057 at thinkering.com>
> To: morten at nidelven-it.no, morten at nidelven-it.no
> From: morten at 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:  morten at 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



More information about the Python-list mailing list