[Zope] Sending mail with attachments via Python
David H
bluepaul at earthlink.net
Tue May 3 19:28:15 EDT 2005
Kirk Strauser wrote:
>On Tuesday 03 May 2005 15:30, J Cameron Cooper wrote:
>
>
>
>>Look at the Python 'email' package:
>>http://python.org/doc/lib/module-email.html
>>
>>You'll need to use External Methods to use this module, or allow access
>>to it so that you can use Python Scripts.
>>
>>
>
>That's what I was afraid of. I love Zope and Python (and am getting ready
>to write a magazine article about the two) but jumping through Pythonic
>hoops to do stuff that would be easy in DTML drives me nuts.
>
>Thanks for the pointer, though. I'd stumbled across that earlier, but had
>kept looking in search of something easier.
>
>
Kirk,
Here is some to send email + attachement. I used it to send PDFs. Use
it as a guide if you choose the external python method option.
I think MimeWriter makes it easier.
Best luck,
David
<code snippets>
import sys, smtplib, MimeWriter, base64, StringIO, os, string, time
<essential code>
message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader('MIME-Version', '1.0')
writer.addheader('Subject', 'Purchase Approved')
#
writer.addheader('To', 'somebody at where.com' )
# get ready to send attachment
writer.startmultipartbody('mixed')
# start off with a text/plain part
part = writer.nextpart()
body = part.startbody('text/plain')
body.write('Services Inc.\n')
body.write('Office Transaction notice\n')
body.write('\n')
body.write('Order: ' + itemNumber + '\n')
body.write(' Authorized by: ' + userID + ' ' + datetime + '\n')
body.write(' Amount: ' + amount + '\n')
body.write('\nSee attached')
# ............................
# add PDF attachment
# ............................
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('application/pdf; name=%s' % attachment)
# pdf file
body.write(base64.encodestring( pdf ))
# finish off
writer.lastpart()
# ..................................................
# send the mail
# . if user supplied userid/password then deal w/it
# ..................................................
smtp = smtplib.SMTP(self.MailHost.smtp_host)
if self.MailHost.smtp_userid:
smtp.ehlo()
smtp_userid64 = base64.encodestring(self.MailHost.smtp_userid)
smtp.docmd("auth", "login " + smtp_userid64[:-1])
if self.MailHost.smtp_pass:
smtp_pass64 = base64.encodestring( self.MailHost.smtp_pass)
smtp.docmd(smtp_pass64[:-1])
#smtp.sendmail( from address, to address, message body)
smtp.sendmail('Acme at Acme.com', 'somebody at somewhere.com',
message.getvalue())
smtp.quit()
</end essential code>
Good luck,
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope/attachments/20050503/671e1588/attachment.htm
More information about the Zope
mailing list