function _message_to_payload
Converts an email Message object to a payload format by replacing line feed characters with CRLF (carriage return + line feed) and removing the first two lines.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/internal/multipart_page_query.py
10 - 17
simple
Purpose
This function transforms an email Message object into a properly formatted payload suitable for HTTP transmission or email protocols. It specifically handles line ending conversion from Unix-style (LF) to Windows/network-style (CRLF) and strips the first two lines of the message, likely removing headers or metadata that should not be included in the final payload.
Source Code
def _message_to_payload(message):
# type: (Message) -> AnyStr
lf = b"\n"
crlf = b"\r\n"
payload = message_as_bytes_or_string(message)
lines = payload.split(lf)
payload = bytes.join(crlf, lines[2:]) + crlf
return payload
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
message |
- | - | positional_or_keyword |
Parameter Details
message: An email.message.Message object containing the message data to be converted. This should be a valid Message instance that can be serialized to bytes or string format.
Return Value
Returns an AnyStr (either bytes or string) representing the formatted payload. The payload consists of all lines from the original message starting from line 3 onwards (index 2), with line endings converted to CRLF (\r\n), and terminated with a final CRLF sequence.
Dependencies
emailtypingoffice365
Required Imports
from email.message import Message
from typing import AnyStr
from office365.runtime.compat import message_as_bytes_or_string
Usage Example
from email.message import Message
from typing import AnyStr
from office365.runtime.compat import message_as_bytes_or_string
def _message_to_payload(message):
lf = b"\n"
crlf = b"\r\n"
payload = message_as_bytes_or_string(message)
lines = payload.split(lf)
payload = bytes.join(crlf, lines[2:]) + crlf
return payload
# Example usage
msg = Message()
msg.set_payload("Line 1\nLine 2\nLine 3\nLine 4")
formatted_payload = _message_to_payload(msg)
print(formatted_payload)
Best Practices
- Ensure the input message object is a valid email.message.Message instance before calling this function
- Be aware that this function removes the first two lines of the message, which may contain important header information
- The function assumes the message can be converted to bytes format; ensure compatibility with the message_as_bytes_or_string utility
- This function is likely intended for internal use within the office365 library for formatting messages for API requests
- The returned payload will always end with CRLF, which is standard for many network protocols
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function msg_to_eml 58.4% similar
-
function msg_to_eml_alternative 57.3% similar
-
function generate_html_from_msg 56.4% similar
-
function generate_simple_html_from_eml 56.2% similar
-
function msg_to_pdf 52.2% similar