[Twisted-Python] Email problem

Alvin Wang alvinwang at gmail.com
Tue Jul 13 05:32:17 EDT 2010


Problem send email through gmail with an attachment

Any help is appreciated

Thanks
Alvin

""" Does not work

"""
from twisted.internet.ssl import ClientContextFactory
from twisted.internet.defer import Deferred
from twisted.mail.smtp import ESMTPSenderFactory
from twisted.internet import reactor
import smtplib, sys, MimeWriter, mimetypes, mimetools, base64

import StringIO
import time
import os

sender = 'acct at gmail.com'
senderpw = 'password'
to = 'alvinwang at gmail.org'
subject = 'gc' + str(time.time())
text = 'Message Text Body'

attachments = ["/home/alvin/Pictures/alvin.jpg",
"/home/alvin/Pictures/badlogo.gif"]

contextFactory = ClientContextFactory()
result = Deferred()

message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader('To', to)
writer.addheader('From', sender)
writer.addheader('Subject', subject)
writer.addheader('MIME-Version', '1.0')
writer.startmultipartbody('mixed')

part = writer.nextpart()
body = part.startbody('text/plain')
part.flushheaders()
body.write(text)

# now add the attachments
if attachments is not None:
    for a in attachments:
        filename = os.path.basename(a)
        ctype, encoding = mimetypes.guess_type(a)
        if ctype is None:
            ctype = 'application/octet-stream'
            encoding = 'base64'
        elif ctype == 'text/plain':
            encoding = 'quoted-printable'
        else:
            encoding = 'base64'

        part = writer.nextpart()
        part.addheader('Content-Transfer-Encoding', encoding)
        body = part.startbody("%s; name=%s" % (ctype, filename))
        mimetools.encode(open(a, 'rb'), body, encoding)

# that's all falks
writer.lastpart()
m = message.getvalue()
message.close()


factory = ESMTPSenderFactory(sender,
                           senderpw,
                           sender,
                           to,
                           m,
                           result,
                           contextFactory=contextFactory)
reactor.connectTCP('smtp.gmail.com', 25, factory)
result.addCallback(lambda ign: reactor.stop())
reactor.run()




/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py:10:
DeprecationWarning: the MimeWriter module is deprecated; use the email
package instead
  import MimeWriter, tempfile, rfc822
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 84, in
callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 69, in
callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line
59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line
37, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py",
line 146, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 137,
in doRead
    return Connection.doRead(self)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 460,
in doRead
    return self.protocol.dataReceived(data)
  File "/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py", line
251, in dataReceived
    why = self.lineReceived(line)
  File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1069,
in lineReceived
    why = self._okresponse(self.code,'\n'.join(self.resp))
  File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1134,
in smtpState_data
    self.getMailData(), self.transport, self.transformChunk)
  File "/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py", line
493, in beginFileTransfer
    self.consumer.registerProducer(self, False)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/abstract.py", line
313, in registerProducer
    producer.resumeProducing()
  File "/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py", line
499, in resumeProducing
    chunk = self.file.read(self.CHUNK_SIZE)
exceptions.AttributeError: 'str' object has no attribute 'read'
Unhandled error in Deferred:
Traceback (most recent call last):
Failure: exceptions.Exception: Consumer asked us to stop producing
Traceback (most recent call last):
  File "postprob.py", line 71, in <module>
    reactor.run()
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line
1170, in run
    self.mainLoop()
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line
1182, in mainLoop
    self.doIteration(t)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py",
line 140, in doSelect
    _logrun(selectable, _drdw, selectable, method, dict)
--- <exception caught here> ---
  File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 84, in
callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 69, in
callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line
59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line
37, in callWithContext
    return func(*args,**kw)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py",
line 156, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, method=="doRead")
  File "/usr/lib/python2.6/dist-packages/twisted/internet/posixbase.py",
line 197, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
  File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 678,
in connectionLost
    self.connector.connectionLost(reason)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line
1039, in connectionLost
    self.factory.clientConnectionLost(self, reason)
  File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1713,
in clientConnectionLost
    self._processConnectionError(connector, err)
  File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1721,
in _processConnectionError
    self.file.seek(0, 0)
exceptions.AttributeError: 'str' object has no attribute 'seek'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100713/27ace390/attachment-0001.htm 


More information about the Twisted-Python mailing list