
    h3I                     `   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j`                  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?y)-    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y)BadHeaderErrorN)__name__
__module____qualname__     U/var/www/django_project/virt/lib/python3.12/site-packages/django/core/mail/message.pyr    r    $   s    r%   r    >   	resent-cc	resent-to
resent-bccresent-fromresent-sendercctobccfromsenderreply-toc                    xs t         j                  t        |      }d|v sd|v rt        d|d| d      	 |j	                  d       | j                         dk(  rt        |      j	                         }| |fS # t        $ r[ | j                         t        v r%dj                  fd	t        |f      D              }nt        |      j	                         }Y | |fS w xY w)
z6Forbid multi-line headers to prevent header injection.
z*Header values can't contain newlines (got z for header )asciisubject, c              3   6   K   | ]  }t        |        y wN)sanitize_address).0addrencodings     r&   	<genexpr>z,forbid_multi_line_headers.<locals>.<genexpr>D   s      59 x0s   )r   DEFAULT_CHARSETstrr    encodelowerr
   UnicodeEncodeErrorADDRESS_HEADERSjoinr   )namevalr>   s     `r&   forbid_multi_line_headersrI   8   s    3833H
c(Cs{dckMPRVW
 	
'

7 ::<9$+$$&C9  1::<?*)) =I3&=Q C h'..0C 91s   A= =AC! C!c                 H   d}t        | t              set        |       } 	 t        j                  |       \  }}|rt        d|d| d      |j                  xs d}|j                  }|j                  xs d}n-| \  }}d|vrt        d| d      |j                  dd	      \  }}||z   |z   }d
|v sd|v rt        d      	 |j                  d       t        |      j                         }	 |j                  d       t        |      }t!        ||      }	t#        ||	j$                  f      S # t        t
        t        f$ r t        d| z        w xY w# t        $ r t        ||      j                         }Y w xY w# t        $ r t        ||      j                         }Y w xY w)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"	( ,,T2KE4  LQSWX  ##)rB((I\\'RFGg0	;<<#NN32	6NV+M} 5RSS+
		'BZ 9! fFi?Nr>33455E !*j9 	<3d:;;	<2  +B!((*+  99h/668	9s/   D, *E ,E; ,#E#E87E8;#F! F!c                       e Zd ZddZddZy)	MIMEMixinc                     t               }t        j                  |d      }|j                  | ||       |j	                         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~   s;     Z7			$7	;{{}r%   c                     t               }t        j                  |d      }|j                  | ||       |j	                         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   s;     Y$$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y)SafeMIMEMessagec                 R    t        ||d      \  }}t        j                  | ||       y )Nr6   )rI   r   __setitem__rm   rG   rH   s      r&   rw   zSafeMIMEMessage.__setitem__   s&    -dCA	cdC0r%   N)r!   r"   r#   rw   r$   r%   r&   ru   ru      s    1r%   ru   c                   "    e Zd ZddZd ZddZy)SafeMIMETextNc                 D    || _         t        j                  | |||       y )N)_subtype_charset)r>   r   __init__)rm   _textr|   r}   s       r&   r~   zSafeMIMEText.__init__   s     $8Lr%   c                 f    t        ||| j                        \  }}t        j                  | ||       y r:   )rI   r>   r   rw   rx   s      r&   rw   zSafeMIMEText.__setitem__   s*    -dCG	cT4-r%   c                     |dk(  rHt        |t        j                        s.t        d |j                         D              }|rt        nt
        }t        j                  | ||       y )Nr   c              3   `   K   | ]&  }t        |j                  d             t        kD   ( yw)surrogateescape)errorsN)lenrB   RFC5322_EMAIL_LINE_LENGTH_LIMIT)r<   lines     r&   r?   z+SafeMIMEText.set_payload.<locals>.<genexpr>   s3      !  DKK'8K9:12!s   ,.r   )rQ   Charsetany
splitlinesutf8_charset_qputf8_charsetr   set_payload)rm   payloadr   has_long_liness       r&   r   zSafeMIMEText.set_payload   sV    gj'//&J  ! $..0! N *8o\GT7G<r%   )plainNr:   )r!   r"   r#   r~   rw   r   r$   r%   r&   rz   rz      s    M.
=r%   rz   c                       e Zd Z	 ddZd Zy)SafeMIMEMultipartNc                 D    || _         t        j                  | |||fi | y r:   )r>   r   r~   )rm   r|   boundary	_subpartsr>   _paramss         r&   r~   zSafeMIMEMultipart.__init__   s$     !tXxNgNr%   c                 f    t        ||| j                        \  }}t        j                  | ||       y r:   )rI   r>   r   rw   rx   s      r&   rw   zSafeMIMEMultipart.__setitem__   s*    -dCG	c!!$c2r%   )mixedNNN)r!   r"   r#   r~   rw   r$   r%   r&   r   r      s    HLO3r%   r   Alternativecontentmimetype
Attachment)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y)EmailMessagez"A container for email information.r   r   Nc                    |r,t        |t              rt        d      t        |      | _        ng | _        |	r,t        |	t              rt        d      t        |	      | _        ng | _        |r,t        |t              rt        d      t        |      | _        ng | _        |
r,t        |
t              rt        d      t        |
      | _        ng | _        |xs t        j                  | _
        || _        |xs d| _        g | _        |r8|D ]3  }t        |t              r| j                  |       % | j                  |  5 |xs i | _        || _        y)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" GHH2hDGDG"c" GHH2hDGDG#s# HIICyDHDH(C( MNN NDMDM$C(C(CJB	) -
