Re: [Zope] Sending mail with attachments via Python

2005-05-03 Thread David H




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


import sys, smtplib, MimeWriter, base64, StringIO, os, string, time


 message = StringIO.StringIO()
 writer = MimeWriter.MimeWriter(message)
 writer.addheader('MIME-Version', '1.0')
 writer.addheader('Subject', 'Purchase Approved')
 #
 writer.addheader('To', '[EMAIL PROTECTED]' )
 # 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('[EMAIL PROTECTED]', '[EMAIL PROTECTED]',
message.getvalue())
 smtp.quit()





Good luck,
David


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Sending mail with attachments via Python

2005-05-03 Thread J Cameron Cooper
Kirk Strauser wrote:
> On Tuesday 03 May 2005 16:09, J Cameron Cooper wrote:
>
>> So you have it in DTML and want to re-write in Python but you don't want
>> to do it in Python?
What's wrong with External Methods? It takes all of about 5 seconds more 
than a TTW Python script. They're a necessary security precaution, but 
not all that burdensome. (You don't want your users having access to 
exec(), do you?)

> No.  I have it in DTML and want to re-write it in Python without 
involving External Methods or other hackery.
>
>> Then why change it? It works, right?
>
>
> For certain values of "works", sure.  The problem I'm trying to solve
> is that the body of the email needs to become more complex than I'm
> willing to handle with DTML.

You don't have to write the body in DTML. Create a method that does the 
body however you like and include it in the message by calling it with a 
dtml-var. You could use a Python script or ZPT or whatever.

> The natural next step for me was to look into rewriting the whole
> mail-sending object in Python, but I didn't expect that I'd have to
> reimplement the functionality of DTML to do it.
How did you want it to work? There's DTML and there's Python libs. I 
can't imagine another way, but maybe I'm in too deep.

--jcc
--
http://plonebook.packtpub.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Sending mail with attachments via Python

2005-05-03 Thread Kirk Strauser
On Tuesday 03 May 2005 16:09, J Cameron Cooper wrote:

> So you have it in DTML and want to re-write in Python but you don't want
> to do it in Python?

No.  I have it in DTML and want to re-write it in Python without involving 
External Methods or other hackery.  

> Then why change it? It works, right?

For certain values of "works", sure.  The problem I'm trying to solve is 
that the body of the email needs to become more complex than I'm willing to 
handle with DTML.  The natural next step for me was to look into rewriting 
the whole mail-sending object in Python, but I didn't expect that I'd have 
to reimplement the functionality of DTML to do it.

Please don't misunderstand me.  I'm not complaining, and if that's what I've 
got to do then so be it.  I'm just a little surprised - that's all.
-- 
Kirk Strauser
The Day Companies
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Sending mail with attachments via Python

2005-05-03 Thread J Cameron Cooper
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.
So you have it in DTML and want to re-write in Python but you don't want 
to do it in Python?

Then why change it? It works, right?
--jcc
--
http://plonebook.packtpub.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Sending mail with attachments via Python

2005-05-03 Thread Kirk Strauser
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 Strauser
The Day Companies
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Sending mail with attachments via Python

2005-05-03 Thread J Cameron Cooper
Kirk Strauser wrote:
I'm currently trying to replace a bit of DTML with a Python script.
Here's what I currently have:

To: 
From: 
Subject: 

Attention :
This file was sent to you from the Foo Company web site.
The sender included this message:



Basically, it fetches a file from a public download directory, encodes it
as a MIME attachment, and sends the works to the specified recipient.  Now,
the documentation on sending a normal text email is easy enough to find,
but from Googling around for more advanced docs it seems like I'm the only
person to ever want to do this.  Are there any examples for this sort of
thing lying around that I somehow managed not to find?
The Zope online help will provide the API for using the MailHost to send 
messages. So your problem is composing a message in Python?

Look at the Python 'email' package: 
http://python.org/doc/lib/module-email.html

Specificially the "Generating MIME documents" section. It's probably 
more useful than DTML.

You'll need to use External Methods to use this module, or allow access 
to it so that you can use Python Scripts.

		--jcc
--
http://plonebook.packtpub.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )