MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / send_message

Method send_message

tools/python-3.11.9-amd64/Lib/smtplib.py:912–987  ·  view source on GitHub ↗

Converts message to a bytestring and passes it to sendmail. The arguments are as for sendmail, except that msg is an email.message.Message object. If from_addr is None or to_addrs is None, these arguments are taken from the headers of the Message as described i

(self, msg, from_addr=None, to_addrs=None,
                     mail_options=(), rcpt_options=())

Source from the content-addressed store, hash-verified

910 return senderrs
911
912 def send_message(self, msg, from_addr=None, to_addrs=None,
913 mail_options=(), rcpt_options=()):
914 """Converts message to a bytestring and passes it to sendmail.
915
916 The arguments are as for sendmail, except that msg is an
917 email.message.Message object. If from_addr is None or to_addrs is
918 None, these arguments are taken from the headers of the Message as
919 described in RFC 2822 (a ValueError is raised if there is more than
920 one set of 'Resent-' headers). Regardless of the values of from_addr and
921 to_addr, any Bcc field (or Resent-Bcc field, when the Message is a
922 resent) of the Message object won't be transmitted. The Message
923 object is then serialized using email.generator.BytesGenerator and
924 sendmail is called to transmit the message. If the sender or any of
925 the recipient addresses contain non-ASCII and the server advertises the
926 SMTPUTF8 capability, the policy is cloned with utf8 set to True for the
927 serialization, and SMTPUTF8 and BODY=8BITMIME are asserted on the send.
928 If the server does not support SMTPUTF8, an SMTPNotSupported error is
929 raised. Otherwise the generator is called without modifying the
930 policy.
931
932 """
933 # 'Resent-Date' is a mandatory field if the Message is resent (RFC 2822
934 # Section 3.6.6). In such a case, we use the 'Resent-*' fields. However,
935 # if there is more than one 'Resent-' block there's no way to
936 # unambiguously determine which one is the most recent in all cases,
937 # so rather than guess we raise a ValueError in that case.
938 #
939 # TODO implement heuristics to guess the correct Resent-* block with an
940 # option allowing the user to enable the heuristics. (It should be
941 # possible to guess correctly almost all of the time.)
942
943 self.ehlo_or_helo_if_needed()
944 resent = msg.get_all('Resent-Date')
945 if resent is None:
946 header_prefix = ''
947 elif len(resent) == 1:
948 header_prefix = 'Resent-'
949 else:
950 raise ValueError("message has more than one 'Resent-' header block")
951 if from_addr is None:
952 # Prefer the sender field per RFC 2822:3.6.2.
953 from_addr = (msg[header_prefix + 'Sender']
954 if (header_prefix + 'Sender') in msg
955 else msg[header_prefix + 'From'])
956 from_addr = email.utils.getaddresses([from_addr])[0][1]
957 if to_addrs is None:
958 addr_fields = [f for f in (msg[header_prefix + 'To'],
959 msg[header_prefix + 'Bcc'],
960 msg[header_prefix + 'Cc'])
961 if f is not None]
962 to_addrs = [a[1] for a in email.utils.getaddresses(addr_fields)]
963 # Make a local copy so we can delete the bcc headers.
964 msg_copy = copy.copy(msg)
965 del msg_copy['Bcc']
966 del msg_copy['Resent-Bcc']
967 international = False
968 try:
969 ''.join([from_addr, *to_addrs]).encode('ascii')

Callers 1

emitMethod · 0.95

Calls 11

has_extnMethod · 0.95
sendmailMethod · 0.95
flattenMethod · 0.80
get_allMethod · 0.45
copyMethod · 0.45
encodeMethod · 0.45
joinMethod · 0.45
cloneMethod · 0.45
getvalueMethod · 0.45

Tested by

no test coverage detected