B
    u9a²¬  ã               @   sj  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ ddddddd	d
ddddgZdZdZdZdZdZe dej¡ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd	„ d	eƒZ G dd
„ d
eƒZ!G dd„ deƒZ"d d„ Z#d!d"„ Z$d#d„ Z%d$d%„ Z&d&d'„ Z'yd dl(Z(W n e)k
r   d(Z*Y nX d)Z*G d*d„ dƒZ+e*rÄG d+d,„ d,e+ƒZ,e -d,¡ d-Z.G d.d/„ d/e+ƒZ/e0d0krfd1d2„ Z1e1d3ƒZ2e1d4ƒ 3d5¡Z4e5d6ƒ d7Z6x ej7 8¡ Z9e9s P e6e9 Z6qW e5d8e:e6ƒ ƒ e+d9ƒZ;e; <d:¡ e; =e2e4e6¡ e; >¡  dS );é    N)Úbody_encodeÚSMTPExceptionÚSMTPServerDisconnectedÚSMTPResponseExceptionÚSMTPSenderRefusedÚSMTPRecipientsRefusedÚSMTPDataErrorÚSMTPConnectErrorÚSMTPHeloErrorÚSMTPAuthenticationErrorÚ	quoteaddrÚ	quotedataÚSMTPé   iÑ  z
s   
i    z	auth=(.*)c               @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r   r   ú/usr/lib/python3.7/smtplib.pyr   G   s   c               @   s   e Zd ZdS )ÚSMTPNotSupportedErrorN)r   r   r   r   r   r   r   r   J   s   r   c               @   s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r   Q   s   c               @   s   e Zd Zdd„ ZdS )r   c             C   s   || _ || _||f| _d S )N)Ú	smtp_codeÚ
smtp_errorÚargs)ÚselfÚcodeÚmsgr   r   r   Ú__init__b   s    zSMTPResponseException.__init__N)r   r   r   r   r   r   r   r   r   Y   s   	c               @   s   e Zd Zdd„ ZdS )r   c             C   s"   || _ || _|| _|||f| _d S )N)r   r   Úsenderr   )r   r   r   r   r   r   r   r   n   s    zSMTPSenderRefused.__init__N)r   r   r   r   r   r   r   r   r   g   s   c               @   s   e Zd Zdd„ ZdS )r   c             C   s   || _ |f| _d S )N)Ú
recipientsr   )r   r   r   r   r   r   |   s    zSMTPRecipientsRefused.__init__N)r   r   r   r   r   r   r   r   r   t   s   c               @   s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r      s   c               @   s   e Zd ZdS )r	   N)r   r   r   r   r   r   r   r	   „   s   c               @   s   e Zd ZdS )r
   N)r   r   r   r   r   r   r   r
   ‡   s   c               @   s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r   Š   s   c             C   s>   t j | ¡\}}||fdkr6|  ¡  d¡r.| S d|  S d| S )N)Ú r   ú<z<%s>)ÚemailÚutilsÚ	parseaddrÚstripÚ
startswith)Ú
addrstringÚdisplaynameÚaddrr   r   r   r   ‘   s    c             C   s$   t j | ¡\}}||fdkr | S |S )N)r   r   )r!   r"   r#   )r&   r'   r(   r   r   r   Ú
_addr_onlyž   s    r)   c          	   C   s   t  ddt  dt| ¡¡S )Nz(?m)^\.z..z(?:\r\n|\n|\r(?!\n)))ÚreÚsubÚCRLF)Údatar   r   r   r   ¦   s    c             C   s   t  dd| ¡S )Ns   (?m)^\.s   ..)r*   r+   )Zbindatar   r   r   Ú_quote_periods¯   s    r.   c             C   s   t  dt| ¡S )Nz(?:\r\n|\n|\r(?!\n)))r*   r+   r,   )r-   r   r   r   Ú	_fix_eols²   s    r/   FTc               @   sr  e Zd ZdZdZdZdZdZdZe	Z
dddejdfdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ ZdMdd„Zdd„ ZdNdd„Zdd„ ZdOdd„ZdPdd„ZdQdd„Zd d!„ ZdRd"d#„Zd$d%„ Zd&d'„ Zd(d)„ ZdSd+d,„ZdTd-d.„Z d/d0„ Z!d1d2„ Z"e"Z#d3d4„ Z$d5d6„ Z%d7d8œd9d:„Z&dUd;d<„Z'dVd=d>„Z(dWd?d@„Z)d7d8œdAdB„Z*dXdCdD„Z+dYdEdF„Z,dZdGdH„Z-dIdJ„ Z.dKdL„ Z/dS )[r   r   NÚehlor   c       
      C   s°   || _ || _i | _d| _|| _|rL|  ||¡\}}|dkrL|  ¡  t||ƒ‚|d k	r\|| _nPt	 
