本文整理汇总了Python中email.Utils.getaddresses方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.getaddresses方法的具体用法?Python Utils.getaddresses怎么用?Python Utils.getaddresses使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.Utils
的用法示例。
在下文中一共展示了Utils.getaddresses方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addr_header_encode
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def addr_header_encode(text, header_name=None):
"""Encode and line-wrap the value of an email header field containing
email addresses."""
# Convert to unicode, if required.
if not isinstance(text, unicode):
text = unicode(text, "utf-8")
text = ", ".join(
formataddr((header_encode(name), emailaddr))
for name, emailaddr in getaddresses([text])
)
if is_ascii(text):
charset = "ascii"
else:
charset = "utf-8"
return Header(
text, header_name=header_name, charset=Charset(charset)
).encode()
示例2: forbid_multi_line_headers
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def forbid_multi_line_headers(name, val, encoding):
"""Forbids multi-line headers, to prevent header injection."""
encoding = encoding or settings.DEFAULT_CHARSET
val = force_unicode(val)
if '\n' in val or '\r' in val:
raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name))
try:
val = val.encode('ascii')
except UnicodeEncodeError:
if name.lower() in ADDRESS_HEADERS:
val = ', '.join(sanitize_address(addr, encoding)
for addr in getaddresses((val,)))
else:
val = str(Header(val, encoding))
else:
if name.lower() == 'subject':
val = Header(val)
return name, val
示例3: set_recipients
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def set_recipients(self, name, value):
self.unset_all(name)
for pair in getaddresses([value]):
self.add(name, formataddr(pair))
示例4: send
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def send(self, lines, to_addrs):
try:
if self.username or self.password:
self.smtp.login(self.username, self.password)
msg = "".join(lines)
# turn comma-separated list into Python list if needed.
if is_string(to_addrs):
to_addrs = [
email for (name, email) in getaddresses([to_addrs])
]
self.smtp.sendmail(self.envelopesender, to_addrs, msg)
except smtplib.SMTPResponseException:
err = sys.exc_info()[1]
self.environment.get_logger().error(
"*** Error sending email ***\n"
"*** Error %d: %s\n"
% (err.smtp_code, bytes_to_str(err.smtp_error))
)
try:
smtp = self.smtp
# delete the field before quit() so that in case of
# error, self.smtp is deleted anyway.
del self.smtp
smtp.quit()
except:
self.environment.get_logger().error(
"*** Error closing the SMTP connection ***\n"
"*** Exiting anyway ... ***\n"
"*** %s\n" % sys.exc_info()[1]
)
sys.exit(1)
示例5: test_getaddresses
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def test_getaddresses(self):
eq = self.assertEqual
eq(Utils.getaddresses(['aperson@dom.ain (Al Person)',
'Bud Person <bperson@dom.ain>']),
[('Al Person', 'aperson@dom.ain'),
('Bud Person', 'bperson@dom.ain')])
示例6: test_getaddresses_nasty
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def test_getaddresses_nasty(self):
eq = self.assertEqual
eq(Utils.getaddresses(['foo: ;']), [('', '')])
eq(Utils.getaddresses(
['[]*-- =~$']),
[('', ''), ('', ''), ('', '*--')])
eq(Utils.getaddresses(
['foo: ;', '"Jason R. Mastaler" <jason@dom.ain>']),
[('', ''), ('Jason R. Mastaler', 'jason@dom.ain')])
示例7: test_getaddresses_embedded_comment
# 需要导入模块: from email import Utils [as 别名]
# 或者: from email.Utils import getaddresses [as 别名]
def test_getaddresses_embedded_comment(self):
"""Test proper handling of a nested comment"""
eq = self.assertEqual
addrs = Utils.getaddresses(['User ((nested comment)) <foo@bar.com>'])
eq(addrs[0][1], 'foo@bar.com')