本文整理汇总了Python中email.message.EmailMessage方法的典型用法代码示例。如果您正苦于以下问题:Python message.EmailMessage方法的具体用法?Python message.EmailMessage怎么用?Python message.EmailMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.message
的用法示例。
在下文中一共展示了message.EmailMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: put
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def put(self, device_id):
self._authenticate(device_id)
payload = json.loads(self.request.data)
send_email = SendEmail(payload)
send_email.validate()
account = AccountRepository(self.db).get_account_by_device_id(device_id)
if account:
message = EmailMessage()
message['Subject'] = str(send_email.title)
message['From'] = str(send_email.sender)
message.set_content(str(send_email.body))
message['To'] = account.email_address
self._send_email(message)
response = '', HTTPStatus.OK
else:
response = '', HTTPStatus.NO_CONTENT
return response
示例2: handle
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def handle(self, **options: Optional[str]) -> None:
if options['fixture'] is None:
self.print_help('./manage.py', 'send_to_email_mirror')
raise CommandError
if options['stream'] is None:
stream = "Denmark"
else:
stream = options['stream']
realm = self.get_realm(options)
if realm is None:
realm = get_realm("zulip")
full_fixture_path = os.path.join(settings.DEPLOY_ROOT, options['fixture'])
# parse the input email into EmailMessage type and prepare to process_message() it
message = self._parse_email_fixture(full_fixture_path)
self._prepare_message(message, realm, stream)
mirror_email_message(
message['To'].addresses[0].addr_spec,
base64.b64encode(message.as_bytes()).decode(),
)
示例3: get_imap_messages
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def get_imap_messages() -> Generator[EmailMessage, None, None]:
mbox = IMAP4_SSL(settings.EMAIL_GATEWAY_IMAP_SERVER, settings.EMAIL_GATEWAY_IMAP_PORT)
mbox.login(settings.EMAIL_GATEWAY_LOGIN, settings.EMAIL_GATEWAY_PASSWORD)
try:
mbox.select(settings.EMAIL_GATEWAY_IMAP_FOLDER)
try:
status, num_ids_data = mbox.search(None, 'ALL')
for message_id in num_ids_data[0].split():
status, msg_data = mbox.fetch(message_id, '(RFC822)')
assert isinstance(msg_data[0], tuple)
msg_as_bytes = msg_data[0][1]
message = email.message_from_bytes(msg_as_bytes, policy=email.policy.default)
assert isinstance(message, EmailMessage) # https://github.com/python/typeshed/issues/2417
yield message
mbox.store(message_id, '+FLAGS', '\\Deleted')
mbox.expunge()
finally:
mbox.close()
finally:
mbox.logout()
示例4: test_receive_stream_email_messages_blank_subject_success
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_stream_email_messages_blank_subject_success(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('TestStreamEmailMessages Body')
incoming_valid_message['Subject'] = ''
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
# Hamlet is subscribed to this stream so should see the email message from Othello.
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages Body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assertEqual(message.topic_name(), "(no topic)")
示例5: test_receive_private_stream_email_messages_success
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_private_stream_email_messages_success(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.make_stream("private_stream", invite_only=True)
self.subscribe(user_profile, "private_stream")
stream = get_stream("private_stream", user_profile.realm)
stream_to_address = encode_email_address(stream)
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('TestStreamEmailMessages Body')
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
# Hamlet is subscribed to this stream so should see the email message from Othello.
message = most_recent_message(user_profile)
self.assertEqual(message.content, "TestStreamEmailMessages Body")
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
示例6: test_receive_stream_email_show_sender_success
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_stream_email_show_sender_success(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
parts = stream_to_address.split('@')
parts[0] += "+show-sender"
stream_to_address = '@'.join(parts)
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('TestStreamEmailMessages Body')
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
message = most_recent_message(user_profile)
self.assertEqual(message.content, "From: {}\n{}".format(self.example_email('hamlet'),
"TestStreamEmailMessages Body"))
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
示例7: test_receive_stream_email_show_sender_utf8_encoded_sender
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_stream_email_show_sender_utf8_encoded_sender(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
parts = stream_to_address.split('@')
parts[0] += "+show-sender"
stream_to_address = '@'.join(parts)
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('TestStreamEmailMessages Body')
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = 'Test =?utf-8?b?VXNlcsOzxIXEmQ==?= <=?utf-8?q?hamlet=5F=C4=99?=@zulip.com>'
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
message = most_recent_message(user_profile)
self.assertEqual(message.content, "From: {}\n{}".format('Test Useróąę <hamlet_ę@zulip.com>',
"TestStreamEmailMessages Body"))
self.assertEqual(get_display_recipient(message.recipient), stream.name)
self.assertEqual(message.topic_name(), incoming_valid_message['Subject'])
示例8: test_message_with_invalid_attachment
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_message_with_invalid_attachment(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content("Test body")
# Create an invalid attachment:
attachment_msg = MIMEPart()
attachment_msg.add_header('Content-Disposition', 'attachment', filename="some_attachment")
incoming_valid_message.add_attachment(attachment_msg)
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
with mock.patch('zerver.lib.email_mirror.logger.warning') as mock_warn:
process_message(incoming_valid_message)
mock_warn.assert_called_with(
"Payload is not bytes (invalid attachment %s in message from %s).",
'some_attachment', self.example_email('hamlet'),
)
示例9: test_receive_only_plaintext_with_prefer_html_option
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_only_plaintext_with_prefer_html_option(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_address_prefer_html = f"Denmark.{stream.email_token}.prefer-html@testserver"
text = "Test message"
# This should be correctly identified as empty html body:
html = "<html><body></body></html>"
incoming_valid_message = EmailMessage()
incoming_valid_message.add_alternative(text)
incoming_valid_message.add_alternative(html, subtype="html")
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_address_prefer_html
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
message = most_recent_message(user_profile)
# HTML body is empty, so the plaintext content should be picked, despite prefer-html option.
self.assertEqual(message.content, "Test message")
示例10: test_receive_stream_email_messages_empty_body
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_stream_email_messages_empty_body(self) -> None:
# build dummy messages for stream
# test message with empty body is not sent
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
# empty body
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('')
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
with mock.patch('zerver.lib.email_mirror.logging.warning') as mock_warn:
process_message(incoming_valid_message)
mock_warn.assert_called_with("Email has no nonempty body sections; ignoring.")
示例11: test_receive_stream_email_messages_no_textual_body
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_stream_email_messages_no_textual_body(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
# No textual body
incoming_valid_message = EmailMessage()
with open(os.path.join(settings.DEPLOY_ROOT, "static/images/default-avatar.png"), 'rb') as f:
incoming_valid_message.add_attachment(
f.read(),
maintype="image",
subtype="png",
)
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
with mock.patch('zerver.lib.email_mirror.logging.warning') as mock_warn:
process_message(incoming_valid_message)
mock_warn.assert_called_with("Unable to find plaintext or HTML message body")
示例12: test_receive_stream_email_messages_empty_body_after_stripping
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_receive_stream_email_messages_empty_body_after_stripping(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
headers = {}
headers['Reply-To'] = self.example_email('othello')
# empty body
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('-- \nFooter')
incoming_valid_message['Subject'] = 'TestStreamEmailMessages Subject'
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
message = most_recent_message(user_profile)
self.assertEqual(message.content, "(No email body)")
示例13: test_process_message_strips_subject
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_process_message_strips_subject(self) -> None:
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
incoming_valid_message = EmailMessage()
incoming_valid_message.set_content('TestStreamEmailMessages Body')
incoming_valid_message['Subject'] = "Re: Fwd: Re: Test"
incoming_valid_message['From'] = self.example_email('hamlet')
incoming_valid_message['To'] = stream_to_address
incoming_valid_message['Reply-to'] = self.example_email('othello')
process_message(incoming_valid_message)
message = most_recent_message(user_profile)
self.assertEqual("Test", message.topic_name())
# If after stripping we get an empty subject, it should get set to (no topic)
del incoming_valid_message['Subject']
incoming_valid_message['Subject'] = "Re: Fwd: Re: "
process_message(incoming_valid_message)
message = most_recent_message(user_profile)
self.assertEqual("(no topic)", message.topic_name())
示例14: test_charset_not_specified
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def test_charset_not_specified(self) -> None:
message_as_string = self.fixture_data('1.txt', type='email')
message_as_string = message_as_string.replace("Content-Type: text/plain; charset=\"us-ascii\"",
"Content-Type: text/plain")
incoming_message = message_from_string(message_as_string, policy=email.policy.default)
assert isinstance(incoming_message, EmailMessage) # https://github.com/python/typeshed/issues/2417
user_profile = self.example_user('hamlet')
self.login_user(user_profile)
self.subscribe(user_profile, "Denmark")
stream = get_stream("Denmark", user_profile.realm)
stream_to_address = encode_email_address(stream)
del incoming_message['To']
incoming_message['To'] = stream_to_address
process_message(incoming_message)
message = most_recent_message(user_profile)
self.assertEqual(message.content, "Email fixture 1.txt body")
示例15: construct_zulip_body
# 需要导入模块: from email import message [as 别名]
# 或者: from email.message import EmailMessage [as 别名]
def construct_zulip_body(message: EmailMessage, realm: Realm, show_sender: bool=False,
include_quotes: bool=False, include_footer: bool=False,
prefer_text: bool=True) -> str:
body = extract_body(message, include_quotes, prefer_text)
# Remove null characters, since Zulip will reject
body = body.replace("\x00", "")
if not include_footer:
body = filter_footer(body)
if not body.endswith('\n'):
body += '\n'
body += extract_and_upload_attachments(message, realm)
body = body.strip()
if not body:
body = '(No email body)'
if show_sender:
sender = str(message.get("From", ""))
body = f"From: {sender}\n{body}"
return body
## Sending the Zulip ##