¡ }d|krt|| _n8d}	yt	 t	 ¡ ¡}	W n t	jk
r    Y nX d|	 | _d S )NÚasciiéÜ   Ú.z	127.0.0.1z[%s])Ú_hostÚtimeoutÚesmtp_featuresÚcommand_encodingÚsource_addressÚconnectÚcloser	   Úlocal_hostnameÚsocketZgetfqdnZgethostbynameZgethostnameZgaierror)
r   ÚhostÚportr;   r5   r8   r   r   Zfqdnr(   r   r   r   r   â   s*    
zSMTP.__init__c             C   s   | S )Nr   )r   r   r   r   Ú	__enter__  s    zSMTP.__enter__c             G   sN   z>y$|   d¡\}}|dkr$t||ƒ‚W n tk
r:   Y nX W d |  ¡  X d S )NZQUITéÝ   )Údocmdr   r   r:   )r   r   r   Úmessager   r   r   Ú__exit__  s    
zSMTP.__exit__c             C   s
   || _ d S )N)Ú
debuglevel)r   rD   r   r   r   Úset_debuglevel  s    zSMTP.set_debuglevelc             G   s@   | j dkr,ttj ¡  ¡ f|ždtjiŽ nt|dtjiŽ d S )Né   Úfile)rD   ÚprintÚdatetimeZnowZtimeÚsysÚstderr)r   r   r   r   r   Ú_print_debug'  s    
"zSMTP._print_debugc             C   s2   | j dkr|  d||f| j¡ t ||f|| j¡S )Nr   zconnect: to)rD   rL   r8   r<   Úcreate_connection)r   r=   r>   r5   r   r   r   Ú_get_socket-  s    
zSMTP._get_socketÚ	localhostc             C   sæ   |r
|| _ |s|| d¡| d¡kr|| d¡}|dkr||d |… ||d d …  }}yt|ƒ}W n tk
rz   tdƒ‚Y nX |s†| j}| jdkr |  d||f¡ |  	||| j
¡| _d | _|  ¡ \}}| jdkrÞ|  dt|ƒ¡ ||fS )Nú:r   rF   znonnumeric portzconnect:)r8   ÚfindÚrfindÚintÚ
ValueErrorÚOSErrorÚdefault_portrD   rL   rN   r5   ÚsockrG   ÚgetreplyÚrepr)r   r=   r>   r8   Úir   r   r   r   r   r9   5  s(    


zSMTP.connectc             C   s„   | j dkr|  dt|ƒ¡ t| dƒrx| jrxt|tƒr@| | j¡}y| j 	|¡ W q€ t
k
rt   |  ¡  tdƒ‚Y q€X ntdƒ‚d S )Nr   zsend:rW   zServer not connectedzplease run connect() first)rD   rL   rY   ÚhasattrrW   Ú
isinstanceÚstrÚencoder7   ZsendallrU   r:   r   )r   Úsr   r   r   ÚsendW  s    

