U AT^&c @sjddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Zddddddd d d d d ddg ZdZdZdZdZdZedejZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZGdd d eZ Gdd d eZ!Gdd d eZ"d d Z#d!d"Z$d#dZ%d$d%Z&d&d'Z'z ddl(Z(Wne)k rd(Z*YnXd)Z*Gd*ddZ+e*rGd+d,d,e+Z,e-d,d-Z.Gd.d/d/e+Z/e0d0krfd1d2Z1e1d3Z2e1d43d5Z4e5d6d7Z6e j78Z9e9s"q.e6e9Z6qe5d8e:e6e+d9Z;e;dS);N) body_encode SMTPExceptionSMTPNotSupportedErrorSMTPServerDisconnectedSMTPResponseExceptionSMTPSenderRefusedSMTPRecipientsRefused SMTPDataErrorSMTPConnectError SMTPHeloErrorSMTPAuthenticationError quoteaddr quotedataSMTPii s i z auth=(.*)c@ eZdZdS)rN__name__ __module__ __qualname__rr/usr/lib64/python3.8/smtplib.pyrGc@r)rNrrrrrrJrc@r)rNrrrrrrQrc@eZdZddZdS)rcCs||_||_||f|_dSN) smtp_code smtp_errorargs)selfcodemsgrrr__init__bszSMTPResponseException.__init__Nrrrr rrrrrYs c@r)rcCs"||_||_||_|||f|_dSr)rrsenderr)rrrr"rrrr nszSMTPSenderRefused.__init__Nr!rrrrrgsc@r)rcCs||_|f|_dSr) recipientsr)rr#rrrr |szSMTPRecipientsRefused.__init__Nr!rrrrrtsc@r)rNrrrrrrrc@r)r Nrrrrrr rc@r)r Nrrrrrr rc@r)r Nrrrrrr rcCs>tj|\}}||fdkr6|dr.|Sd|Sd|S)Nr%z)emailutils parseaddrstripZ startswithZ addrstringZ displaynameaddrrrrr s  cCs$tj|\}}||fdkr |S|S)Nr$)r&r'r(r*rrr _addr_onlys r,c Cstddtdt|S)Nz(?m)^\.z..(?:\r\n|\n|\r(?!\n))resubCRLFdatarrrr s cCstdd|S)Ns(?m)^\.s..)r/r0)Zbindatarrr_quote_periodsr4cCstdt|S)Nr-r.r2rrr _fix_eolsr5r6FTc@sveZdZdZdZdZdZdZdZdZ e Z ddde j dfddZddZd d Zd d Zd dZddZdMddZddZdNddZddZdOddZdPddZdQddZd d!ZdRd"d#Zd$d%Zd&d'Zd(d)ZdSd+d,Z dTd-d.Z!d/d0Z"d1d2Z#e#Z$d3d4Z%d5d6Z&d7d8d9d:Z'dUd;d<Z(dVd=d>Z)dWd?d@Z*d7d8dAdBZ+dXdCdDZ,dYdEdFZ-dZdGdHZ.dIdJZ/dKdLZ0dS)[rrNehlor%c Cs||_||_i|_d|_||_|rL|||\}}|dkrL|t|||dk r\||_nPt }d|krt||_n8d} zt t } Wnt j k rYnXd| |_dS)NasciiZ.z 127.0.0.1z[%s])_hosttimeoutesmtp_featurescommand_encodingsource_addressconnectcloser local_hostnamesocketZgetfqdnZ gethostbynameZ gethostnameZgaierror) rhostportrAr;r>rrZfqdnr+rrrr s* z SMTP.__init__cCs|Srrrrrr __enter__r5zSMTP.__enter__cGsNz>z$|d\}}|dkr$t||Wntk r:YnXW5|XdS)NZQUITi)r@docmdrr)rrrZmessagerrr__exit__s z SMTP.__exit__cCs ||_dSr) debuglevel)rrIrrrset_debuglevel szSMTP.set_debuglevelcGs@|jdkr,ttjf|dtjint|dtjidS)Nfile)rIprintdatetimeZnowZtimesysZstderrrrrrr _print_debug)s "zSMTP._print_debugcCs2|jdkr|d||f|jt||f||jS)Nrz connect: to)rIrQr>rBcreate_connection)rrCrDr;rrr _get_socket/s   zSMTP._get_socket localhostcCs|r ||_|s||d|dkr||d}|dkr||d|||dd}}z t|}Wntk rztdYnX|s|j}|jdkr|d||ft d|||| |||j |_ d|_|\}}|jdkr|dt|||fS)Nz:rrKznonnumeric portconnect:zsmtplib.connect)r>ZfindZrfindint ValueErrorOSError default_portrIrQrOauditrSr;sockrLgetreplyrepr)rrCrDr>Zirrrrrr?7s*      z SMTP.connectcCs|jdkr|dt||jr|t|tr6||j}t d||z|j |Wqt k rx| t dYqXnt ddS)Nrzsend:z smtplib.sendServer not connectedzplease run connect() first)rIrQr]r[ isinstancestrencoder=rOrZZsendallrXr@r)rZsrrrsendZs   z SMTP.sendcCs2|dkrd|tf}nd||tf}||dS)Nr%z%s%sz%s %s%s)r1rb)rcmdrr`rrrputcmdmsz SMTP.putcmdc CsPg}|jdkr|jd|_z|jtd}Wn:tk rj}z|tdt|W5d}~XYnX|s|td|j dkr| dt |t |tkr|t dd||d dd |dd }z t|}Wn tk rd }Yq YnX|d d d krq qd|}|j dkrH| d||f||fS)NZrbrKz Connection unexpectedly closed: zConnection unexpectedly closedrzreply:izLine too long.is is-s zreply: retcode (%s); Msg: %a)rLr[Zmakefilereadline_MAXLINErXr@rr`rIrQr]lenrappendr)rVrWjoin)rresplineerZerrcodeZerrmsgrrrr\us>          z SMTP.getreplycCs||||Srrdr\)rrcrrrrrGs z SMTP.docmdcCs,|d|p|j|\}}||_||fS)Nhelo)rdrAr\ helo_resp)rnamerrrrrros z SMTP.heloc Cs.i|_||j|p|j|\}}|dkrJt|dkrJ|td||_|dkr`||fSd|_ |j d d}|d=|D]}t |}|r|jdd d |dd|jd<qt d |}|r|d }|j|d d} |dkr|j|d d | |j|<q| |j|<q||fS) Nrerr^rKzlatin-1Z authr% z((?P[A-Za-z0-9][A-Za-z0-9\-]*) ?feature)r<rdehlo_msgrAr\rhr@r ehlo_resp does_esmtpZdecodesplit OLDSTYLE_AUTHZmatchZgetZgroupsr/ZgrouplowerZstringZendr)) rrqrrrkeachZ auth_matchZmruZparamsrrrr7sB         z SMTP.ehlocCs||jkSr)r{r<)rZoptrrrhas_extnz SMTP.has_extncCs|d||dS)NhelprKrnrPrrrrs z SMTP.helpcCsd|_|dS)Nr8rset)r=rGrErrrrsz SMTP.rsetcCs&z |Wntk r YnXdSr)rrrErrr_rsets z SMTP._rsetcCs |dS)Nnoop)rGrErrrrr~z SMTP.nooprcCshd}|rH|jrHtdd|Dr:|dr2d|_ntddd|}|dd t||f|S) Nr%css|]}|dkVqdS)smtputf8N)r{).0ZxrrrZ szSMTP.mail..rzutf-8z SMTPUTF8 not supported by serverrtmailz FROM:%s%s) rxZanyr}r=rrjrdr r\)rr"options optionlistrrrrs   z SMTP.mailcCs<d}|r|jrdd|}|ddt||f|S)Nr%rtrcptzTO:%s%s)rxrjrdr r\)rZreciprrrrrrs  z SMTP.rcptcCs|d|\}}|jdkr0|d||f|dkrDt||n|t|tr\t|d}t |}|ddt kr||t }|dt }| ||\}}|jdkr|d||f||fSdS)Nr3rzdata:ibr8is.) rdr\rIrQrr_r`r6rar4bCRLFrb)rrrZreplZqrrrr3%s"         z SMTP.datacC|dt||S)Nvrfyrdr,r\rZaddressrrrverifyBz SMTP.verifycCr)NexpnrrrrrrIrz SMTP.expncCsb|jdkr^|jdkr^d|dkr0dks^n|\}}d|krRdks^nt||dS)Niri+)rprwr7ror )rrrkrrrehlo_or_helo_if_neededPs  zSMTP.ehlo_or_helo_if_neededTinitial_response_okc Cs|}|r|nd}|dk rJt|ddd}|d|d|\}}n|d|\}}|dkrt|}t||ddd}||\}}|dkr||fSt||dS)Nr8r%)ZeolZAUTHrtiNii)Zupper encode_base64rarGbase64Z decodebytesr ) rZ mechanismZ authobjectrZinitial_responseZresponserrk challengerrrrsas   z SMTP.authcCs0|dkr dS|jdt|jd|dS)Nrtr8Zmd5)userhmacZHMACpasswordraZ hexdigestrrrrr auth_cram_md5s  zSMTP.auth_cram_md5cCsd|j|jfS)Nz%s%srrrrrr auth_plainszSMTP.auth_plaincCs|dkr|jS|jSdSrrrrrr auth_loginszSMTP.auth_loginc s||dstd|jddddg}fdd|D}|sPtd|||_|_|D]t}d | d d }z4|j |t |||d \}} |d kr|| fWSWqbt k r} z| } W5d} ~ XYqbXqb| dS)Nrsz,SMTP AUTH extension not supported by server.zCRAM-MD5ZPLAINZLOGINcsg|]}|kr|qSrr)rrsZadvertised_authlistrr szSMTP.login..z(No suitable authentication method found.Zauth_z-Z_rr) rr}rr<ryrrrr{ZreplacersZgetattrr ) rrrrZpreferred_authsZauthlistZ authmethodZ method_namerrkrmZlast_exceptionrrrlogins0   z SMTP.logincCs||dstd|d\}}|dkrts.ToZBccZCccSsg|] }|dqS)rKr)rZarrrrsz Resent-BccFr8rzOne or more source or delivery addresses require internationalized email support, but the server does not advertise the required SMTPUTF8 capabilityT)Zutf8)policySMTPUTF8 BODY=8BITMIMEr)Zlinesep)rr)rZget_allrhrWr&r'Z getaddressescopyrjraZUnicodeEncodeErrorr}rioZBytesIOZ generatorZBytesGeneratorrZcloneZflattenZgetvaluer)rrrrrrZresentZ header_prefixZ addr_fieldsZmsg_copyZ internationalZbytesmsgZgZflatmsgrrr send_messagesX              zSMTP.send_messagecCs<z|j}d|_|r|W5|j}d|_|r6|XdSr)r[r@rL)rr[rLrrrr@s z SMTP.closecCs.|d}d|_|_i|_d|_||S)NquitF)rGrwrpr<rxr@)rZresrrrrs   z SMTP.quitrTrNr%rrrrrrrrr)NNN)rr)NNrr)1rrrrIr[rLrprvrwrx SMTP_PORTrYrB_GLOBAL_DEFAULT_TIMEOUTr rFrHrJrQrSr?rbrdr\rGror7r}rrrrrrr3rrrrrsrrrrrrrr@rrrrrrsf /   # 1  3    '  B 8 h M c@s4eZdZeZdddddejddfddZddZdS)SMTP_SSLr%rNc Cs|dk r|dk rtd|dk r0|dk r0td|dk s@|dk rVddl} | dtd||_||_|dkrxtj||d}||_t ||||||dS)Nrrrrrr) rWrrrrrrrrrr ) rrCrDrArrr;r>rrrrrr s(zSMTP_SSL.__init__cCsD|jdkr|d||ft||f||j}|jj||jd}|S)NrrUr)rIrQrBrRr>rrr:)rrCrDr;Z new_socketrrrrS s  zSMTP_SSL._get_socket) rrr SMTP_SSL_PORTrYrBrr rSrrrrrs ric@s,eZdZdZdeddfddZd dd ZdS) LMTPZlhlor%NcCstj|||||ddS)N)rAr>)rr )rrCrDrAr>rrrr -s z LMTP.__init__rTrcCs|ddkrtj||||dSz(ttjtj|_d|_|j|WnBtk r|jdkrl| d||jr||j d|_YnX| \}}|jdkr| d|||fS)Nrz/)r>z connect fail:rU) rr?rBZAF_UNIXZ SOCK_STREAMr[rLrXrIrQr@r\)rrCrDr>rrrrrr?3s"       z LMTP.connectr)rrrrv LMTP_PORTr r?rrrrrs  rZ__main__cCs(tj|dtjtjS)Nz: )rOZstdoutZwriteZflushstdinrfr))promptrrrrMs rrrz,zEnter message, end with ^D:r%zMessage length is %drTrK)?rBrr/Z email.utilsr&Z email.messageZemail.generatorrrrrNrOZemail.base64mimerrZ__all__rrr1rrgZcompileZIrzrXrrrrrrrr r r r r,r r4r6rZ ImportErrorrrrrirrrrZfromaddrryZtoaddrsrMrrrfrlrhZserverrJrrrrrrZ,s       -0 /