我们从Python开源项目中,提取了以下41个代码示例,用于说明如何使用email.header.make_header()。
def test_rfc2047_multiline(self): eq = self.assertEqual s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""" dh = decode_header(s) eq(dh, [ (b'Re:', None), (b'r\x8aksm\x9arg\x8cs', 'mac-iceland'), (b'baz foo bar', None), (b'r\x8aksm\x9arg\x8cs', 'mac-iceland')]) header = make_header(dh) eq(str(header), 'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s') self.ndiffAssertEqual(header.encode(maxlinelen=76), """\ Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?= =?mac-iceland?q?=9Arg=8Cs?=""")
def test_rfc2047_multiline(self): eq = self.assertEqual s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""" dh = decode_header(s) eq(dh, [ (b'Re: ', None), (b'r\x8aksm\x9arg\x8cs', 'mac-iceland'), (b' baz foo bar ', None), (b'r\x8aksm\x9arg\x8cs', 'mac-iceland')]) header = make_header(dh) eq(str(header), 'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s') self.ndiffAssertEqual(header.encode(maxlinelen=76), """\ Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?= =?mac-iceland?q?=9Arg=8Cs?=""")
def test_rfc2047_multiline(self): eq = self.assertEqual s = """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""" dh = decode_header(s) eq(dh, [ ('Re:', None), ('r\x8aksm\x9arg\x8cs', 'mac-iceland'), ('baz foo bar', None), ('r\x8aksm\x9arg\x8cs', 'mac-iceland')]) eq(str(make_header(dh)), """Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""")
def test_whitespace_eater_unicode_2(self): eq = self.assertEqual s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?=' dh = decode_header(s) eq(dh, [('The', None), ('quick brown fox', 'iso-8859-1'), ('jumped over the', None), ('lazy dog', 'iso-8859-1')]) hu = make_header(dh).__unicode__() eq(hu, u'The quick brown fox jumped over the lazy dog')
def test_us_ascii_header(self): eq = self.assertEqual s = 'hello' x = decode_header(s) eq(x, [('hello', None)]) h = make_header(x) eq(s, h.encode())
def test_whitespace_eater(self): eq = self.assertEqual s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.' parts = decode_header(s) eq(parts, [('Subject:', None), ('\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), ('zz.', None)]) hdr = make_header(parts) eq(hdr.encode(), 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.')
def get_header_value(msg, name): "Handle header decoding and return a string we examine." return str(make_header(decode_header(msg.get(name, ''))))
def test_whitespace_eater_unicode_2(self): eq = self.assertEqual s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?=' dh = decode_header(s) eq(dh, [(b'The', None), (b'quick brown fox', 'iso-8859-1'), (b'jumped over the', None), (b'lazy dog', 'iso-8859-1')]) hu = str(make_header(dh)) eq(hu, 'The quick brown fox jumped over the lazy dog')
def test_encoded_adjacent_nonencoded(self): eq = self.assertEqual h = Header() h.append('hello', 'iso-8859-1') h.append('world') s = h.encode() eq(s, '=?iso-8859-1?q?hello?= world') h = make_header(decode_header(s)) eq(h.encode(), s)
def test_whitespace_eater(self): eq = self.assertEqual s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.' parts = decode_header(s) eq(parts, [(b'Subject:', None), (b'\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), (b'zz.', None)]) hdr = make_header(parts) eq(hdr.encode(), 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.')
def as_envelope(self, must_raise=True): envelope = Envelope() headers_filters.process( self.headers, self, settings.TRANSACTIONAL['HEADERS_FILTERS_PARAMS']) headers = "" for key, value in self.headers.items(): if key in ('Subject', ): header = make_header( decode_header(value), header_name=key, maxlinelen=78) value = header.encode(linesep='\r\n') headers += "{}: {}\n".format(key, value) message = "" if self.message: message = self.message.content or "" if self.message is None and must_raise: raise Exception( "Can't build this envelope because " "there is no RawMail attached to it.") envelope.parse(headers.encode('utf-8') + message.encode('utf-8')) envelope.sender = self.sender envelope.recipients.append(self.recipient) return envelope
def masseges(self): msg = MIMEMultipart() msg['Message-ID'] = make_msgid() msg['Date'] = formatdate(localtime=True) msg['From'] = make_header([('{}'.format(self.from_text), 'UTF-8'), ('<' + self.from_email + '>', 'us-ascii')]) msg['To'] = self.to msg['Subject'] = self.subject msg['User-Agent'] = 'Horde Application Framework 5' msg['Content-Disposition'] = "inline" msg['Content-Transfer-Encoding'] = "8bit" msg.attach(MIMEText(self.message_text, _subtype='html', _charset='utf-8')) return(msg)
def test_whitespace_keeper_unicode_2(self): eq = self.assertEqual s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?=' dh = decode_header(s) eq(dh, [(b'The ', None), (b'quick brown fox', 'iso-8859-1'), (b' jumped over the ', None), (b'lazy dog', 'iso-8859-1')]) hu = str(make_header(dh)) eq(hu, 'The quick brown fox jumped over the lazy dog')
def test_rfc2047_rfc2047_7(self): # 7th testcase at end of rfc2047 s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=)' self.assertEqual(decode_header(s), [(b'(', None), (b'a', 'iso-8859-1'), (b' b', 'iso-8859-2'), (b')', None)]) self.assertEqual(make_header(decode_header(s)).encode(), s.lower()) self.assertEqual(str(make_header(decode_header(s))), '(a b)')