j(3KK
+DKK,	-
 %]$r%   c                 Z    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,=IDOr%   c                     | j                   xs t        j                  }t        | j                  | j
                  |      }| j                  |      }| j                  |d<   | j                  j                  d| j                        |d<   | j                  |d| j                         | j                  |d| j                         | j                  |d| j                         | j                  D cg c]  }|j                          }}d|vrt!        t        j"                        |d<   d	|vrt%        t&        
      |d<   | j                  j)                         D ]  \  }}|j                         dvs|||<    |S c c}w )NSubjectFromToCczReply-To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   rC   r   EMAIL_USE_LOCALTIMEr   r   items)rm   r>   msgkeyheader_namesrG   values          r&   messagezEmailMessage.message
  sJ   ==<H$<$<499d&:&:HE""3'I((,,VT__EF**3dgg>**3dgg>**3
DMMJ 04/A/AB		BB%
 %x/K/KLCK|+ *( ;C--335 	"KD%zz|#CC!D		" 
 Cs   .E;c                 z    | j                   | j                  z   | j                  z   D cg c]  }|s|	 c}S c c}w )z
        Return a list of all recipients of the email (includes direct
        addressees as well as Cc and Bcc entries).
        )r-   r,   r.   )rm   emails     r&   
recipientszEmailMessage.recipients&  s0    
 %)GGdgg$5$@K%UKKKs   88c                 f    | j                         sy| j                  |      j                  | g      S )zSend the email message.r   )r   r   send_messages)rm   r   s     r&   sendzEmailMessage.send-  s/      ""=1??GGr%   c                    t        |t              r+||t        d      | j                  j	                  |       y|t        d      |xs  t        j                  |      d   xs t        }|j                  dd      \  }}|dk(  r!t        |t              r	 |j                         }| j                  j	                  t        |||             y# t        $ r	 t        }Y 8w xY w)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UnicodeDecodeErrorEmailAttachment)rm   r   r   r   basetypesubtypes         r&   r   zEmailMessage.attach5  s     h)"h&: ,  ##H-_899  0''1!40/ 
 !)sA 6Hg6!gu-@").."2 ##OHgx$PQ . @ $@@s   C C! C!c                     t        |      }|j                  d      5 }|j                         }| j                  |j                  ||       ddd       y# 1 sw Y   yxY w)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]  sN     DzYYt_ 	6iikGKK		7H5	6 	6 	6s   .AAc                 $    | j                  |      S r:   )_create_attachmentsrm   r   s     r&   r   zEmailMessage._create_messagem  s    '',,r%   c                    | j                   r| j                  xs t        j                  }|}t	        | j
                  |      }| j                  s|j                         r|j                  |       | j                   D ]B  }t        |t              r|j                  |       %|j                   | j                  |        D |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    }}@(@(@HH#T-?-?(SCyyH113

8$".. E
j(3JJz*JJ6t66
CD	E
 
r%   c                    |j                  dd      \  }}|dk(  r-| j                  xs t        j                  }t	        |||      }|S |dk(  rX|dk(  rSt        |t              r|j                         }n$t        |t              st        t        |            }t        ||      }|S t        ||      }|j                  |       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'v}}@(@(@H%gwAJ$ # "w(': '<0!//+1 -[-AB(':J  "(G4J""7+"":.r%   c                     | j                  ||      }|r&	 |j                  d       |j                  dd|       |S # t        $ r dd|f}Y &w xY w)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  sk    
 11'8D
3( !!%|h "   & 3#R23s   = AAc                     	 | j                   |   ||<   y# t        $ r" |rdj                  d |D              ||<   Y yY y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   2   K   | ]  }t        |        y wr:   )rA   )r<   vs     r&   r?   z=EmailMessage._set_list_header_if_not_empty.<locals>.<genexpr>  s     '?1A'?s   N)r   KeyErrorrF   )rm   r   headervaluess       r&   r   z*EmailMessage._set_list_header_if_not_empty  sM    
	@,,V4CK 	@"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      sv    ,OMH 4%l8LH&RP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.
    alternativec                 ~    t         |   |||||||||
|
       |	xs g D cg c]
  }t        |  c}| _        yc c}w )r   N)superr~   EmailAlternativealternatives)rm   r7   r   r   r-   r.   r   r   r   r   r,   r   r   	__class__s                r&   r~   zEmailMultiAlternatives.__init__  s\    $ 		
 @L?Qr
/:k*
 
s   :c                 l    ||t        d      | j                  j                  t        ||             y)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  s4    ?h.JKK  !1'8!DEr%   c                 B    | j                  | j                  |            S r:   )r   _create_alternativesr   s     r&   r   z&EmailMultiAlternatives._create_message  s    ''(A(A#(FGGr%   c                 R   | j                   xs t        j                  }| j                  r||}t	        | j
                  |      }| j                  r|j                  |       | j                  D ]7  }|j                  | j                  |j                  |j                               9 |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#11HC yy

8$#00 

00#++[-A-A 
r%   c                 z    || j                   vry| j                  D ]  \  }}|j                  d      s||vs y y)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  sJ    
 tyy !%!2!2 	GX""7+G0C	 r%   )rL   rL   NNNNNNNNN)r!   r"   r#   r   r  r~   r   r   r  r  __classcell__)r   s   @r&   r   r     sO     (  
DF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     s    " $ & / )  0 ! $ * . $ H H      + B B ww'! !'//'* '

   : "% 	Z 	
.+6\ 81i 1=9h =,	3	= 	3 mi-DE \+NOo@ o@dP\ Pr%   