
    	i=I                        d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZ d dl m!Z!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z*m+Z+m,Z,  ej        d          Z-de-_.         ej        d          Z/ej0        e/_.        dZ1dZ2 G d de3          Z4h dZ5d Z6d Z7 G d d          Z8 G d de8e          Z9 G d  d!e8e          Z: G d" d#e8e          Z; ed$d%d&g          Z< ed'g d(          Z= G d) d*          Z> G d+ d,e>          Z?dS )-    N)
namedtuplecharset)encoders)	generatormessage_from_bytes)HeaderParseError)Header)Addressparser)Message)MIMEBase)MIMEMessage)MIMEMultipart)MIMEText)
formataddr
formatdategetaddresses
make_msgid)BytesIOStringIO)Path)settings)DNS_NAME)force_bytes	force_strpunycodeutf-8zapplication/octet-streami  c                       e Zd ZdS )BadHeaderErrorN)__name__
__module____qualname__     P/srv/django_bis/venv311/lib/python3.11/site-packages/django/core/mail/message.pyr    r    $   s        Dr%   r    >   	resent-cc	resent-to
resent-bccresent-fromresent-sendercctobccfromsenderreply-toc                    pt           j        t          |          }d|v sd|v rt          d|d| d          	 |                    d           |                                 dk    r!t          |                                          }n}# t          $ rp |                                 t          v r0d	                    fd	t          |f          D                       }n"t          |                                          }Y nw xY w| |fS )
z6Forbid multi-line headers to prevent header injection.
z*Header values can't contain newlines (got z for header )asciisubject, c              3   8   K   | ]}t          |          V  d S N)sanitize_address).0addrencodings     r&   	<genexpr>z,forbid_multi_line_headers.<locals>.<genexpr>D   s?        59 x00     r%   )r   DEFAULT_CHARSETstrr    encodelowerr
   UnicodeEncodeErrorADDRESS_HEADERSjoinr   )namevalr>   s     `r&   forbid_multi_line_headersrI   8   s7   383H
c((Cs{{dckknMPSSRVRVRVW
 
 	
'

7 ::<<9$$++$$&&C  1 1 1::<<?**))    =I3&=Q=Q    CC h''..00C1 9s   B A7DDc                    d}t          | t                    st          |           } 	 t          j        |           \  }}|rt          d|d| d          |j        pd}|j        }|j        pd}nb# t          t
          t          f$ r t          d| z            w xY w| \  }}d|vrt          d| d          |                    dd	          \  }}||z   |z   }d
|v sd|v rt          d          	 |                    d           t          |                                          }n2# t          $ r% t          ||                                          }Y nw xY w	 |                    d           n2# t          $ r% t          ||                                          }Y nw xY wt          |          }t!          ||          }	t#          ||	j        f          S )zF
    Format a pair of (name, address) or an email address string.
    NzInvalid address; only z could be parsed from "" zInvalid address "%s"@zInvalid address "   r3   r4   z7Invalid address; address parts cannot contain newlines.r6   )usernamedomain)
isinstancetupler   r   get_mailbox
ValueErrordisplay_name
local_partrP   r	   
IndexErrorrsplitrB   r
   rD   r   r   r   	addr_spec)
r=   r>   addresstokenrestnm	localpartrP   address_partsparsed_addresss
             r&   r;   r;   O   s<    GdE"" 3	( ,T22KE4   jLQEESWSWSWX   #)rB(I\'RFF !*j9 	< 	< 	<3d:;;;	< Gg;;;;<<<#NN322	6NV+M} 5 5RSSS+
		'BZZ   + + +B!!((**+9!!!! 9 9 99h//6688			9fFi???Nr>34555s/   A1 1*B36D* *,EEE3 3,F"!F"c                       e Zd ZddZddZdS )	MIMEMixinFr3   c                     t                      }t          j        |d          }|                    | ||           |                                S )a&  Return the entire formatted message as a string.
        Optional `unixfrom' when True, means include the Unix From_ envelope
        header.

        This overrides the default as_string() implementation to not mangle
        lines that begin with 'From '. See bug #13433 for details.
        Fmangle_from_unixfromlinesep)r   r   	Generatorflattengetvalueselfrg   rh   fpgs        r&   	as_stringzMIMEMixin.as_string~   sH     ZZ777			$7	;;;{{}}r%   c                     t                      }t          j        |d          }|                    | ||           |                                S )a"  Return the entire formatted message as bytes.
        Optional `unixfrom' when True, means include the Unix From_ envelope
        header.

        This overrides the default as_bytes() implementation to not mangle
        lines that begin with 'From '. See bug #13433 for details.
        Frd   rf   )r   r   BytesGeneratorrj   rk   rl   s        r&   as_byteszMIMEMixin.as_bytes   sH     YY$Re<<<			$7	;;;{{}}r%   N)Fr3   )r!   r"   r#   rp   rs   r$   r%   r&   rb   rb   }   s<                r%   rb   c                       e Zd Zd ZdS )SafeMIMEMessagec                 Z    t          ||d          \  }}t          j        | ||           d S )Nr6   )rI   r   __setitem__rm   rG   rH   s      r&   rw   zSafeMIMEMessage.__setitem__   s2    -dCAA	cdC00000r%   N)r!   r"   r#   rw   r$   r%   r&   ru   ru      s#        1 1 1 1 1r%   ru   c                   $    e Zd ZddZd ZddZdS )SafeMIMETextplainNc                 D    || _         t          j        | |||           d S )N)_subtype_charset)r>   r   __init__)rm   _textr}   r~   s       r&   r   zSafeMIMEText.__init__   s)     $8LLLLLLr%   c                 d    t          ||| j                  \  }}t          j        | ||           d S r:   )rI   r>   r   rw   rx   s      r&   rw   zSafeMIMEText.__setitem__   s4    -dCGG	cT4-----r%   c                     |dk    rUt          |t          j                  s;t          d |                                D                       }|rt          nt
          }t          j        | ||           d S )Nr   c              3   n   K   | ]0}t          |                    d                     t          k    V  1dS )surrogateescape)errorsN)lenrB   RFC5322_EMAIL_LINE_LENGTH_LIMIT)r<   lines     r&   r?   z+SafeMIMEText.set_payload.<locals>.<genexpr>   sW       ! !  DKK'8K99::12! ! ! ! ! !r%   r   )rQ   Charsetany
splitlinesutf8_charset_qputf8_charsetr   set_payload)rm   payloadr   has_long_liness       r&   r   zSafeMIMEText.set_payload   s    gj'/&J&J  ! ! $..00! ! !  N *8Ioo\GT7G<<<<<<r%   )r{   Nr:   )r!   r"   r#   r   rw   r   r$   r%   r&   rz   rz      sO        M M M M. . .
= 
= 
= 
= 
= 
=r%   rz   c                       e Zd Z	 ddZd ZdS )SafeMIMEMultipartmixedNc                 >    || _         t          j        | |||fi | d S r:   )r>   r   r   )rm   r}   boundary	_subpartsr>   _paramss         r&   r   zSafeMIMEMultipart.__init__   s0     !tXxNNgNNNNNr%   c                 d    t          ||| j                  \  }}t          j        | ||           d S r:   )rI   r>   r   rw   rx   s      r&   rw   zSafeMIMEMultipart.__setitem__   s4    -dCGG	c!$c22222r%   )r   NNN)r!   r"   r#   r   rw   r$   r%   r&   r   r      s>        HLO O O O3 3 3 3 3r%   r   EmailAlternativecontentmimetypeEmailAttachment)filenamer   r   c                       e Zd ZdZdZdZdZ	 	 	 	 	 	 	 	 	 	 ddZddZd	 Z	d
 Z
ddZddZddZd Zd Zd ZddZd ZdS )EmailMessagez"A container for email information.r{   r   NrL   c                    |r9t          |t                    rt          d          t          |          | _        ng | _        |	r9t          |	t                    rt          d          t          |	          | _        ng | _        |r9t          |t                    rt          d          t          |          | _        ng | _        |
r9t          |
t                    rt          d          t          |
          | _        ng | _        |pt          j	        | _
        || _        |pd| _        g | _        |r:|D ]7}t          |t                    r|                     |           - | j        |  8|pi | _        || _        dS )g
        Initialize a single email message (which can be sent to multiple
        recipients).
        z%"to" argument must be a list or tuplez%"cc" argument must be a list or tuplez&"bcc" argument must be a list or tuplez+"reply_to" argument must be a list or tuplerL   N)rQ   rA   	TypeErrorlistr-   r,   r.   reply_tor   DEFAULT_FROM_EMAIL
from_emailr7   bodyattachmentsr   attachextra_headers
connection)rm   r7   r   r   r-   r.   r   r   headersr,   r   
attachments               r&   r   zEmailMessage.__init__   s   "  	"c"" I GHHH2hhDGGDG 	"c"" I GHHH2hhDGGDG 	#s## J HIIICyyDHHDH 	(C(( O MNNN NNDMMDM$C(CJB	 	-) - -
j(33 -KK
++++DK,,,$]$r%   Fc                 L    ddl m} | j        s ||          | _        | j        S )Nr   )get_connection)fail_silently)django.core.mailr   r   )rm   r   r   s      r&   r   zEmailMessage.get_connection  s;    333333 	J,n=IIIDOr%   c                    | j         pt          j        }t          | j        | j        |          }|                     |          }| j        |d<   | j        	                    d| j
                  |d<   |                     |d| j                   |                     |d| j                   |                     |d| j                   d | j        D             }d|vrt          t          j                  |d	<   d
|vrt#          t$                    |d<   | j                                        D ] \  }}|                                dvr|||<   !|S )NSubjectFromToCczReply-Toc                 6    g | ]}|                                 S r$   )rC   )r<   keys     r&   
<listcomp>z(EmailMessage.message.<locals>.<listcomp>  s     BBB		BBBr%   date)	localtimeDatez
message-id)rP   z
Message-ID>   r,   r-   r/   r1   )r>   r   r@   rz   r   content_subtype_create_messager7   r   getr   _set_list_header_if_not_emptyr-   r,   r   r   EMAIL_USE_LOCALTIMEr   r   itemsrC   )rm   r>   msgheader_namesrG   values         r&   messagezEmailMessage.message
  sS   =<H$<49d&:HEE""3''I(,,VT_EEF**3dg>>>**3dg>>>**3
DMJJJ CBt/ABBB%%
 %x/KLLLCK|++ *( ; ; ;C-3355 	" 	"KD%zz||#CCC!D	
r%   c                 D    d | j         | j        z   | j        z   D             S )z
        Return a list of all recipients of the email (includes direct
        addressees as well as Cc and Bcc entries).
        c                     g | ]}||S r$   r$   )r<   emails     r&   r   z+EmailMessage.recipients.<locals>.<listcomp>+  s    KKK%UKKKKr%   )r-   r,   r.   )rm   s    r&   
recipientszEmailMessage.recipients&  s)    
 LKDGdg$5$@KKKKr%   c                     |                                  sdS |                     |                              | g          S )zSend the email message.r   )r   r   send_messages)rm   r   s     r&   sendzEmailMessage.send-  s?       	 1""=11??GGGr%   c                 
   t          |t                    r/||t          d          | j                            |           dS |t          d          |p t          j        |          d         pt          }|                    dd          \  }}|dk    rBt          |t                    r-	 |
                                }n# t          $ r
 t          }Y nw xY w| j                            t          |||                     dS )a  
        Attach a file with the given filename and content. The filename can
        be omitted and the mimetype is guessed, if not provided.

        If the first parameter is a MIMEBase subclass, insert it directly
        into the resulting message attachments.

        For a text/* mimetype (guessed or specified), when a bytes object is
        specified as content, decode it as UTF-8. If that fails, set the
        mimetype to DEFAULT_ATTACHMENT_MIME_TYPE and don't decode the content.
        NzLcontent and mimetype must not be given when a MIMEBase instance is provided.zcontent must be provided.r   /rN   text)rQ   r   rT   r   append	mimetypes
guess_typeDEFAULT_ATTACHMENT_MIME_TYPEsplitbytesdecodeUnicodeDecodeErrorr   )rm   r   r   r   basetypesubtypes         r&   r   zEmailMessage.attach5  s;    h)) 	R"h&: ,   ##H-----_8999  0'11!40/ 
 !)sA 6 6Hg6!!gu-- @@").."2"2- @ @ @ $@@
 ##OHgx$P$PQQQQQs   .C CCc                     t          |          }|                    d          5 }|                                }|                     |j        ||           ddd           dS # 1 swxY w Y   dS )al  
        Attach a file from the filesystem.

        Set the mimetype to DEFAULT_ATTACHMENT_MIME_TYPE if it isn't specified
        and cannot be guessed.

        For a text/* mimetype (guessed or specified), decode the file's content
        as UTF-8. If that fails, set the mimetype to
        DEFAULT_ATTACHMENT_MIME_TYPE and don't decode the content.
        rbN)r   openreadr   rG   )rm   pathr   filer   s        r&   attach_filezEmailMessage.attach_file]  s     DzzYYt__ 	6iikkGKK	7H555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6s   1A##A'*A'c                 ,    |                      |          S r:   )_create_attachmentsrm   r   s     r&   r   zEmailMessage._create_messagem  s    '',,,r%   c                 n   | j         r| j        pt          j        }|}t	          | j        |          }| j        s|                                r|                    |           | j         D ]J}t          |t                    r|                    |           -|                     | j        |            K|S N)r}   r>   )r   r>   r   r@   r   mixed_subtyper   is_multipartr   rQ   r   _create_attachment)rm   r   r>   body_msgr   s        r&   r   z EmailMessage._create_attachmentsp  s     
	E}@(@HH#T-?(SSSCy %H1133 %

8$$$". E E
j(33 EJJz****JJ6t6
CDDDD
r%   c                    |                     dd          \  }}|dk    r%| j        pt          j        }t	          |||          }n|dk    rr|dk    rlt          |t                    r|                                }n1t          |t                    st          t          |                    }t          ||          }n9t          ||          }|                    |           t          j        |           |S )z
        Convert the content, mimetype pair into a MIME attachment object.

        If the mimetype is message/rfc822, content may be an
        email.Message or EmailMessage object, as well as a str.
        r   rN   r   r   rfc822)r   r>   r   r@   rz   rQ   r   r   r   r   r   ru   r   r   Encodersencode_base64)rm   r   r   r   r   r>   r   s          r&   _create_mime_attachmentz$EmailMessage._create_mime_attachment~  s     %NN322'v}@(@H%gwAAJJ""w(':': '<00 C!//++11 C -[-A-ABB('::JJ "(G44J""7+++":...r%   c                     |                      ||          }|rD	 |                    d           n# t          $ r dd|f}Y nw xY w|                    dd|           |S )zg
        Convert the filename, content, mimetype triple into a MIME attachment
        object.
        r6   r   rL   zContent-Dispositionr   )r   )r   rB   rD   
add_header)rm   r   r   r   r   s        r&   r   zEmailMessage._create_attachment  s    
 11'8DD
 	3((((% 3 3 3#R23!!%|h "    s   0 AAc                     	 | j         |         ||<   dS # t          $ r+ |r%d                    d |D                       ||<   Y dS Y dS w xY w)z
        Set msg's header, either from self.extra_headers, if present, or from
        the values argument if not empty.
        r8   c              3   4   K   | ]}t          |          V  d S r:   )rA   )r<   vs     r&   r?   z=EmailMessage._set_list_header_if_not_empty.<locals>.<genexpr>  s(      '?'?1A'?'?'?'?'?'?r%   N)r   KeyErrorrF   )rm   r   headervaluess       r&   r   z*EmailMessage._set_list_header_if_not_empty  s    
	@,V4CKKK 	@ 	@ 	@ @"ii'?'?'?'?'???F@ @ @	@s    .A	A	)
rL   rL   NNNNNNNN)F)NNNr:   )r!   r"   r#   __doc__r   r   r>   r   r   r   r   r   r   r   r   r   r   r   r   r$   r%   r&   r   r      s-       ,,OMH 4% 4% 4% 4%l     8L L LH H H H&R &R &R &RP6 6 6 6 - - -    <    	@ 	@ 	@ 	@ 	@r%   r   c                   V     e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 d
 fd	Zd Zd Zd Zd	 Z	 xZ
S )EmailMultiAlternativesz
    A version of EmailMessage that makes it easy to send multipart/alternative
    messages. For example, including text and HTML versions of the text is
    made easier.
    alternativerL   Nc                     t                                          |||||||||
|
  
         d |	pg D             | _        dS )r   c                      g | ]}t          | S r$   )r   )r<   r   s     r&   r   z3EmailMultiAlternatives.__init__.<locals>.<listcomp>  s+     
 
 
/:k*
 
 
r%   N)superr   alternatives)rm   r7   r   r   r-   r.   r   r   r   r   r,   r   	__class__s               r&   r   zEmailMultiAlternatives.__init__  sk    $ 		
 	
 	

 
?K?Qr
 
 
r%   c                 |    ||t          d          | j                            t          ||                     dS )z-Attach an alternative content representation.Nz+Both content and mimetype must be provided.)rT   r   r   r   )rm   r   r   s      r&   attach_alternativez)EmailMultiAlternatives.attach_alternative  sC    ?h.JKKK  !1'8!D!DEEEEEr%   c                 R    |                      |                     |                    S r:   )r   _create_alternativesr   s     r&   r   z&EmailMultiAlternatives._create_message  s$    ''(A(A#(F(FGGGr%   c                    | j         pt          j        }| j        rq|}t	          | j        |          }| j        r|                    |           | j        D ]5}|                    |                     |j	        |j
                             6|S r   )r>   r   r@   r   r   alternative_subtyper   r   r   r   r   )rm   r   r>   r   r   s        r&   r  z+EmailMultiAlternatives._create_alternatives  s    =<H$< 	H#1H  C y %

8$$$#0  

00#+[-A    
 
r%   c                 n    || j         vrdS | j        D ]!\  }}|                    d          r||vr dS "dS )zz
        Checks that ``text`` occurs in the email body and in all attached MIME
        type text/* alternatives.
        Fztext/T)r   r   
startswith)rm   r   r   r   s       r&   body_containsz$EmailMultiAlternatives.body_contains  sZ    
 ty  5!%!2 	 	GX""7++ G0C0Cuutr%   )rL   rL   NNNNNNNNN)r!   r"   r#   r   r  r   r  r   r  r	  __classcell__)r   s   @r&   r   r     s          (  
  
  
  
  
  
DF F FH H H  "      r%   r   )@r   collectionsr   r   r   r   r   r   r   r   email.errorsr	   email.headerr
   email.headerregistryr   r   email.messager   email.mime.baser   email.mime.messager   email.mime.multipartr   email.mime.textr   email.utilsr   r   r   r   ior   r   pathlibr   django.confr   django.core.mail.utilsr   django.utils.encodingr   r   r   r   body_encodingr   QPr   r   rT   r    rE   rI   r;   rb   ru   rz   r   r   r   r   r   r$   r%   r&   <module>r     sy       " " " " " " $ $ $ $ $ $ & & & & & & / / / / / / / / ) ) ) ) ) )       0 0 0 0 0 0 0 0 ! ! ! ! ! ! $ $ $ $ $ $ * * * * * * . . . . . . $ $ $ $ $ $ H H H H H H H H H H H H                                   + + + + + + B B B B B B B B B B ww''! !'/'** '
   : "% 	 	 	 	 	Z 	 	 	
    .+6 +6 +6\       81 1 1 1 1i 1 1 1= = = = =9h = = =,	3 	3 	3 	3 	3	= 	3 	3 	3 :09j2IJJ *.0S0S0STTo@ o@ o@ o@ o@ o@ o@ o@dP P P P P\ P P P P Pr%   