def send_message( self, recipients: str, subject: str = "", body: str = "", attachments: str = None, html: bool = False, images: str = None, cc: str = None, bcc: str = None, save: bool = False, ): """Keyword for sending message through connected Exchange account. :param recipients: list of email addresses, defaults to [] :param subject: message subject, defaults to "" :param body: message body, defaults to "" :param attachments: list of filepaths to attach, defaults to [] :param html: if message content is in HTML, default `False` :param images: list of filepaths for inline use, defaults to [] :param cc: list of email addresses, defaults to [] :param bcc: list of email addresses, defaults to [] :param save: is sent message saved to Sent messages folder or not, defaults to False Email addresses can be prefixed with ``ex:`` to indicate an Exchange account address. Recipients is a `required` parameter. """ recipients, cc, bcc, attachments, images = self._handle_message_parameters( recipients, cc, bcc, attachments, images ) self.logger.info("Sending message to %s", ",".join(recipients)) m = Message( account=self.account, subject=subject, body=body, to_recipients=recipients, cc_recipients=cc, bcc_recipients=bcc, ) self._add_attachments_to_msg(attachments, m) self._add_images_inline_to_msg(images, html, body, m) if html: m.body = HTMLBody(body) else: m.body = body if save: m.folder = self.account.sent m.send_and_save() else: m.send() return True
def send_message( self, recipients: Optional[Union[List[str], str]] = None, subject: Optional[str] = "", body: Optional[str] = "", attachments: Optional[Union[List[str], str]] = None, html: Optional[bool] = False, images: Optional[Union[List[str], str]] = None, cc: Optional[Union[List[str], str]] = None, bcc: Optional[Union[List[str], str]] = None, save: Optional[bool] = False, ) -> None: """Keyword for sending message through connected Exchange account. :param recipients: list of email addresses :param subject: message subject, defaults to "" :param body: message body, defaults to "" :param attachments: list of filepaths to attach, defaults to `None` :param html: if message content is in HTML, default `False` :param images: list of filepaths for inline use, defaults to `None` :param cc: list of email addresses :param bcc: list of email addresses :param save: is sent message saved to Sent messages folder or not, defaults to False Email addresses can be prefixed with ``ex:`` to indicate an Exchange account address. At least one target needs to exist for `recipients`, `cc` or `bcc`. """ if not self.account: raise AuthenticationError("Not authorized to any Exchange account") recipients, cc, bcc, attachments, images = self._handle_message_parameters( recipients, cc, bcc, attachments, images) if not recipients and not cc and not bcc: raise NoRecipientsError( "Atleast one address is required for 'recipients', 'cc' or 'bcc' parameter" # noqa: E501 ) self.logger.info("Sending message to %s", ",".join(recipients)) m = Message( account=self.account, subject=subject, body=body, to_recipients=recipients, cc_recipients=cc, bcc_recipients=bcc, ) self._add_attachments_to_msg(attachments, m) self._add_images_inline_to_msg(images, html, body, m) if html: m.body = HTMLBody(body) else: m.body = body # TODO. The exchangelib does not seem to provide any straightforward way of # verifying if message was sent or not if save: m.folder = self.account.sent m.send_and_save() else: m.send()
def send_message( self, recipients: list = None, cc_recipients: list = None, bcc_recipients: list = None, subject: str = "", body: str = "", save: bool = False, ): """Keyword for sending message through connected Exchange account. Email addresses can be prefixed with `ex:` to indicate Exchange account address. :param recipients: list of email addresses, defaults to [] :param cc_recipients: list of email addresses, defaults to [] :param bcc_recipients: list of email addresses, defaults to [] :param subject: message subject, defaults to "" :param body: message body, defaults to "" :param save: is sent message saved to Sent messages folder or not, defaults to False """ if recipients is None: self.logger.warning("recipients is None - not sending message") return if cc_recipients is None: cc_recipients = [] if bcc_recipients is None: bcc_recipients = [] if not isinstance(recipients, list): recipients = [recipients] if not isinstance(cc_recipients, list): cc_recipients = [cc_recipients] if not isinstance(bcc_recipients, list): bcc_recipients = [bcc_recipients] self.logger.info("Sending message to %s", ",".join(recipients)) mail_recipients = [] mail_cc = [] mail_bcc = [] mail_recipients = [ Mailbox(email_address=p.split("ex:")[1]) if "ex:" in p else p for p in recipients ] mail_cc = [ Mailbox(email_address=p.split("ex:")[1]) if "ex:" in p else p for p in cc_recipients ] mail_bcc = [ Mailbox(email_address=p.split("ex:")[1]) if "ex:" in p else p for p in bcc_recipients ] m = Message( account=self.account, subject=subject, body=body, to_recipients=mail_recipients, cc_recipients=mail_cc, bcc_recipients=mail_bcc, ) if save: m.folder = self.account.sent m.send_and_save() else: m.send()