Problem send email through gmail with an attachment<br><br>Any help is appreciated<br><br>Thanks<br>Alvin<br><br>""" Does not work<br><br>"""<br>from twisted.internet.ssl import ClientContextFactory<br>
from twisted.internet.defer import Deferred<br>from twisted.mail.smtp import ESMTPSenderFactory<br>from twisted.internet import reactor<br>import smtplib, sys, MimeWriter, mimetypes, mimetools, base64<br><br>import StringIO<br>
import time<br>import os<br><br>sender = '<a href="mailto:acct@gmail.com">acct@gmail.com</a>'<br>senderpw = 'password'<br>to = '<a href="mailto:alvinwang@gmail.org">alvinwang@gmail.org</a>'<br>subject = 'gc' + str(time.time())<br>
text = 'Message Text Body'<br><br>attachments = ["/home/alvin/Pictures/alvin.jpg", "/home/alvin/Pictures/badlogo.gif"]<br><br>contextFactory = ClientContextFactory()<br>result = Deferred()<br>
<br>
message = StringIO.StringIO()<br>writer = MimeWriter.MimeWriter(message)<br>writer.addheader('To', to)<br>writer.addheader('From', sender)<br>writer.addheader('Subject', subject)<br>writer.addheader('MIME-Version', '1.0')<br>
writer.startmultipartbody('mixed')<br><br>part = writer.nextpart()<br>body = part.startbody('text/plain')<br>part.flushheaders()<br>body.write(text)<br><br># now add the attachments<br>if attachments is not None:<br>
for a in attachments:<br> filename = os.path.basename(a)<br> ctype, encoding = mimetypes.guess_type(a)<br> if ctype is None:<br> ctype = 'application/octet-stream'<br> encoding = 'base64'<br>
elif ctype == 'text/plain':<br> encoding = 'quoted-printable'<br> else:<br> encoding = 'base64'<br> <br> part = writer.nextpart()<br> part.addheader('Content-Transfer-Encoding', encoding)<br>
body = part.startbody("%s; name=%s" % (ctype, filename))<br> mimetools.encode(open(a, 'rb'), body, encoding)<br><br># that's all falks<br>writer.lastpart()<br>m = message.getvalue()<br>
message.close()<br><br><br>factory = ESMTPSenderFactory(sender,<br> senderpw,<br> sender,<br> to,<br> m,<br> result,<br>
contextFactory=contextFactory)<br>reactor.connectTCP('<a href="http://smtp.gmail.com">smtp.gmail.com</a>', 25, factory)<br>result.addCallback(lambda ign: reactor.stop())<br>reactor.run()<br>
<br><br><br><br>/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py:10: DeprecationWarning: the MimeWriter module is deprecated; use the email package instead<br> import MimeWriter, tempfile, rfc822<br>Traceback (most recent call last):<br>
File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 84, in callWithLogger<br> return callWithContext({"system": lp}, func, *args, **kw)<br> File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 69, in callWithContext<br>
return context.call({ILogContext: newCtx}, func, *args, **kw)<br> File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line 59, in callWithContext<br> return self.currentContext().callWithContext(ctx, func, *args, **kw)<br>
File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line 37, in callWithContext<br> return func(*args,**kw)<br>--- <exception caught here> ---<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite<br>
why = getattr(selectable, method)()<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 137, in doRead<br> return Connection.doRead(self)<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 460, in doRead<br>
return self.protocol.dataReceived(data)<br> File "/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py", line 251, in dataReceived<br> why = self.lineReceived(line)<br> File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1069, in lineReceived<br>
why = self._okresponse(self.code,'\n'.join(self.resp))<br> File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1134, in smtpState_data<br> self.getMailData(), self.transport, self.transformChunk)<br>
File "/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py", line 493, in beginFileTransfer<br> self.consumer.registerProducer(self, False)<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/abstract.py", line 313, in registerProducer<br>
producer.resumeProducing()<br> File "/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py", line 499, in resumeProducing<br> chunk = self.file.read(self.CHUNK_SIZE)<br>exceptions.AttributeError: 'str' object has no attribute 'read'<br>
Unhandled error in Deferred:<br>Traceback (most recent call last):<br>Failure: exceptions.Exception: Consumer asked us to stop producing<br>Traceback (most recent call last):<br> File "postprob.py", line 71, in <module><br>
reactor.run()<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1170, in run<br> self.mainLoop()<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1182, in mainLoop<br>
self.doIteration(t)<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py", line 140, in doSelect<br> _logrun(selectable, _drdw, selectable, method, dict)<br>--- <exception caught here> ---<br>
File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 84, in callWithLogger<br> return callWithContext({"system": lp}, func, *args, **kw)<br> File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 69, in callWithContext<br>
return context.call({ILogContext: newCtx}, func, *args, **kw)<br> File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line 59, in callWithContext<br> return self.currentContext().callWithContext(ctx, func, *args, **kw)<br>
File "/usr/lib/python2.6/dist-packages/twisted/python/context.py", line 37, in callWithContext<br> return func(*args,**kw)<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py", line 156, in _doReadOrWrite<br>
self._disconnectSelectable(selectable, why, method=="doRead")<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/posixbase.py", line 197, in _disconnectSelectable<br> selectable.connectionLost(failure.Failure(why))<br>
File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py", line 678, in connectionLost<br> self.connector.connectionLost(reason)<br> File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1039, in connectionLost<br>
self.factory.clientConnectionLost(self, reason)<br> File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1713, in clientConnectionLost<br> self._processConnectionError(connector, err)<br> File "/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py", line 1721, in _processConnectionError<br>
self.file.seek(0, 0)<br>exceptions.AttributeError: 'str' object has no attribute 'seek'<br><br><br>