python tles / ssl email (tles 지원 여 부 를 자동 으로 판단 합 니 다)

#from: http://blog.chinaunix.net/u/19742/showart_422492.html
  # - * - coding: cp936 - * -
#! / usr/ bin/ env python
import sys, smtplib
"" "
smtp.sina.com
1. TLS
2. ehlo, , TLS
3. starttls
4. ehlo
5.
"
""
server = "smtp.sina.com"
fromaddr= "[email protected]"
toaddr = "[email protected]"

msg = "" "
to:%s
from:%s
Hello,I am smtp server
"
"" % ( toaddr, fromaddr)

s = smtplib. SMTP( server)
# ,
s. login( "jcodeer" , " " )
"" "
ehlo :
code: 。
string: 。
"
""
code = s. ehlo( ) [ 0]
usesesmtp = 1
if not ( 200 < = code < = 299) :
    usesesmtp = 0
    "" "
    code string ehlo 。
    "
""
    code = s. helo( ) [ 0]
    if not ( 200 < = code < = 299) :
        raise SMTPHeloError( code, resp)
if usesesmtp and s. has_extn( "tls" ) :
    "" " tls, starttls(3)" ""
    print ( "starttls" )
    s. starttls( )
    "" " ehlo(4)" ""
    code = s. ehlo( ) [ 0]
    if not ( 200 < = code < = 299) :
        print ( "Could not EHLO after starttls" )
        sys. exit( 5)
    print ( "using tls connection" )
else :
    "" " tls, (2)" ""
    print ( "server does not support tls,using normal connection" )
"" " (5)" ""
s. sendmail( fromaddr, toaddr, msg)

좋은 웹페이지 즐겨찾기