
 XJ                 @   sn  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z m	 Z
 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 d  d
 l m Z 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& e j d  Z' d e' _( e j d  Z) e j* e) _( d Z+ d Z, Gd d   d e-  Z. d d d d  Z/ d d d d d d d d d d  d! h Z0 d" d#   Z1 d$ d%   Z2 d& d'   Z3 Gd( d)   d)  Z4 Gd* d+   d+ e4 e  Z5 Gd, d-   d- e4 e  Z6 Gd. d/   d/ e4 e  Z7 Gd0 d1   d1 e8  Z9 Gd2 d3   d3 e9  Z: d S)4    )unicode_literalsN)charsetencoders	generatormessage_from_string)Header)Message)MIMEBase)MIMEMessage)MIMEMultipart)MIMEText)
formatdategetaddresses	parseaddr)BytesIO)settings)DNS_NAME)six)
force_textzutf-8zapplication/octet-streami  c               @   s   e  Z d  Z d S)BadHeaderErrorN)__name__
__module____qualname__ r   r   F/home/ubuntu/projects/ifolica/build/django/django/core/mail/message.pyr   &   s   r   c             C   s   t  j    } t  j d t  j |   } t j   } t j d  } |  d k rW d }  n
 d |  }  | d k rv t } n  d | | | |  | f } | S)an  Returns a string suitable for RFC 5322 compliant Message-ID, e.g:

    <20020201195627.33539.96671@nightshade.la.mastaler.com>

    Optional idstring if given is a string used to strengthen the
    uniqueness of the message id.  Optional domain if given provides the
    portion of the message id after the '@'.  It defaults to the locally
    defined hostname.
    z%Y%m%d%H%M%Si N .z<%s.%s.%s%s@%s>)timestrftimegmtimeosgetpidrandom	randranger   )idstringdomaintimevalutcdatepidrandintmsgidr   r   r   
make_msgid.   s    
	
	r+   fromZsenderzreply-totoccbcczresent-fromzresent-senderz	resent-toz	resent-ccz
resent-bccc                s     p t  j   t |  } d | k s3 d | k rL t d | |  f   n  y | j d  Wng t k
 r |  j   t k r d j   f d d   t	 | f  D  } n t
 |    j   } Yn( X|  j   d k r t
 |  j   } n  t |   | f S)	z8Forbids multi-line headers, to prevent header injection.
z;Header values can't contain newlines (got %r for header %r)asciiz, c             3   s   |  ] } t  |    Vq d  S)N)sanitize_address).0addr)encodingr   r   	<genexpr>a   s    z,forbid_multi_line_headers.<locals>.<genexpr>subject)r   DEFAULT_CHARSETr   r   encodeUnicodeEncodeErrorlowerADDRESS_HEADERSjoinr   r   str)namevalr6   r   )r6   r   forbid_multi_line_headersW   s    .rB   c             C   s   d |  k rz |  j  d d  \ } } y | j d  Wn' t k
 r^ t | |  j   } Yn X| j d  j d  } n t |  |  j   } d } | | f S)z
    Split the address into local part and domain, properly encoded.

    When non-ascii characters are present in the local part, it must be
    MIME-word encoded. The domain name must be idna-encoded if it contains
    non-ascii characters.
    @   r2   idnar   )splitr:   r;   r   decode)r5   r6   	localpartr%   r   r   r   
split_addrj   s    rI   c       
      C   s  t  |  t  s$ t t |    }  n  |  \ } }  d \ } } t | |  j   } y |  j d  Wn' t k
 r t |  |  \ } } Yn Xt j	 r d d l
 m } | r | r d j | | g  }  n  | | |  f  Sd d l m } d d l m } m } | r-| r-| | d | d	 | }	 t |	  Sy | | d
 |  }	 WnE | | f k
 rt |  |  \ } } | | d | d	 | }	 Yn Xt |	  S)zF
    Format a pair of (name, address) or an email address string.
    Nr2   r   )