z	SMTP.sendc             C   s2   |dkrd|t f }nd||t f }|  |¡ d S )Nr   z%s%sz%s %s%s)r,   r`   )r   Úcmdr   r]   r   r   r   Úputcmdi  s    zSMTP.putcmdc          
   C   sP  g }| j d kr| j d¡| _ x y| j  td ¡}W n: tk
rn } z|  ¡  tdt|ƒ ƒ‚W d d }~X Y nX |s„|  ¡  tdƒ‚| j	dkrž|  
dt|ƒ¡ t|ƒtkr¼|  ¡  tddƒ‚| |d	d …  d
¡¡ |d d… }yt|ƒ}W n tk
r   d}P Y nX |dd	… dkr P q W d |¡}| j	dkrH|  
d||f ¡ ||fS )NÚrbrF   z Connection unexpectedly closed: zConnection unexpectedly closedr   zreply:iô  zLine too long.é   s    	
é   éÿÿÿÿó   -ó   
zreply: retcode (%s); Msg: %a)rG   rW   ZmakefileÚreadlineÚ_MAXLINErU   r:   r   r]   rD   rL   rY   Úlenr   Úappendr$   rS   rT   Újoin)r   ÚrespÚlineÚer   ZerrcodeÚerrmsgr   r   r   rX   q  s>    



zSMTP.getreplyc             C   s   |   ||¡ |  ¡ S )N)rb   rX   )r   ra   r   r   r   r   rA   ¢  s    z
SMTP.docmdc             C   s,   |   d|p| j¡ |  ¡ \}}|| _||fS )NÚhelo)rb   r;   rX   Ú	helo_resp)r   Únamer   r   r   r   r   rr   ¨  s    z	SMTP.heloc       
      C   s2  i | _ |  | j|p| j¡ |  ¡ \}}|dkrJt|ƒdkrJ|  ¡  tdƒ‚|| _|dkr`||fS d| _	| j 
d¡ d¡}|d= xª|D ]¢}t |¡}|r¾| j  dd	¡d
 | d¡d  | j d< q„t d|¡}|r„| d¡ ¡ }|j| d¡d …  ¡ }	|dkr| j  |d	¡d
 |	 | j |< q„|	| j |< q„W ||fS )Nrf   r   zServer not connectedéú   rF   zlatin-1Ú
Úauthr   ú z((?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*) ?Úfeature)r6   rb   Úehlo_msgr;   rX   rk   r:   r   Ú	ehlo_respÚ
does_esmtpÚdecodeÚsplitÚOLDSTYLE_AUTHÚmatchÚgetÚgroupsr*   ÚgroupÚlowerÚstringÚendr$   )
r   rt   r   r   rn   ÚeachZ
auth_matchÚmry   Úparamsr   r   r   r0   ²  s2    

&
z	SMTP.ehloc             C   s   |  ¡ | jkS )N)r„   r6   )r   Úoptr   r   r   Úhas_extnå  s    zSMTP.has_extnc             C   s   |   d|¡ |  ¡ d S )NÚhelprF   )rb   rX   )r   r   r   r   r   rŒ   é  s    z	SMTP.helpc             C   s   d| _ |  d¡S )Nr1   Úrset)r7   rA   )r   r   r   r   r   ï  s    z	SMTP.rsetc             C   s&   y|   ¡  W n tk
r    Y nX d S )N)r   r   )r   r   r   r   Ú_rsetô  s    z
SMTP._rsetc             C   s
   |   d¡S )NÚnoop)rA   )r   r   r   r   r      s    z	SMTP.noopr   c             C   sh   d}|rH| j rHtdd„ |D ƒƒr:|  d¡r2d| _ntdƒ‚dd |¡ }|  dd	t|ƒ|f ¡ |  ¡ S )
Nr   c             s   s   | ]}|  ¡ d kV  qdS )Úsmtputf8N)r„   )Ú.0Úxr   r   r   ú	<genexpr>  s    zSMTP.mail.<locals>.<genexpr>r   zutf-8z SMTPUTF8 not supported by serverrx   Úmailz	FROM:%s%s)	r|   Úanyr‹   r7   r   rm   rb   r   rX   )r   r   ÚoptionsÚ
optionlistr   r   r   r”     s    	

z	SMTP.mailc             C   s<   d}|r| j rdd |¡ }|  ddt|ƒ|f ¡ |  ¡ S )Nr   rx   ÚrcptzTO:%s%s)r|   rm   rb   r   rX   )r   Zrecipr–   r—   r   r   r   r˜     s
    
