Skip to content

Commit 6745bfc

Browse files
authored
Merge pull request #958 from bhavberi/send_mail
Added Reply To and HTML ItemBody Type support in send_mail functionality
2 parents 09ecb30 + cfb4712 commit 6745bfc

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

office365/directory/users/user.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,26 @@ def send_mail(
272272
to_recipients,
273273
cc_recipients=None,
274274
bcc_recipients=None,
275+
reply_to = None,
275276
save_to_sent_items=False,
277+
body_type="Text",
276278
):
277-
# 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
278280
"""Send a new message on the fly
279281
280282
:param str subject: The subject of the message.
281283
:param str body: The body of the message. It can be in HTML or text format
282284
:param list[str] to_recipients: The To: recipients for the message.
283285
:param list[str] cc_recipients: The CC: recipients for the message.
284286
:param list[str] bcc_recipients: The BCC: recipients for the message.
287+
:param list[str] reply_to: The Reply-To: : recipients for the reply to the message.
285288
:param bool save_to_sent_items: Indicates whether to save the message in Sent Items. Specify it only if
286289
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".
287291
"""
288292
return_type = Message(self.context)
289293
return_type.subject = subject
290-
return_type.body = body
294+
return_type.body = (body, body_type)
291295
[
292296
return_type.to_recipients.add(Recipient.from_email(email))
293297
for email in to_recipients
@@ -302,6 +306,11 @@ def send_mail(
302306
return_type.cc_recipients.add(Recipient.from_email(email))
303307
for email in cc_recipients
304308
]
309+
if reply_to is not None:
310+
[
311+
return_type.reply_to.add(Recipient.from_email(email))
312+
for email in reply_to
313+
]
305314

306315
payload = {"message": return_type, "saveToSentItems": save_to_sent_items}
307316
qry = ServiceOperationQuery(self, "sendmail", None, payload)

office365/outlook/mail/messages/message.py

+23-4
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):
@@ -407,6 +418,14 @@ def cc_recipients(self):
407418
"ccRecipients", ClientValueCollection(Recipient)
408419
)
409420

421+
@property
422+
def reply_to(self):
423+
"""The replyTo: recipients for the reply to the message."""
424+
self._persist_changes("replyTo")
425+
return self.properties.setdefault(
426+
"replyTo", ClientValueCollection(Recipient)
427+
)
428+
410429
@property
411430
def sender(self):
412431
"""The account that is actually used to generate the message. In most cases, this value is the same as the

0 commit comments

Comments
 (0)