Skip to content

Commit cfb4712

Browse files
committed
Added functionality to handle HTML type emails inside body directly alongwith backward compatibility
1 parent afca89e commit cfb4712

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

office365/directory/users/user.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ def send_mail(
274274
bcc_recipients=None,
275275
reply_to = None,
276276
save_to_sent_items=False,
277+
body_type="Text",
277278
):
278-
# type: (str, str|ItemBody, List[str], List[str], List[str], bool) -> Message
279+
# type: (str, str|ItemBody, List[str], List[str]|None, List[str]|None, List[str]|None, bool, str) -> Message
279280
"""Send a new message on the fly
280281
281282
:param str subject: The subject of the message.
@@ -286,10 +287,11 @@ def send_mail(
286287
:param list[str] reply_to: The Reply-To: : recipients for the reply to the message.
287288
:param bool save_to_sent_items: Indicates whether to save the message in Sent Items. Specify it only if
288289
the parameter is false; default is true
290+
:param str body_type: The type of the message body. It can be "HTML" or "Text". Default is "Text".
289291
"""
290292
return_type = Message(self.context)
291293
return_type.subject = subject
292-
return_type.body = body
294+
return_type.body = (body, body_type)
293295
[
294296
return_type.to_recipients.add(Recipient.from_email(email))
295297
for email in to_recipients

office365/outlook/mail/messages/message.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,22 @@ def body(self):
258258

259259
@body.setter
260260
def body(self, value):
261-
# type: (str|ItemBody) -> None
261+
# type: (str|ItemBody|tuple) -> None
262262
"""Sets the body of the message. It can be in HTML or text format."""
263-
if not isinstance(value, ItemBody):
264-
value = ItemBody(value)
265-
self.set_property("body", value)
263+
content_type = "Text" # Default content type
264+
if isinstance(value, tuple):
265+
if len(value) != 2:
266+
raise ValueError("value must be a tuple of (content, content_type)")
267+
content, content_type = value
268+
else:
269+
content = value
270+
if isinstance(content, ItemBody):
271+
self.set_property("body", content)
272+
return
273+
if content_type.lower() not in ["text", "html"]:
274+
raise ValueError("content_type must be either 'Text' or 'HTML'")
275+
item_body = ItemBody(content=content, content_type=content_type)
276+
self.set_property("body", item_body)
266277

267278
@property
268279
def body_preview(self):

0 commit comments

Comments
 (0)