我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用email.Utils()。
def parsedate(value): if value: value = decode_header(value) value = email.Utils.parsedate_tz(value) if isinstance(value, tuple): timestamp = time.mktime(tuple(value[:9])) if value[9]: timestamp -= time.timezone + value[9] if time.daylight: timestamp += 3600 return time.localtime(timestamp)
def data(self, args): """ Receive the remeining part of the e-mail (beside of the from: and to: received earlier, ie. the remaining header and the body part of the e-mail). A new received: header is added to the header. An optional return-path: header is added to the header. Finally, the e-mail is stored in the file system. """ import email.Utils global msgdir, receivedHeader self.mail.msg = ( args ) # call the mail handlers to process this message # TODO: specify the order of the handlers to be called # TODO: handle the returned and possible modified email try: msg = email.message_from_string(self.mail.msg) for h in mailHandlers: mailHandlers[h].handleMessage(msg, self.mail, self) except: mlog.logerr('Message handler caught exception: ' + str(sys.exc_info()[0]) +": " + str(sys.exc_info()[1])) # Get account data account = getMailAccount(self.mail.frm) if account == None: mlog.logerr('No account data found for ' + self.mail.frm) return # Add headers at the start! self.mail.msg = 'Received: (' + receivedHeader + ') ' + email.Utils.formatdate() + '\n' + self.mail.msg #self.mail.msg = ("From: %s\r\nTo: %s\r\n%s" % (self.mail.frm, ", ".join(self.mail.to), args)) if account.returnpath != None: self.mail.msg = 'Return-Path: ' + account.returnpath + '\n' + self.mail.msg # Save message try: (file, fn) = tempfile.mkstemp(suffix='.msg', dir=msgdir) pickle.dump(self.mail, os.fdopen(file, 'w')) except: mlog.logerr('Saving mail caught exception: ' + str(sys.exc_info()[0]) +": " + str(sys.exc_info()[1])) return mlog.log('Mail scheduled for sending (' + fn + ')')