z	SMTP.rcptc             C   sÄ   |   d¡ |  ¡ \}}| jdkr0|  d||f¡ |dkrDt||ƒ‚n|t|tƒr\t|ƒ d¡}t	|ƒ}|dd … t
kr||t
 }|d t
 }|  |¡ |  ¡ \}}| jdkr¸|  d||f¡ ||fS d S )Nr-   r   zdata:ib  r1   éþÿÿÿó   .)rb   rX   rD   rL   r   r\   r]   r/   r^   r.   ÚbCRLFr`   )r   r   r   ÚreplÚqr   r   r   r-   !  s"    





z	SMTP.datac             C   s   |   dt|ƒ¡ |  ¡ S )NÚvrfy)rb   r)   rX   )r   Úaddressr   r   r   Úverify>  s    zSMTP.verifyc             C   s   |   dt|ƒ¡ |  ¡ S )NÚexpn)rb   r)   rX   )r   rŸ   r   r   r   r¡   E  s    z	SMTP.expnc             C   sb   | j d kr^| jd kr^d|  ¡ d   kr0dks^n |  ¡ \}}d|  krRdks^n t||ƒ‚d S )NéÈ   r   i+  )rs   r{   r0   rr   r
   )r   r   rn   r   r   r   Úehlo_or_helo_if_neededL  s
    zSMTP.ehlo_or_helo_if_neededT)Úinitial_response_okc      	      C   s®   |  ¡ }|r|ƒ nd }|d k	rJt| d¡dd}|  d|d | ¡\}}n|  d|¡\}}|dkrt |¡}t||ƒ d¡dd}|  |¡\}}|dkr ||fS t||ƒ‚d S )Nr1   r   )ZeolZAUTHrx   iN  )éë   i÷  )ÚupperÚencode_base64r^   rA   Úbase64Zdecodebytesr   )	r   Z	mechanismZ
authobjectr¤   Zinitial_responseZresponser   rn   Ú	challenger   r   r   rw   ]  s    
z	SMTP.authc             C   s0   |d krd S | j d t | j d¡|d¡ ¡  S )Nrx   r1   Zmd5)ÚuserÚhmacZHMACÚpasswordr^   Z	hexdigest)r   r©   r   r   r   Úauth_cram_md5„  s    zSMTP.auth_cram_md5c             C   s   d| j | jf S )Nz %s %s)rª   r¬   )r   r©   r   r   r   Ú
auth_plain  s    zSMTP.auth_plainc             C   s   |d kr| j S | jS d S )N)rª   r¬   )r   r©   r   r   r   Ú
auth_login’  s    zSMTP.auth_loginc               sÞ   |   ¡  |  d¡stdƒ‚| jd  ¡ ‰ dddg}‡ fdd„|D ƒ}|sPtdƒ‚|| | _| _xv|D ]n}d	| ¡  	d
d¡ }y.| j
|t| |ƒ|d\}}	|dkr¨||	fS W qd tk
rÐ }
 z|
}W d d }
~
X Y qdX qdW |‚d S )Nrw   z,SMTP AUTH extension not supported by server.zCRAM-MD5ZPLAINZLOGINc                s   g | ]}|ˆ kr|‘qS r   r   )r‘   rw   )Úadvertised_authlistr   r   ú
<listcomp>Ã  s    zSMTP.login.<locals>.<listcomp>z(No suitable authentication method found.Zauth_ú-Ú_)r¤   )r¥   i÷  )r£   r‹   r   r6   r~   r   rª   r¬   r„   Úreplacerw   Úgetattrr   )r   rª   r¬   r¤   Zpreferred_authsZauthlistZ
authmethodZmethod_namer   rn   rp   Zlast_exceptionr   )r°   r   Úloginš  s*    



z
SMTP.loginc             C   sî   |   ¡  |  d¡stdƒ‚|  d¡\}}|dkrÜts<tdƒ‚|d k	rT|d k	rTtdƒ‚|d k	rl|d k	rltdƒ‚|d k	s||d k	r’dd l}| d	t	d
¡ |d kr¨t
j||d}|j| j| jd| _d | _d | _d | _i | _d| _n
t||ƒ‚||fS )NÚstarttlsz+STARTTLS extension not supported by server.ZSTARTTLSr2   z&No SSL support included in this Pythonz4context and keyfile arguments are mutually exclusivez5context and certfile arguments are mutually exclusiver   zAkeyfile and certfile are deprecated, use a custom context insteadé   )ÚcertfileÚkeyfile)Úserver_hostname)r£   r‹   r   rA   Ú	_have_sslÚRuntimeErrorrT   ÚwarningsÚwarnÚDeprecationWarningÚsslÚ_create_stdlib_contextÚwrap_socketrW   r4   rG   rs   r{   r6   r|   r   )r   rº   r¹   Úcontextrn   Zreplyr¾   r   r   r   r·   Ü  s8    

zSMTP.starttlsc             C   sf  |   ¡  g }t|tƒr$t|ƒ d¡}| jr^|  d¡rF| dt|ƒ ¡ x|D ]}| |¡ qLW |  	||¡\}}	|dkrœ|dkrˆ|  
¡  n|  ¡  t||	|ƒ‚i }
t|tƒr°|g}xP|D ]H}|  ||¡\}}	|dkræ|dkræ||	f|
|< |dkr¶|  
¡  t|
ƒ‚q¶W t|
ƒt|ƒkr$|  ¡  t|
ƒ‚|  |¡\}}	|dkrb|dkrP|  
¡  n|  ¡  t||	ƒ‚|
S )Nr1   Úsizezsize=%dru   i¥  éû   )r£   r\   r]   r/   r^   r|   r‹   rl   rk   r”   r:   rŽ   r   r˜   r   r-   r   )r   Ú	from_addrÚto_addrsr   Úmail_optionsÚrcpt_optionsZ
esmtp_optsÚoptionr   rn   Zsenderrsr‡   r   r   r   Úsendmail  sF    @









zSMTP.sendmailc          	   C   sœ  |   ¡  | d¡}|d kr d}nt|ƒdkr2d}ntdƒ‚|d kr||d |krZ||d  n
||d  }tj |g¡d d }|d krÆd	d
„ ||d  ||d  ||d  fD ƒ}dd
„ tj |¡D ƒ}t |¡}	|	d= |	d= d}
yd |f|•¡ 	d¡ W n. t
k
r(   |  d¡s tdƒ‚d}
Y nX t ¡ R}|
r^tjj||jjddd}|d˜}ntj |¡}|j|	dd | ¡ }W d Q R X |  |||||¡S )NzResent-Dater   rF   zResent-z0message has more than one 'Resent-' header blockZSenderÚFromr   c             S   s   g | ]}|d k	r|‘qS )Nr   )r‘   Úfr   r   r   r±   ª  s    z%SMTP.send_message.<locals>.<listcomp>ÚToZBccZCcc             S   s   g | ]}|d  ‘qS )rF   r   )r‘   Úar   r   r   r±   ®  s    z
Resent-BccFr1   r   z”One or more source or delivery addresses require internationalized email support, but the server does not advertise the required SMTPUTF8 capabilityT)Úutf8)ÚpolicyÚSMTPUTF8úBODY=8BITMIMEz
)Úlinesep)rÓ   rÔ   )r£   Zget_allrk   rT   r!   r"   ZgetaddressesÚcopyrm   r^   ÚUnicodeEncodeErrorr‹   r   ÚioÚBytesIOÚ	generatorZBytesGeneratorrÒ   ZcloneZflattenÚgetvaluerÌ   )r   r   rÇ   rÈ   rÉ   rÊ   ZresentZheader_prefixZaddr_fieldsZmsg_copyÚinternationalZbytesmsgÚgZflatmsgr   r   r   Úsend_message|  sJ    





zSMTP.send_messagec             C   s<   z| j }d | _ |r| ¡  W d | j}d | _|r6| ¡  X d S )N)rG   r:   rW   )r   rG   rW   r   r   r   r:   É  s    z
SMTP.closec             C   s.   |   d¡}d  | _| _i | _d| _|  ¡  |S )NÚquitF)rA   r{   rs   r6   r|   r:   )r   Zresr   r   r   rß   Ö  s    
z	SMTP.quit)rO   r   N)r   )r   )r   )r   )r   )r   )r   )N)N)N)NNN)r   r   )NNr   r   )0r   r   r   rD   rG   rs   rz   r{   r|   Ú	SMTP_PORTrV   r<   Ú_GLOBAL_DEFAULT_TIMEOUTr   r?   rC   rE   rL   rN   r9   r`   rb   rX   rA   rr   r0   r‹   rŒ   r   rŽ   r   r”   r˜   r-   r    rž   r¡   r£   rw   r­   r®   r¯   r¶   r·   rÌ   rÞ   r:   rß   r   r   r   r   r   ½   s\   -
	
