U AT^& @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 quotedataSMTPi s i z auth=(.*)c@s eZdZdS)rN__name__ __module__ __qualname__rr/usr/lib64/python3.8/smtplib.pyrGsc@s eZdZdS)rNrrrrrrJsc@s eZdZdS)rNrrrrrrQsc@seZdZddZdS)rcCs||_||_||f|_dSN) smtp_code smtp_errorargs)selfcodemsgrrr__init__bszSMTPResponseException.__init__NrrrrrrrrrYs c@seZdZddZdS)rcCs"||_||_||_|||f|_dSr)rrsenderr)rrrr!rrrrnszSMTPSenderRefused.__init__Nr rrrrrgsc@seZdZddZdS)rcCs||_|f|_dSr) recipientsr)rr"rrrr|szSMTPRecipientsRefused.__init__Nr rrrrrtsc@s eZdZdS)r Nrrrrrr sc@s eZdZdS)r Nrrrrrr sc@s eZdZdS)r Nrrrrrr sc@s eZdZdS)r Nrrrrrr scCs>tj|\}}||fdkr6|dr.|Sd|Sd|S)Nr$)emailutils parseaddrstrip 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))resubCRLFdatarrrrs cCstdd|S)Ns(?m)^\.s..)r0r1)Zbindatarrr_quote_periodssr5cCstdt|S)Nr.r/r3rrr _fix_eolssr6FTc@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)Nascii.z 127.0.0.1z[%s])_hosttimeoutesmtp_featurescommand_encodingsource_addressconnectcloser local_hostnamesocketgetfqdn gethostbyname gethostnamegaierror) rhostportrBr<r?rrZfqdnr,rrrrs* z SMTP.__init__cCs|Srrrrrr __enter__szSMTP.__enter__cGsNz>z$|d\}}|dkr$t||Wntk r:YnXW5|XdS)NZQUIT)rAdocmdrr)rrrmessagerrr__exit__s z SMTP.__exit__cCs ||_dSr) debuglevel)rrPrrrset_debuglevel szSMTP.set_debuglevelcGs@|jdkr,ttjf|dtjint|dtjidS)Nfile)rPprintdatetimeZnowtimesysstderrrrrrr _print_debug)s "zSMTP._print_debugcCs2|jdkr|d||f|jt||f||jS)Nrz connect: to)rPrZr?rCcreate_connection)rrHrIr<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)N:rrRznonnumeric portconnect:zsmtplib.connect)r?findrfindint ValueErrorOSError default_portrPrZrWauditr\r<sockrSgetreplyrepr)rrHrIr?irrrrrr@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)rPrZrirg isinstancestrencoder>rWrfsendallrdrAr)rsrrrsendZs   z SMTP.sendcCs2|dkrd|tf}nd||tf}||dS)Nr$z%s%sz%s %s%s)r2rq)rcmdrrmrrrputcmdmsz 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)NrbrRz Connection unexpectedly closed: zConnection unexpectedly closedrzreply:izLine too long.s - zreply: retcode (%s); Msg: %a)rSrgmakefilereadline_MAXLINErdrArrmrPrZrilenrappendr)rbrcjoin)rresplineerZerrcodeerrmsgrrrrhus>          z SMTP.getreplycCs||||Srrsrh)rrrrrrrrMs z SMTP.docmdcCs,|d|p|j|\}}||_||fS)Nhelo)rsrBrh helo_resp)rnamerrrrrrs 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) NrwrrkrRzlatin-1 authr$ z((?P[A-Za-z0-9][A-Za-z0-9\-]*) ?feature)r=rsehlo_msgrBrhr}rAr ehlo_resp does_esmtpdecodesplit OLDSTYLE_AUTHmatchgetgroupsr0grouplowerstringendr)) rrrrreachZ auth_matchmrparamsrrrr7sB         z SMTP.ehlocCs||jkSr)rr=)roptrrrhas_extnsz SMTP.has_extncCs|d||dS)NhelprRrrYrrrrs z SMTP.helpcCsd|_|dS)Nr8rset)r>rMrJrrrrsz SMTP.rsetcCs&z |Wntk r YnXdSr)rrrJrrr_rsets z SMTP._rsetcCs |dS)Nnoop)rMrJrrrrsz SMTP.nooprcCshd}|rH|jrHtdd|Dr:|dr2d|_ntddd|}|dd t||f|S) Nr$css|]}|dkVqdS)smtputf8N)r).0xrrr szSMTP.mail..rzutf-8z SMTPUTF8 not supported by serverrmailz FROM:%s%s) ranyrr>rrrsr rh)rr!options optionlistrrrrs   z SMTP.mailcCs<d}|r|jrdd|}|ddt||f|S)Nr$rrcptzTO:%s%s)rrrsr rh)rZreciprrrrrrs  z SMTP.rcptcCs|d|\}}|jdkr0|d||f|dkrDt||n|t|tr\t|d}t |}|ddt kr||t }|dt }| ||\}}|jdkr|d||f||fSdS)Nr4rzdata:ibr8.) rsrhrPrZr rlrmr6rnr5bCRLFrq)rrrreplqrrrr4%s"         z SMTP.datacCs|dt||S)Nvrfyrsr-rhraddressrrrverifyBsz SMTP.verifycCs|dt||S)NexpnrrrrrrIsz SMTP.expncCsb|jdkr^|jdkr^d|dkr0dks^n|\}}d|krRdks^nt||dS)Nri+)rrr7rr )rrrrrrehlo_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$)ZeolZAUTHriNi)upper encode_base64rnrMbase64Z decodebytesr ) rZ mechanismZ authobjectrZinitial_responseresponserr challengerrrras   z SMTP.authcCs0|dkr dS|jdt|jd|dS)Nrr8md5)userhmacZHMACpasswordrn 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)Nrz,SMTP AUTH extension not supported by server.zCRAM-MD5ZPLAINZLOGINcsg|]}|kr|qSrr)rrZadvertised_authlistrr szSMTP.login..z(No suitable authentication method found.Zauth_-_rr) rrrr=rrrrrreplacergetattrr ) rrrrZpreferred_authsZauthlistZ authmethodZ method_namerrrZlast_exceptionrrrlogins0   z SMTP.logincCs||dstd|d\}}|dkrts.ToZBccZCccSsg|] }|dqS)rRr)rarrrrsz Resent-BccFr8rzOne or more source or delivery addresses require internationalized email support, but the server does not advertise the required SMTPUTF8 capabilityT)utf8)policySMTPUTF8 BODY=8BITMIMEr)linesep)rr)rZget_allr}rcr&r'Z getaddressescopyrrnUnicodeEncodeErrorrrioBytesIO generatorZBytesGeneratorrZcloneZflattengetvaluer)rrrrrrZresentZ header_prefixZ addr_fieldsZmsg_copy internationalZbytesmsggZflatmsgrrr send_messagesX              zSMTP.send_messagecCs<z|j}d|_|r|W5|j}d|_|r6|XdSr)rgrArS)rrgrSrrrrAs z SMTP.closecCs.|d}d|_|_i|_d|_||S)NquitF)rMrrr=rrA)rresrrrrs   z SMTP.quit)r]rN)r$)r$)r$)r$)r$)r)r)N)N)N)NNN)rr)NNrr)1rrrrPrgrSrrrr SMTP_PORTrerC_GLOBAL_DEFAULT_TIMEOUTrrKrOrQrZr\r@rqrsrhrMrr7rrrrrrrr4rrrrrrrrrrrrrArrrrrrsf /   # 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) rcrrrrrrrrrr) rrHrIrBrrr<r?rrrrrrs(zSMTP_SSL.__init__cCsD|jdkr|d||ft||f||j}|jj||jd}|S)Nrr_r)rPrZrCr[r?rrr;)rrHrIr<Z new_socketrrrr\ s  zSMTP_SSL._get_socket) rrr SMTP_SSL_PORTrerCrrr\rrrrrs ric@s,eZdZdZdeddfddZd dd ZdS) LMTPZlhlor$NcCstj|||||ddS)N)rBr?)rr)rrHrIrBr?rrrr-s z LMTP.__init__r]rcCs|ddkrtj||||dSz(ttjtj|_d|_|j|WnBtk r|jdkrl| d||jr||j d|_YnX| \}}|jdkr| d|||fS)Nr/)r?z connect fail:r_) rr@rCAF_UNIX SOCK_STREAMrgrSrdrPrZrArh)rrHrIr?rrrrrr@3s"       z LMTP.connect)r]rN)rrrr LMTP_PORTrr@rrrrrs  r__main__cCs(tj|dtjtjS)Nz: )rWstdoutwriteflushstdinr{r))promptrrrrMs rrr,zEnter message, end with ^D:r$zMessage length is %dr]rR)?rCrr0Z email.utilsr&Z email.messageZemail.generatorrrrrUrWZemail.base64mimerr__all__rrr2rr|compileIrrdrrrrrrr r r r r r-rr5r6r ImportErrorrrrr~r rrrZfromaddrrZtoaddrsrTrrr{rr}ZserverrQrrrrrr,s       -0 /