o
    "7h=I                     @   s  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dZ-de-_.edZ/ej0e/_.dZ1dZ2G dd de3Z4h dZ5dd Z6dd Z7G dd dZ8G d d! d!e8eZ9G d"d# d#e8eZ:G d$d% d%e8eZ;ed&d'd(gZ<ed)g d*Z=G d+d, d,Z>G d-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                   @   s   e Zd ZdS )BadHeaderErrorN)__name__
__module____qualname__ r"   r"   m/var/www/epreuve.sigeris.cm/public_html/epreuve/venv/lib/python3.10/site-packages/django/core/mail/message.pyr   $   s    r   >   	resent-cc	resent-to
resent-bccresent-fromresent-sendercctobccfromsenderreply-toc                    s    pt j t|}d|v sd|v rtd|| f z|d W n. tyN   |  tv r=d fddt	|fD }nt
|  }Y | |fS Y | |fS w |  dkr[t
| }| |fS )	z6Forbid multi-line headers to prevent header injection.
z;Header values can't contain newlines (got %r for header %r)ascii, c                 3   s    | ]}t | V  qd S N)sanitize_address).0addrencodingr"   r#   	<genexpr>D   s    

z,forbid_multi_line_headers.<locals>.<genexpr>subject)r   DEFAULT_CHARSETstrr   encodeUnicodeEncodeErrorlowerADDRESS_HEADERSjoinr   r	   )namevalr8   r"   r7   r#   forbid_multi_line_headers8   s*   


rD   c           
   
   C   s\  d}t | ts=t| } z	t| \}}W n tttfy$   td|  w |r/td|| f |jp3d}|j	}|j
p;d}n| \}}d|vrMtd| d|dd\}}|| | }d	|v scd
|v rgtdz|d t| }W n ty   t|| }Y nw z|d W n ty   t|| }Y nw t|}t||d}	t||	jfS )zF
    Format a pair of (name, address) or an email address string.
    NzInvalid address "%s"z2Invalid address; only %s could be parsed from "%s" @zInvalid address ""   r/   r0   z7Invalid address; address parts cannot contain newlines.r1   )usernamedomain)
isinstancetupler   r   get_mailboxr   
ValueError
IndexErrordisplay_name
local_partrJ   rsplitr=   r	   r>   r   r
   r   	addr_spec)
r6   r8   addresstokenrestnm	localpartrJ   address_partsparsed_addressr"   r"   r#   r4   O   sH   