"
1



3


'
	

B
8 
g 
Lc               @   s4   e Zd ZeZdddddejddfdd„Zdd„ ZdS )ÚSMTP_SSLr   r   Nc	       
      C   s–   |d k	r|d k	rt dƒ‚|d k	r0|d k	r0t dƒ‚|d k	s@|d k	rVdd l}	|	 dtd¡ || _|| _|d krxtj||d}|| _t	 
| |||||¡ d S )Nz4context and keyfile arguments are mutually exclusivez5context and certfile arguments are mutually exclusiver   zAkeyfile and certfile are deprecated, use a custom context insteadr¸   )r¹   rº   )rT   r¾   r¿   rÀ   rº   r¹   rÁ   rÂ   rÄ   r   r   )
r   r=   r>   r;   rº   r¹   r5   r8   rÄ   r¾   r   r   r   r   ò  s     zSMTP_SSL.__init__c             C   sD   | j dkr|  d||f¡ t ||f|| j¡}| jj|| jd}|S )Nr   zconnect:)r»   )rD   rL   r<   rM   r8   rÄ   rÃ   r4   )r   r=   r>   r5   Z
new_socketr   r   r   rN   	  s    

zSMTP_SSL._get_socket)	r   r   r   ÚSMTP_SSL_PORTrV   r<   rá   r   rN   r   r   r   r   râ   â  s   râ   iÓ  c               @   s,   e Zd ZdZdeddfdd„Zd
dd	„ZdS )ÚLMTPZlhlor   Nc             C   s   t j| ||||d d S )N)r;   r8   )r   r   )r   r=   r>   r;   r8   r   r   r   r   )  s    zLMTP.__init__rO   r   c             C   s´   |d dkrt j| |||dS y(t tjtj¡| _d | _| j |¡ W nB tk
rˆ   | jdkrl|  	d|¡ | jr|| j 
¡  d | _‚ Y nX |  ¡ \}}| jdkr¬|  	d|¡ ||fS )Nr   ú/)r8   zconnect fail:zconnect:)r   r9   r<   ZAF_UNIXZSOCK_STREAMrW   rG   rU   rD   rL   r:   rX   )r   r=   r>   r8   r   r   r   r   r   r9   /  s"    


zLMTP.connect)rO   r   N)r   r   r   rz   Ú	LMTP_PORTr   r9   r   r   r   r   rä     s   rä   Ú__main__c             C   s(   t j | d ¡ t j ¡  t j ¡  ¡ S )Nz: )rJ   ÚstdoutÚwriteÚflushÚstdinri   r$   )Úpromptr   r   r   rì   I  s    
rì   rÍ   rÏ   ú,zEnter message, end with ^D:r   zMessage length is %drO   rF   )?r<   rØ   r*   Zemail.utilsr!   Zemail.messageZemail.generatorr¨   r«   rÖ   rI   rJ   Zemail.base64mimer   r§   Ú__all__rà   rã   r,   r›   rj   ÚcompileÚIr   rU   r   r   r   r   r   r   r   r	   r
   r   r   r)   r   r.   r/   rÁ   ÚImportErrorr¼   r   râ   rl   ræ   rä   r   rì   Zfromaddrr~   ZtoaddrsrH   r   rë   ri   ro   rk   ZserverrE   rÌ   rß   r   r   r   r   Ú<module>,   sŠ   
	
      )0
/


