Problem send email through gmail with an attachment<br><br>Any help is appreciated<br><br>Thanks<br>Alvin<br><br>&quot;&quot;&quot; Does not work<br><br>&quot;&quot;&quot;<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 = &#39;<a href="mailto:acct@gmail.com">acct@gmail.com</a>&#39;<br>senderpw = &#39;password&#39;<br>to = &#39;<a href="mailto:alvinwang@gmail.org">alvinwang@gmail.org</a>&#39;<br>subject = &#39;gc&#39; + str(time.time())<br>

text = &#39;Message Text Body&#39;<br><br>attachments = [&quot;/home/alvin/Pictures/alvin.jpg&quot;, &quot;/home/alvin/Pictures/badlogo.gif&quot;]<br><br>contextFactory = ClientContextFactory()<br>result = Deferred()<br>
<br>
message = StringIO.StringIO()<br>writer = MimeWriter.MimeWriter(message)<br>writer.addheader(&#39;To&#39;, to)<br>writer.addheader(&#39;From&#39;, sender)<br>writer.addheader(&#39;Subject&#39;, subject)<br>writer.addheader(&#39;MIME-Version&#39;, &#39;1.0&#39;)<br>

writer.startmultipartbody(&#39;mixed&#39;)<br><br>part = writer.nextpart()<br>body = part.startbody(&#39;text/plain&#39;)<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 = &#39;application/octet-stream&#39;<br>            encoding = &#39;base64&#39;<br>

        elif ctype == &#39;text/plain&#39;:<br>            encoding = &#39;quoted-printable&#39;<br>        else:<br>            encoding = &#39;base64&#39;<br>            <br>        part = writer.nextpart()<br>        part.addheader(&#39;Content-Transfer-Encoding&#39;, encoding)<br>

        body = part.startbody(&quot;%s; name=%s&quot; % (ctype, filename))<br>        mimetools.encode(open(a, &#39;rb&#39;), body, encoding)<br><br># that&#39;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(&#39;<a href="http://smtp.gmail.com">smtp.gmail.com</a>&#39;, 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 &quot;/usr/lib/python2.6/dist-packages/twisted/python/log.py&quot;, line 84, in callWithLogger<br>    return callWithContext({&quot;system&quot;: lp}, func, *args, **kw)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/log.py&quot;, line 69, in callWithContext<br>

    return context.call({ILogContext: newCtx}, func, *args, **kw)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/context.py&quot;, line 59, in callWithContext<br>    return self.currentContext().callWithContext(ctx, func, *args, **kw)<br>

  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/context.py&quot;, line 37, in callWithContext<br>    return func(*args,**kw)<br>--- &lt;exception caught here&gt; ---<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py&quot;, line 146, in _doReadOrWrite<br>

    why = getattr(selectable, method)()<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py&quot;, line 137, in doRead<br>    return Connection.doRead(self)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py&quot;, line 460, in doRead<br>

    return self.protocol.dataReceived(data)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py&quot;, line 251, in dataReceived<br>    why = self.lineReceived(line)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py&quot;, line 1069, in lineReceived<br>

    why = self._okresponse(self.code,&#39;\n&#39;.join(self.resp))<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py&quot;, line 1134, in smtpState_data<br>    self.getMailData(), self.transport, self.transformChunk)<br>

  File &quot;/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py&quot;, line 493, in beginFileTransfer<br>    self.consumer.registerProducer(self, False)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/abstract.py&quot;, line 313, in registerProducer<br>

    producer.resumeProducing()<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/protocols/basic.py&quot;, line 499, in resumeProducing<br>    chunk = self.file.read(self.CHUNK_SIZE)<br>exceptions.AttributeError: &#39;str&#39; object has no attribute &#39;read&#39;<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 &quot;postprob.py&quot;, line 71, in &lt;module&gt;<br>

    reactor.run()<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/base.py&quot;, line 1170, in run<br>    self.mainLoop()<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/base.py&quot;, line 1182, in mainLoop<br>

    self.doIteration(t)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py&quot;, line 140, in doSelect<br>    _logrun(selectable, _drdw, selectable, method, dict)<br>--- &lt;exception caught here&gt; ---<br>

  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/log.py&quot;, line 84, in callWithLogger<br>    return callWithContext({&quot;system&quot;: lp}, func, *args, **kw)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/log.py&quot;, line 69, in callWithContext<br>

    return context.call({ILogContext: newCtx}, func, *args, **kw)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/context.py&quot;, line 59, in callWithContext<br>    return self.currentContext().callWithContext(ctx, func, *args, **kw)<br>

  File &quot;/usr/lib/python2.6/dist-packages/twisted/python/context.py&quot;, line 37, in callWithContext<br>    return func(*args,**kw)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py&quot;, line 156, in _doReadOrWrite<br>

    self._disconnectSelectable(selectable, why, method==&quot;doRead&quot;)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/posixbase.py&quot;, line 197, in _disconnectSelectable<br>    selectable.connectionLost(failure.Failure(why))<br>

  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py&quot;, line 678, in connectionLost<br>    self.connector.connectionLost(reason)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/internet/base.py&quot;, line 1039, in connectionLost<br>

    self.factory.clientConnectionLost(self, reason)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py&quot;, line 1713, in clientConnectionLost<br>    self._processConnectionError(connector, err)<br>  File &quot;/usr/lib/python2.6/dist-packages/twisted/mail/smtp.py&quot;, line 1721, in _processConnectionError<br>

    self.file.seek(0, 0)<br>exceptions.AttributeError: &#39;str&#39; object has no attribute &#39;seek&#39;<br><br><br>