[Twisted-Python] about smtp client auth problem.

Chris chris.yan at saybot.com
Tue Apr 15 04:38:28 MDT 2008


Hi,
I am trying to use twisted to implement a script to send the email,I got
some problem.here is the script:
####################
import StringIO

from twisted.application import service

application = service.Application("SMTP Client Tutorial")

from twisted.application import internet
from twisted.python import log
from twisted.internet import defer, reactor
from twisted.mail import smtp

mailFrom = "chris.yan at xxxx.com"
mailTo = ["chrisyan81 at gmail.com",]
mail = StringIO.StringIO("""Date: Fri, 3 Nov 2006 230:14:39 +0000
From: chris.yan at xxxx.com
To: chrisyan81 at gmail.com
Subject: Tutorate!

Hello, how are you, goodbye.
""")

whenDone = defer.Deferred()

def send(r):
print "message sent:", r

whenDone.addCallbacks(send, log.err)

def done(d):
from twisted.internet import reactor
reactor.callLater(1, reactor.stop)

whenDone.addBoth(done)


sender = smtp.ESMTPSenderFactory(
'chris.yan',
'xxxxxx',
mailFrom,
mailTo,
mail,
whenDone,
requireTransportSecurity=False
)

smtpClientService = internet.TCPClient('smtp.xxxx.com', 25, sender)
smtpClientService.setServiceParent(application)

in fact the script from
http://twistedmatrix.com/pipermail/twisted-python/2006-November/014299.html,and
I didn't modify anything except my own information.Because the smtp
server doesn't support SSL,so I use requireTransportSecurity=False.

here is the output:

2008/04/15 17:56 +0800 [-] Log opened.
2008/04/15 17:56 +0800 [-] twistd 2.5.0 (C:\Python25\python.exe 2.5.2)
starting up
2008/04/15 17:56 +0800 [-] reactor class: <class
'twisted.internet.selectreactor.SelectReactor'>
2008/04/15 17:56 +0800 [-] Loading smtpclient_tls_001.py...
2008/04/15 17:56 +0800 [-] Loaded.
2008/04/15 17:56 +0800 [-] Starting factory
<twisted.mail.smtp.ESMTPSenderFactory instance at 0x01347878>
2008/04/15 17:56 +0800 [ESMTPSender,client] Unhandled Error
Traceback (most recent call last):
Failure: twisted.mail.smtp.AUTHRequiredError: 502 Server does not
support Client Authentication schemes [CRAM-MD
5, LOGIN, PLAIN]

<<< 250-AUTH
<<< 250-AUTH=PLAIN LOGIN LOGIN
<<< 250-XXXXXXXA
<<< 250 XXXB

2008/04/15 17:56 +0800 [ESMTPSender,client] Stopping factory
<twisted.mail.smtp.ESMTPSenderFactory instance at 0x0134787
8>
2008/04/15 17:56 +0800 [-] Main loop terminated.
2008/04/15 17:56 +0800 [-] Server Shut Down.

but when I use another script which uses smtplib:
SMTP_server='smtp.xxxx.com'
SMTP_user='chris.yan'
SMTP_passwd='xxxxx'

def
send_email(Subject,From,To,Attachment,Body,SMTP_server,SMTP_user,SMTP_passwd):
msg = MIMEMultipart()
msg['Subject'] = Subject
msg['From'] = From
msg['To'] = To


txt = MIMEText(Body,_charset='utf-8')
msg.attach(txt)

filename = Attachment
if not os.path.isfile(filename):
print 'Can not find file %s,please check!' % filename
sys.exit(1)

fp = open(filename,'rb')
ctype,encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype,subtype = ctype.split('/',1)
m = MIMEBase(maintype,subtype)
m.set_payload(fp.read())
fp.close()
Encoders.encode_base64(m)
m.add_header('Content-disposition','attachment',filename=filename)
msg.attach(m)

s = smtplib.SMTP(SMTP_server)
s.debuglevel = 5
s.ehlo()
s.esmtp_features["auth"] = "LOGIN"

s.login(SMTP_user,SMTP_passwd)
s.sendmail(From,To,msg.as_string())
s.close()

I can get the output:
send: 'ehlo chris.shuobaotang.com\r\n'
reply: '250-mail.saybot.com Hello chris.shuobaotang.com [218.79.132.2]\r\n'
reply: '250-SIZE 52428800\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-AUTH \r\n'
reply: '250-AUTH=PLAIN LOGIN LOGIN\r\n'
reply: '250-XXXXXXXA\r\n'
reply: '250 XXXB\r\n'
reply: retcode (250); Msg: mail.saybot.com Hello chris.shuobaotang.com
[218.79.132.2]
SIZE 52428800
PIPELINING
AUTH
AUTH=PLAIN LOGIN LOGIN
XXXXXXXA
XXXB
send: 'AUTH LOGIN Y2hyaXMueWFu\r\n'
reply: '334 UGFzc3dvcmQ6\r\n'
reply: retcode (334); Msg: UGFzc3dvcmQ6
send: 'd3JpbHV5Yg==\r\n'
reply: '235 Authentication succeeded\r\n'
reply: retcode (235); Msg: Authentication succeeded
send: 'mail FROM:<chris.yan at saybot.com> size=668\r\n'
reply: '250 OK\r\n'
reply: retcode (250); Msg: OK
send: 'rcpt TO:<chrisyan81 at gmail.com>\r\n'
reply: '250 Accepted\r\n'
reply: retcode (250); Msg: Accepted
send: 'data\r\n'
reply: '354 Enter message, ending with "." on a line by itself\r\n'
reply: retcode (354); Msg: Enter message, ending with "." on a line by
itself
data: (354, 'Enter message, ending with "." on a line by itself')
send: 'Content-Type: multipart/mixed;
boundary="===============1595669237=="\r\nMIME-Version: 1.0\r\nSubject:
scores fro
m saybot rater.\r\nFrom: chris.yan at saybot.com\r\nTo:
chrisyan81 at gmail.com\r\n\r\n--===============1595669237==\r\nConten
t-Type: text/plain; charset="utf-8"\r\nMIME-Version:
1.0\r\nContent-Transfer-Encoding: base64\r\n\r\nSGksCiAgICBUaGlzIGl
zIHRoZSBzY29yZXMgd2hpY2ggZ2VuZXJhdGVkIGJ5IHNheWJvdCByYXRl\r\ncixwbGVhc2UgY2hlY2sgdGhlIGF0dGFjaG1lbnQhCgpCZXN0UmVnYXJkCkN
ocmlzCg==\r\n\r\n--===============1595669237==\r\nContent-Type:
text/plain\r\nMIME-Version: 1.0\r\nContent-Transfer-Enco
ding: base64\r\nContent-disposition: attachment;
filename="scores.txt"\r\n\r\nMTM4MTMwMzE0NzUg17/Hvw0K\r\n\r\n--========
=======1595669237==--\r\n.\r\n'
reply: '250 OK id=1Jlhvl-00071w-1f\r\n'
reply: retcode (250); Msg: OK id=1Jlhvl-00071w-1f
data: (250, 'OK id=1Jlhvl-00071w-1f')
Done.

some guy said,this script force to use the LOGIN auth,because something
wrong with the server,is it true?I don't know very much about the mail
server,does any one can tell me if I can make my twisted script work?I
really like to implement a twisted one.

BestWishes
Chris







More information about the Twisted-Python mailing list