formataddrrC   )Address)InvalidHeaderDefectNonASCIILocalPartDefectusernamer%   Z	addr_spec)NN)
isinstancetupler   r   r   r:   r;   rI   r   PY2email.utilsrJ   r>   Zemail.headerregistryrK   Zemail.errorsrL   rM   r?   )
r5   r6   nmrH   r%   rJ   rK   rL   rM   addressr   r   r   r3      s2    	
r3   c               @   sF   e  Z d  Z d d d d  Z e j r0 e Z n d d d d  Z d S)	MIMEMixinFr0   c             C   sc   t  j   } t j | d d } t  j r@ | j |  d | n | j |  d | d | | 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.
        mangle_from_Funixfromlinesep)r   StringIOr   	GeneratorrQ   flattengetvalue)selfrW   rX   fpgr   r   r   	as_string   s    	zMIMEMixin.as_stringc             C   sA   t    } t j | d d } | j |  d | d | | j   S)a6  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.
            rV   FrW   rX   )r   r   BytesGeneratorr[   r\   )r]   rW   rX   r^   r_   r   r   r   as_bytes   s    	zMIMEMixin.as_bytesN)r   r   r   r`   r   rQ   rb   r   r   r   r   rU      s   		rU   c               @   s   e  Z d  Z d d   Z d S)SafeMIMEMessagec             C   s/   t  | | d  \ } } t j |  | |  d  S)Nr2   )rB   r
   __setitem__)r]   r@   rA   r   r   r   rd      s    zSafeMIMEMessage.__setitem__N)r   r   r   rd   r   r   r   r   rc      s   rc   c               @   s.   e  Z d  Z d d d d  Z d d   Z d S)SafeMIMETextplainNc             C   s   | |  _  | d k r t j |  | | d   |  d =t d d   | j   D  } |  j | | rf t n t  |  j d d | | f  n8 | d  k r t j |  | |  n t j |  | | |  d  S)Nzutf-8zContent-Transfer-Encodingc             s   s!   |  ] } t  |  t k Vq d  S)N)lenRFC5322_EMAIL_LINE_LENGTH_LIMIT)r4   lr   r   r   r7      s    z(SafeMIMEText.__init__.<locals>.<genexpr>zContent-Typeztext/%s; charset="%s")	r6   r   __init__any
splitlinesset_payloadutf8_charset_qputf8_charsetreplace_header)r]   Z_text_subtype_charsetZhas_long_linesr   r   r   rj      s    	zSafeMIMEText.__init__c             C   s2   t  | | |  j  \ } } t j |  | |  d  S)N)rB   r6   r   rd   )r]   r@   rA   r   r   r   rd      s    zSafeMIMEText.__setitem__)r   r   r   rj   rd   r   r   r   r   re      s   re   c               @   s4   e  Z d  Z d d d d d d  Z d d   Z d S)SafeMIMEMultipartmixedNc             K   s&   | |  _  t j |  | | | |  d  S)N)r6   r   rj   )r]   rq   boundaryZ	_subpartsr6   _paramsr   r   r   rj      s    	zSafeMIMEMultipart.__init__c             C   s2   t  | | |  j  \ } } t j |  | |  d  S)N)rB   r6   r   rd   )r]   r@   rA   r   r   r   rd      s    zSafeMIMEMultipart.__setitem__)r   r   r   rj   rd   r   r   r   r   rs      s   rs   c               @   s   e  Z d  Z d Z d Z d Z d Z d d d d d d d d d d d d 
 Z d d	 d
  Z d d   Z	 d d   Z
 d d d  Z d d d d d  Z d d d  Z d d   Z d d   Z d d   Z d d d  Z d S)EmailMessagez,
    A container for email information.
    rf   rt   Nr   c             C   sW  | r9 t  | t j  r' t d   n  t |  |  _ n	 g  |  _ |	 r{ t  |	 t j  ri t d   n  t |	  |  _ n	 g  |  _ | r t  | t j  r t d   n  t |  |  _ n	 g  |  _ |
 r t  |
 t j  r t d   n  t |
  |  _ n	 g  |  _ | pt	 j
 |  _ | |  _ | |  _ | p5g  |  _ | pDi  |  _ | |  _ d S)a  
        Initialize a single email message (which can be sent to multiple
        recipients).

        All strings used to create the message can be unicode strings
        (or UTF-8 bytestrings). The SafeMIMEText class will handle any
        necessary encoding conversions.
        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 tupleN)rO   r   string_types	TypeErrorlistr-   r.   r/   reply_tor   ZDEFAULT_FROM_EMAIL
from_emailr8   bodyattachmentsextra_headers
connection)r]   r8   r}   r|   r-   r/   r   r~   headersr.   r{   r   r   r   rj      s4    						zEmailMessage.__init__Fc             C   s5   d d l  m } |  j s. | d |  |  _ n  |  j S)Nr   )get_connectionfail_silently)Zdjango.core.mailr   r   )r]   r   r   r   r   r   r   )  s    	zEmailMessage.get_connectionc             C   s  |  j  p t j } t |  j |  j |  } |  j |  } |  j | d <|  j j	 d |  j
  | d <|  j j	 d d j t t |  j    | d <|  j r d j t t |  j   | d <n  |  j r |  j j	 d d j t t |  j    | d <n  d d   |  j D } d	 | k r't   | d
 <n  d | k rIt d t  | d <n  x? |  j j   D]. \ } } | j   d k r}qYn  | | | <qYW| S)NZSubjectZFromZToz, CczReply-Toc             S   s   g  |  ] } | j     q Sr   )r<   )r4   keyr   r   r   
<listcomp>=  s   	 z(EmailMessage.message.<locals>.<listcomp>dateZDatez
message-idr%   z
Message-IDr,   r-   )zfromzto)r6   r   r9   re   r}   content_subtype_create_messager8   r   getr|   r>   mapr   r-   r.   r{   r   r+   r   itemsr<   )r]   r6   msgheader_namesr@   valuer   r   r   message/  s(    .	"	1zEmailMessage.messagec             C   s   |  j  |  j |  j S)z
        Returns a list of all recipients of the email (includes direct
        addressees as well as Cc and Bcc entries).
        )r-   r.   r/   )r]   r   r   r   
recipientsI  s    zEmailMessage.recipientsc             C   s)   |  j    s d S|  j |  j |  g  S)zSends the email message.r   )r   r   Zsend_messages)r]   r   r   r   r   sendP  s    zEmailMessage.sendc             C   su   t  | t  rF | d k s! t  | d k s3 t  |  j j |  n+ | d k	 sX t  |  j j | | | f  d S)a  
        Attaches 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 it is inserted directly
        into the resulting message attachments.
        N)rO   r	   AssertionErrorr~   append)r]   filenamecontentmimetyper   r   r   attachX  s    zEmailMessage.attachc       
      C   s  t  j j |  } | s? t j |  \ } } | s? t } q? n  | j d d  \ } } | d k ri d n d } d } t | |  , }	 y |	 j   } Wn t	 k
 r Yn XWd QX| d k r t | d   }	 |	 j   } t } Wd QXn  |  j
 | | |  d S)z
        Attaches a file from the filesystem.

        The mimetype will be set to the DEFAULT_ATTACHMENT_MIME_TYPE if it is
        not specified and cannot be guessed or (PY3 only) if it suggests
        text/* for a binary file.
        /rD   textrrbN)r    pathbasename	mimetypes
guess_typeDEFAULT_ATTACHMENT_MIME_TYPErF   openreadUnicodeDecodeErrorr   )
r]   r   r   r   _basetypesubtypeZ	read_moder   fr   r   r   attach_fileh  s$    zEmailMessage.attach_filec             C   s   |  j  |  S)N)_create_attachments)r]   r   r   r   r   r     s    zEmailMessage._create_messagec             C   s   |  j  r |  j p t j } | } t d |  j d |  } |  j rR | j |  n  xI |  j  D]; } t | t	  r | j |  q\ | j |  j
 |    q\ Wn  | S)Nrq   r6   )r~   r6   r   r9   rs   mixed_subtyper}   r   rO   r	   _create_attachment)r]   r   r6   body_msg
attachmentr   r   r   r     s    		z EmailMessage._create_attachmentsc             C   s   | j  d d  \ } } | d k rK |  j p3 t j } t | | |  } n | d k r | d k r t | t  r | j   } n t | t  s t	 |  } n  t
 | |  } n) t | |  } | j |  t j |  | S)z
        Converts 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   rD   r   r   Zrfc822)rF   r6   r   r9   re   rO   rw   r   r   r   rc   r	   rm   Encodersencode_base64)r]   r   r   r   r   r6   r   r   r   r   _create_mime_attachment  s    z$EmailMessage._create_mime_attachmentc             C   s   | d k r9 t  j |  \ } } | d k r9 t } q9 n  |  j | |  } | r y | j d  Wn< t k
 r t j r | j d  } n  d d | f } Yn X| j d d d | n  | S)zh
        Converts the filename, content, mimetype triple into a MIME attachment
        object.
        Nr2   zutf-8r   zContent-Dispositionr   r   )	r   r   r   r   r:   r;   r   rQ   
add_header)r]   r   r   r   r   r   r   r   r   r     s    	
zEmailMessage._create_attachment)r   r   r   __doc__r   r   r6   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rw      s"   (!rw   c                   ss   e  Z d  Z d Z d Z d d d d d d d d d d d   f d d  Z d d   Z d	 d
   Z d d   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.
    alternativer   Nc                sD   t  t |   j | | | | | | | | |
 | 
 |	 p: g  |  _ d S)a  
        Initialize a single email message (which can be sent to multiple
        recipients).

        All strings used to create the message can be unicode strings (or UTF-8
        bytestrings). The SafeMIMEText class will handle any necessary encoding
        conversions.
        N)superr   rj   alternatives)r]   r8   r}   r|   r-   r/   r   r~   r   r   r.   r{   )	__class__r   r   rj     s    zEmailMultiAlternatives.__init__c             C   s>   | d k	 s t   | d k	 s$ t   |  j j | | f  d S)z-Attach an alternative content representation.N)r   r   r   )r]   r   r   r   r   r   attach_alternative  s    z)EmailMultiAlternatives.attach_alternativec             C   s   |  j  |  j |   S)N)r   _create_alternatives)r]   r   r   r   r   r     s    z&EmailMultiAlternatives._create_messagec             C   s   |  j  p t j } |  j r | } t d |  j d |  } |  j rR | j |  n  x* |  j D] } | j |  j |    q\ Wn  | S)Nrq   r6   )	r6   r   r9   r   rs   alternative_subtyper}   r   r   )r]   r   r6   r   r   r   r   r   r     s    		z+EmailMultiAlternatives._create_alternatives)	r   r   r   r   r   rj   r   r   r   r   r   )r   r   r     s   r   );
__future__r   r   r    r"   r   emailr   Charsetr   r   r   r   Zemail.headerr   Zemail.messager   Zemail.mime.baser	   Zemail.mime.messager
   Zemail.mime.multipartr   Zemail.mime.textr   rR   r   r   r   ior   Zdjango.confr   Zdjango.core.mail.utilsr   Zdjango.utilsr   Zdjango.utils.encodingr   ro   body_encodingrn   QPr   rh   
ValueErrorr   r+   r=   rB   rI   r3   rU   rc   re   rs   objectrw   r   r   r   r   r   <module>   sZ   "		&"