r4   c                   @   s    e Zd ZdddZdddZdS )		MIMEMixinFr/   c                 C   ,   t  }tj|dd}|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selfr`   ra   fpgr"   r"   r#   	as_string~      zMIMEMixin.as_stringc                 C   r\   )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.
        Fr]   r_   )r   r   BytesGeneratorrc   rd   re   r"   r"   r#   as_bytes   rj   zMIMEMixin.as_bytesN)Fr/   )r   r    r!   ri   rl   r"   r"   r"   r#   r[   }   s    
r[   c                   @   s   e Zd Zdd ZdS )SafeMIMEMessagec                 C   s"   t ||d\}}t| || d S )Nr1   )rD   r   __setitem__rf   rB   rC   r"   r"   r#   rn      s   zSafeMIMEMessage.__setitem__N)r   r    r!   rn   r"   r"   r"   r#   rm      s    rm   c                   @   s(   e Zd Zd	ddZdd Zd
ddZdS )SafeMIMETextplainNc                 C   s   || _ tj| |||d d S )N)_subtype_charset)r8   r   __init__)rf   _textrr   rs   r"   r"   r#   rt      s   zSafeMIMEText.__init__c                 C   $   t ||| j\}}t| || d S r3   )rD   r8   r   rn   ro   r"   r"   r#   rn         zSafeMIMEText.__setitem__c                 C   sJ   |dkrt |tjstdd | D }|rtnt}tj| ||d d S )Nr   c                 s   s$    | ]}t |jd dtkV  qdS )surrogateescape)errorsN)lenr=   RFC5322_EMAIL_LINE_LENGTH_LIMIT)r5   liner"   r"   r#   r9      s    
z+SafeMIMEText.set_payload.<locals>.<genexpr>r   )rK   Charsetany
splitlinesutf8_charset_qputf8_charsetr   set_payload)rf   payloadr   has_long_linesr"   r"   r#   r      s   zSafeMIMEText.set_payload)rq   Nr3   )r   r    r!   rt   rn   r   r"   r"   r"   r#   rp      s    
rp   c                   @   s    e Zd Z	dddZdd ZdS )SafeMIMEMultipartmixedNc                 K   s"   || _ tj| |||fi | d S r3   )r8   r   rt   )rf   rr   boundary	_subpartsr8   _paramsr"   r"   r#   rt      s   zSafeMIMEMultipart.__init__c                 C   rv   r3   )rD   r8   r   rn   ro   r"   r"   r#   rn      rw   zSafeMIMEMultipart.__setitem__)r   NNN)r   r    r!   rt   rn   r"   r"   r"   r#   r      s    
r   EmailAlternativecontentmimetypeEmailAttachment)filenamer   r   c                   @   s   e Zd ZdZdZdZdZ										dddZd d	d
Zdd Z	dd Z
d ddZd!ddZd"ddZdd Zdd Zdd Zd"ddZdd ZdS )#EmailMessagez"A container for email information.rq   r   NrE   c                 C   s  |rt |trtdt|| _ng | _|	r%t |	trtdt|	| _ng | _|r9t |tr3tdt|| _ng | _|
rMt |
trGtdt|
| _ng | _|pTtj	| _
|| _|p\d| _g | _|rx|D ]}t |trr| | qe| j|  qe|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 tuplerE   N)rK   r<   	TypeErrorlistr*   r)   r+   reply_tor   DEFAULT_FROM_EMAIL
from_emailr:   bodyattachmentsr   attachextra_headers
connection)rf   r:   r   r   r*   r+   r   r   headersr)   r   
attachmentr"   r"   r#   rt      s>   







zEmailMessage.__init__Fc                 C   s$   ddl m} | js||d| _| jS )Nr   )get_connection)fail_silently)django.core.mailr   r   )rf   r   r   r"   r"   r#   r     s   zEmailMessage.get_connectionc                 C   s   | j ptj}t| j| j|}| |}| j|d< | j	d| j
|d< | |d| j | |d| j | |d| j dd | jD }d|vrNttjd	|d
< d|vrYttd|d< | j D ]\}}| dvrl|||< q^|S )NSubjectFromToCczReply-Toc                 S   s   g | ]}|  qS r"   )r?   )r5   keyr"   r"   r#   
<listcomp>      z(EmailMessage.message.<locals>.<listcomp>date)	localtimeDatez
message-id)rJ   z
Message-ID>   r)   r*   r,   r.   )r8   r   r;   rp   r   content_subtype_create_messager:   r   getr   _set_list_header_if_not_emptyr*   r)   r   r   EMAIL_USE_LOCALTIMEr   r   itemsr?   )rf   r8   msgheader_namesrB   valuer"   r"   r#   message
  s$   

zEmailMessage.messagec                 C   s   dd | j | j | j D S )z
        Return a list of all recipients of the email (includes direct
        addressees as well as Cc and Bcc entries).
        c                 S   s   g | ]}|r|qS r"   r"   )r5   emailr"   r"   r#   r   +  r   z+EmailMessage.recipients.<locals>.<listcomp>)r*   r)   r+   )rf   r"   r"   r#   
recipients&  s   zEmailMessage.recipientsc                 C   s   |   sdS | || gS )zSend the email message.r   )r   r   send_messages)rf   r   r"   r"   r#   send-  s   zEmailMessage.sendc                 C   s   t |tr|dus|durtd| j| dS |du r!td|p+t|d p+t}|dd\}}|dkrOt |t	rOz|
 }W n tyN   t}Y n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   /rH   text)rK   r   rN   r   append	mimetypes
guess_typeDEFAULT_ATTACHMENT_MIME_TYPEsplitbytesdecodeUnicodeDecodeErrorr   )rf   r   r   r   basetypesubtyper"   r"   r#   r   5  s,   

zEmailMessage.attachc                 C   sP   t |}|d}| }| |j|| W d   dS 1 s!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   rB   )rf   pathr   filer   r"   r"   r#   attach_file]  s
   "zEmailMessage.attach_filec                 C   s
   |  |S r3   )_create_attachmentsrf   r   r"   r"   r#   r   m  s   
zEmailMessage._create_messagec                 C   sr   | j r7| jptj}|}t| j|d}| js| r|| | j D ]}t	|t
r.|| q!|| j|  q!|S N)rr   r8   )r   r8   r   r;   r   mixed_subtyper   is_multipartr   rK   r   _create_attachment)rf   r   r8   body_msgr   r"   r"   r#   r   p  s   


z EmailMessage._create_attachmentsc                 C   s   | dd\}}|dkr| jptj}t|||}|S |dkr>|dkr>t|tr,| }nt|ts7t	t
|}t||}|S t||}|| t| |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   rH   r   r   rfc822)r   r8   r   r;   rp   rK   r   r   r   r   r   rm   r   r   Encodersencode_base64)rf   r   r   r   r   r8   r   r"   r"   r#   _create_mime_attachment~  s    






z$EmailMessage._create_mime_attachmentc                 C   sP   |  ||}|r&z|d W n ty   dd|f}Y nw |jdd|d |S )zg
        Convert the filename, content, mimetype triple into a MIME attachment
        object.
        r1   r   rE   zContent-Dispositionr   )r   )r   r=   r>   
add_header)rf   r   r   r   r   r"   r"   r#   r     s   zEmailMessage._create_attachmentc                 C   sL   z
| j | ||< W dS  ty%   |r"ddd |D ||< Y dS Y dS w )z
        Set msg's header, either from self.extra_headers, if present, or from
        the values argument if not empty.
        r2   c                 s   s    | ]}t |V  qd S r3   )r<   )r5   vr"   r"   r#   r9     s    z=EmailMessage._set_list_header_if_not_empty.<locals>.<genexpr>N)r   KeyErrorrA   )rf   r   headervaluesr"   r"   r#   r     s   z*EmailMessage._set_list_header_if_not_empty)
rE   rE   NNNNNNNN)F)NNNr3   )r   r    r!   __doc__r   r   r8   rt   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r      s6    

6


(
r   c                       s\   e Zd ZdZdZ											d fdd	Zdd Zd	d
 Zdd Zdd Z	  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.
    alternativerE   Nc                    s6   t  |||||||||
|
 dd |	pg D | _dS )r   c                 S   s   g | ]}t | qS r"   )r   )r5   r   r"   r"   r#   r     s    z3EmailMultiAlternatives.__init__.<locals>.<listcomp>N)superrt   alternatives)rf   r:   r   r   r*   r+   r   r   r   r   r)   r   	__class__r"   r#   rt     s   zEmailMultiAlternatives.__init__c                 C   s.   |du s|du rt d| jt|| dS )z-Attach an alternative content representation.Nz+Both content and mimetype must be provided.)rN   r   r   r   )rf   r   r   r"   r"   r#   attach_alternative  s   z)EmailMultiAlternatives.attach_alternativec                 C   s   |  | |S r3   )r   _create_alternativesr   r"   r"   r#   r     s   z&EmailMultiAlternatives._create_messagec                 C   sZ   | j ptj}| jr+|}t| j|d}| jr|| | jD ]}|| |j	|j
 q|S r   )r8   r   r;   r   r   alternative_subtyper   r   r   r   r   )rf   r   r8   r   r   r"   r"   r#   r     s   

z+EmailMultiAlternatives._create_alternativesc                 C   s:   || j vrdS | jD ]\}}|dr||vr dS q
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)rf   r   r   r   r"   r"   r#   body_contains  s   
z$EmailMultiAlternatives.body_contains)rE   rE   NNNNNNNNN)r   r    r!   r   r   rt   r   r   r   r   __classcell__r"   r"   r   r#   r     s&    "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{   rN   r   r@   rD   r4   r[   rm   rp   r   r   r   r   r   r"   r"   r"   r#   <module>   sL    

. s