本文整理汇总了Python中email.message.EmailMessage.as_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python EmailMessage.as_bytes方法的具体用法?Python EmailMessage.as_bytes怎么用?Python EmailMessage.as_bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.message.EmailMessage
的用法示例。
在下文中一共展示了EmailMessage.as_bytes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import as_bytes [as 别名]
def main():
parser = ArgumentParser(description="""\
Send the contents of a directory as a MIME message.
Unless the -o option is given, the email is sent by forwarding to your local
SMTP server, which then does the normal delivery process. Your local machine
must be running an SMTP server.
""")
parser.add_argument('-d', '--directory',
help="""Mail the contents of the specified directory,
otherwise use the current directory. Only the regular
files in the directory are sent, and we don't recurse to
subdirectories.""")
parser.add_argument('-o', '--output',
metavar='FILE',
help="""Print the composed message to FILE instead of
sending the message to the SMTP server.""")
parser.add_argument('-s', '--sender', required=True,
help='The value of the From: header (required)')
parser.add_argument('-r', '--recipient', required=True,
action='append', metavar='RECIPIENT',
default=[], dest='recipients',
help='A To: header value (at least one required)')
args = parser.parse_args()
directory = args.directory
if not directory:
directory = '.'
# Create the message
msg = EmailMessage()
msg['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
msg['To'] = ', '.join(args.recipients)
msg['From'] = args.sender
msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
if not os.path.isfile(path):
continue
# Guess the content type based on the file's extension. Encoding
# will be ignored, although we should check for simple things like
# gzip'd or compressed files.
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
with open(path, 'rb') as fp:
msg.add_attachment(fp.read(),
maintype=maintype,
subtype=subtype,
filename=filename)
# Now send or store the message
if args.output:
with open(args.output, 'wb') as fp:
fp.write(msg.as_bytes(policy=SMTP))
else:
with smtplib.SMTP('localhost') as s:
s.send_message(msg)
示例2: test_parse_email_5
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import as_bytes [as 别名]
def test_parse_email_5(self):
"""Parses a generated sample e-mail and tests it against a known good result. In this test
we want to specifically ignore e-mail addresses without TLD."""
msg = EmailMessage()
msg['Subject'] = 'Test subject éèàöüä${}'
msg['From'] = Address("John Doe", "john.doe", "example")
msg['To'] = (Address("Jané Doe", "jane.doe", "example.com"),
Address("James Doe", "james.doe", "example.com"))
msg['Cc'] = (Address("Jané Doe", "jane.doe", "example"),
Address("James Doe", "james.doe", "example"))
msg.set_content('''Hi,
Lorem ipsüm dolor sit amét, consectetur 10$ + 5€ adipiscing elit. Praesent feugiat vitae tellus et molestie. Duis est ipsum, tristique eu pulvinar vel, aliquet a nibh. Vestibulum ultricies semper euismod. Maecenas non sagittis elit. Mauris non feugiat leo. Cras vitae quam est. Donec dapibus justo ut dictum viverra. Aliquam eleifend tortor mollis, vulputate ante sit amet, sodales elit. Fusce scelerisque congue risus mollis pellentesque. Sed malesuada erat sit amet nisl laoreet mollis. Suspendisse potenti. Fusce cursus, tortor sit amet euismod molestie, sem enim semper quam, eu ultricies leo est vel turpis.
You should subscribe by replying to [email protected]
''')
good_output_json = r'''{"body": [{"content_header": {"content-type": ["text/plain; charset=\"utf-8\""], "content-transfer-encoding": ["quoted-printable"]}, "content_type": "text/plain", "hash": "07de6840458e398906e73b2cd188d0da813a80ee0337cc349228d983b5ec1c7e"}], "header": {"subject": "Test subject \u00e9\u00e8\u00e0\u00f6\u00fc\u00e4${}", "from": "[email protected]", "to": ["[email protected]", "[email protected]"], "date": "1970-01-01T00:00:00+00:00", "received": [], "header": {"cc": ["Jan\u00e9 Doe <[email protected]>, James Doe <[email protected]>"], "from": ["John Doe <[email protected]>"], "content-type": ["text/plain; charset=\"utf-8\""], "mime-version": ["1.0"], "subject": ["Test subject \u00e9\u00e8\u00e0\u00f6\u00fc\u00e4${}"], "to": ["Jan\u00e9 Doe <[email protected]>, James Doe <[email protected]>"], "content-transfer-encoding": ["quoted-printable"]}}}'''
good_output = json.loads(good_output_json)
test_output_json = json.dumps(eml_parser.eml_parser.decode_email_b(msg.as_bytes(), email_force_tld=True), default=json_serial)
test_output = json.loads(test_output_json)
recursive_compare(good_output, test_output)