3 \ @sjddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Zddddddd d d d d dg ZdZdZdZdZdZejdejZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZ Gdd d eZ!Gdd d eZ"d d Z#d!d"Z$d#d Z%d$d%Z&d&d'Z'y ddl(Z(Wne)k rd(Z*YnXd)Z*Gd*ddZ+e*rGd+d,d,e+Z,ej-d,d-Z.Gd.d/d/e+Z/e0d0krfd1d2Z1e1d3Z2e1d4j3d5Z4e5d6d7Z6x e j7j8Z9e9s Pe6e9Z6qWe5d8e:e6e+d9Z;e;jdS);N) body_encode SMTPExceptionSMTPServerDisconnectedSMTPResponseExceptionSMTPSenderRefusedSMTPRecipientsRefused SMTPDataErrorSMTPConnectError SMTPHeloErrorSMTPAuthenticationError quoteaddr quotedataSMTPiz s i z auth=(.*)c@s eZdZdS)rN)__name__ __module__ __qualname__rr/usr/lib64/python3.6/smtplib.pyrGsc@s eZdZdS)SMTPNotSupportedErrorN)rrrrrrrrJsrc@s eZdZdS)rN)rrrrrrrrQsc@seZdZddZdS)rcCs||_||_||f|_dS)N) smtp_code smtp_errorargs)selfcodemsgrrr__init__bszSMTPResponseException.__init__N)rrrrrrrrrYs c@seZdZddZdS)rcCs"||_||_||_|||f|_dS)N)rrsenderr)rrrrrrrrnszSMTPSenderRefused.__init__N)rrrrrrrrrgsc@seZdZddZdS)rcCs||_|f|_dS)N) recipientsr)rrrrrr|szSMTPRecipientsRefused.__init__N)rrrrrrrrrtsc@s eZdZdS)rN)rrrrrrrrsc@s eZdZdS)r N)rrrrrrrr sc@s eZdZdS)r N)rrrrrrrr sc@s eZdZdS)r N)rrrrrrrr scCs>tjj|\}}||fdkr6|jjdr.|Sd|Sd|S)N)rr)emailutils parseaddrstrip startswith) addrstring displaynameaddrrrrr s  cCs$tjj|\}}||fdkr |S|S)Nr)rr)r!r"r#)r&r'r(rrr _addr_onlys r)cCstjddtjdt|S)Nz(?m)^\.z..z(?:\r\n|\n|\r(?!\n)))resubCRLF)datarrrr scCstjdd|S)Ns(?m)^\.s..)r*r+)Zbindatarrr_quote_periodssr.cCstjdt|S)Nz(?:\r\n|\n|\r(?!\n)))r*r+r,)r-rrr _fix_eolssr/FTc@seZdZdZdZdZdZdZdZe Z ddde j dfddZ ddZd d Zd d Zd dZddZdLddZddZdMddZddZdNddZdOddZdPddZd d!ZdQd"d#Zd$d%Zd&d'Zd(d)Zffd*d+Zffd,d-Z d.d/Z!d0d1Z"e"Z#d2d3Z$d4d5Z%d6d7d8d9Z&dRd:d;Z'dSdd?Z)d6d7d@dAZ*dUdBdCZ+fffdDdEZ,ddfffdFdGZ-dHdIZ.dJdKZ/dS)VrrNehlorc Cs||_||_i|_d|_||_|rL|j||\}}|dkrL|jt|||dk r\||_nPt j }d|krt||_n8d} yt j t j } Wnt j k rYnXd| |_dS)Nascii.z 127.0.0.1z[%s])_hosttimeoutesmtp_featurescommand_encodingsource_addressconnectcloser local_hostnamesocketZgetfqdnZ gethostbynameZ gethostnameZgaierror) rhostportr;r5r8rrZfqdnr(rrrrs* z SMTP.__init__cCs|S)Nr)rrrr __enter__szSMTP.__enter__cGsNz>y$|jd\}}|dkr$t||Wntk r:YnXWd|jXdS)NZQUIT)docmdrrr:)rrrmessagerrr__exit__s z SMTP.__exit__cCs ||_dS)N) debuglevel)rrDrrrset_debuglevelszSMTP.set_debuglevelcGs@|jdkr,ttjjjf|dtjint|dtjidS)Nfile)rDprintdatetimeZnowZtimesysstderr)rrrrr _print_debug's "zSMTP._print_debugcCs2|jdkr|jd||f|jtj||f||jS)Nrz connect: to)rDrLr8r<create_connection)rr=r>r5rrr _get_socket-s  zSMTP._get_socket localhostc Cs|r ||_| r~|jd|jdkr~|jd}|dkr~|d|||dd}}y t|}Wntk r|tdYnX|s|j}|jdkr|jd||f|j |||j |_ d|_ |j \}}|jdkr|jdt|||fS)N:rrFznonnumeric portzconnect:)r8findrfindint ValueErrorOSError default_portrDrLrNr5sockrGgetreplyrepr)rr=r>r8irrrrrr95s(      z SMTP.connectc Cs|jdkr|jdt|t|drx|jrxt|tr@|j|j}y|jj |Wqt k rt|j t dYqXnt ddS)Nrzsend:rWzServer not connectedzplease run connect() first) rDrLrYhasattrrW isinstancestrencoder7ZsendallrUr:r)rsrrrsendWs   z SMTP.sendcCs2|dkrd|tf}nd||tf}|j|dS)Nrz%s%sz%s %s%s)r,r`)rcmdrr]rrrputcmdisz SMTP.putcmdcCsPg}|jdkr|jjd|_xy|jjtd}Wn:tk rn}z|jtdt|WYdd}~XnX|s|jtd|j dkr|j dt |t |tkr|jt dd|j|d djd |dd }y t|}Wntk rd}PYnX|d d d kr Pq Wd j|}|j dkrH|j d||f||fS)NrbrFz Connection unexpectedly closed: zConnection unexpectedly closedrzreply:izLine too long.s - zreply: retcode (%s); Msg: %a)rGrWmakefilereadline_MAXLINErUr:rr]rDrLrYlenrappendr$rSrTjoin)rresplineerZerrcodeerrmsgrrrrXqs>         z SMTP.getreplycCs|j|||jS)N)rbrX)rrarrrrrAs z SMTP.docmdcCs,|jd|p|j|j\}}||_||fS)Nhelo)rbr;rX helo_resp)rnamerrrrrrss z SMTP.heloc Cs2i|_|j|j|p|j|j\}}|d krJt|dkrJ|jtd||_|dkr`||fSd|_ |jj dj d}|d=x|D]}t j |}|r|jjddd |jdd|jd<qtj d |}|r|jd j}|j|jd dj} |dkr|jj|dd | |j|<q| |j|<qW||fS) NrFrzServer not connectedzlatin-1 authr z((?P[A-Za-z0-9][A-Za-z0-9\-]*) ?featurerh)r6rbehlo_msgr;rXrlr:r ehlo_resp does_esmtpdecodesplit OLDSTYLE_AUTHmatchgetgroupsr*grouplowerstringendr$) rrurrroeachZ auth_matchmrzZparamsrrrr0s2   &  z SMTP.ehlocCs|j|jkS)N)rr6)roptrrrhas_extnsz SMTP.has_extncCs|jd||jdS)NhelprF)rbrX)rrrrrrs z SMTP.helpcCsd|_|jdS)Nr1rset)r7rA)rrrrrsz SMTP.rsetc Cs&y |jWntk r YnXdS)N)rr)rrrr_rsets z SMTP._rsetcCs |jdS)Nnoop)rA)rrrrrsz SMTP.noopcCshd}|rH|jrHtdd|Dr:|jdr2d|_ntdddj|}|jdd t||f|jS) Nrcss|]}|jdkVqdS)smtputf8N)r).0xrrr szSMTP.mail..rzutf-8z SMTPUTF8 not supported by serverrymailz FROM:%s%s) r}anyrr7rrnrbr rX)rroptions optionlistrrrrs   z SMTP.mailcCs<d}|r|jrddj|}|jddt||f|jS)NrryrcptzTO:%s%s)r}rnrbr rX)rZreciprrrrrrs  z SMTP.rcptcCs|jd|j\}}|jdkr0|jd||f|dkrDt||n|t|tr\t|jd}t |}|ddt kr||t }|dt }|j ||j\}}|jdkr|jd||f||fSdS) Nr-rzdata:ibr1.) rbrXrDrLrr\r]r/r^r.bCRLFr`)rrrreplqrrrr-!s"         z SMTP.datacCs|jdt||jS)Nvrfy)rbr)rX)raddressrrrverify>sz SMTP.verifycCs|jdt||jS)Nexpn)rbr)rX)rrrrrrEsz SMTP.expncCsf|jdkrb|jdkrbd|jdko.dknsb|j\}}d|koRdknsbt||dS)Nri+)rtr|r0rsr )rrrorrrehlo_or_helo_if_neededLs   zSMTP.ehlo_or_helo_if_neededT)initial_response_okc Cs|j}|r|nd}|dk rJt|jddd}|jd|d|\}}n|jd|\}}|dkrtj|}t||jddd}|j|\}}|d kr||fSt||dS) Nr1r)ZeolZAUTHryiN)rr)upper encode_base64r^rAbase64Z decodebytesr ) rZ mechanismZ authobjectrZinitial_responseZresponserro challengerrrrx]s z SMTP.authcCs0|dkr dS|jdtj|jjd|djS)Nryr1Zmd5)userhmacZHMACpasswordr^Z hexdigest)rrrrr auth_cram_md5s zSMTP.auth_cram_md5cCsd|j|jfS)Nz%s%s)rr)rrrrr auth_plainszSMTP.auth_plaincCs|dkr|jS|jSdS)N)rr)rrrrr auth_loginszSMTP.auth_loginc s|j|jdstd|jdjdddg}fdd|D}|sPtd|||_|_xv|D]n}d |jj d d }y.|j |t |||d \}} |dkr|| fSWqdt k r} z | } WYdd} ~ XqdXqdW| dS)Nrxz,SMTP AUTH extension not supported by server.zCRAM-MD5ZPLAINZLOGINcsg|]}|kr|qSrr)rrx)advertised_authlistrr szSMTP.login..z(No suitable authentication method found.Zauth_-_)rr)rr) rrrr6rrrrrreplacerxgetattrr ) rrrrZpreferred_authsZauthlistZ authmethodZ method_namerrorqZlast_exceptionr)rrlogins*      z SMTP.logincCs|j|jdstd|jd\}}|dkrts.ToZBccZCccSsg|] }|dqS)rFr)rarrrrsz Resent-BccFr1rzOne or more source or delivery addresses require internationalized email support, but the server does not advertise the required SMTPUTF8 capabilityT)utf8)policySMTPUTF8 BODY=8BITMIMEz )linesep)rr)rZget_allrlrTr!r"Z getaddressescopyrnr^UnicodeEncodeErrorrrioBytesIO generatorZBytesGeneratorrZcloneZflattengetvaluer)rrrrrrZresentZ header_prefixZ addr_fieldsZmsg_copyZ internationalZbytesmsggZflatmsgrrr send_message|sJ           zSMTP.send_messagec Cs<z|j}d|_|r|jWd|j}d|_|r6|jXdS)N)rGr:rW)rrGrWrrrr:s z SMTP.closecCs.|jd}d|_|_i|_d|_|j|S)NquitF)rAr|rtr6r}r:)rresrrrrs   z SMTP.quit)rOrN)r)r)r)r)r)N)N)N)NNN)0rrrrDrGrtr{r|r} SMTP_PORTrVr<_GLOBAL_DEFAULT_TIMEOUTrr?rCrErLrNr9r`rbrXrArsr0rrrrrrrr-rrrrrxrrrrrrrr:rrrrrrs\ -   " 1  3    '  B 8 gL c@s4eZdZeZdddddejddfddZddZdS)SMTP_SSLrrNc Cs|dk r|dk rtd|dk r0|dk r0td|dk s@|dk rVddl} | jdtd||_||_|dkrxtj||d}||_t j ||||||dS)Nz4context and keyfile arguments are mutually exclusivez5context and certfile arguments are mutually exclusiverzAkeyfile and certfile are deprecated, use a custom context insteadr)rr) rTrrrrrrrrrr) rr=r>r;rrr5r8rrrrrrs zSMTP_SSL.__init__cCsD|jdkr|jd||ftj||f||j}|jj||jd}|S)Nrzconnect:)r)rDrLr<rMr8rrr4)rr=r>r5Z new_socketrrrrN s   zSMTP_SSL._get_socket) rrr SMTP_SSL_PORTrVr<rrrNrrrrrs ric@s,eZdZdZdeddfddZd dd ZdS) LMTPZlhlorNcCstj|||||ddS)N)r;r8)rr)rr=r>r;r8rrrr)s z LMTP.__init__rOrc Cs|ddkrtj||||dSy(tjtjtj|_d|_|jj|WnBtk r|jdkrl|j d||jr||jj d|_YnX|j \}}|jdkr|j d|||fS)Nr/)r8z connect fail:zconnect:) rr9r<ZAF_UNIXZ SOCK_STREAMrWrGrUrDrLr:rX)rr=r>r8rrrrrr9/s"       z LMTP.connect)rOrN)rrrr{ LMTP_PORTrr9rrrrrs r__main__cCs(tjj|dtjjtjjjS)Nz: )rJstdoutwriteflushstdinrjr$)promptrrrrIs rrr,zEnter message, end with ^D:rzMessage length is %drOrF)?r<rr*Z email.utilsr!Z email.messageZemail.generatorrrrrIrJZemail.base64mimerr__all__rrr,rrkcompileIrrUrrrrrrrr r r r r)r r.r/r ImportErrorrrrrmrrrrZfromaddrrZtoaddrsrHrrrjrprlZserverrErrrrrr,s        )0 /