[Zope-dev] MIMETag.py patch

Ava ava@dde974.equipement.gouv.fr
Mon, 29 Nov 1999 10:42:49 +0400


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01BF3A34.F440D940
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,

I found myself having a need to send mail with the ability to customize
stuff like disposition, name, filename, encoding, etc.
I modified the MIMETag to allow the use of type_expr, disposition_expr,
encode_expr, name_expr, filename_expr, and skip_expr attributes in the =
mime
and boundary tags.

* type_expr, disposition_expr, encode_expr, and name_expr work like =
type,
disposition, encode and name, except that they take expressions instead =
of
names.
* filename_expr allows to give the filename of the attachment. Note =
that the
filename attribute is included only if the disposition attribute is =
set, and
that in the header, the filename attribute is attached to the
Content-Disposition: header, just like the name attribute is attached =
to the
Content-Type: header. I don't know if it is ok, but it seems to be the =
way
Netscape Messenger and Microsoft Outlook do it.

* skip_expr allows to skip an attachment if the skip_expr evaluates to =
true.

Here is an example. document is a FileUpload object.

<dtml-sendmail mailhost=3DMailHost>
to: you@anywhere.com
from: me@nowhere.com
subject: this is the subject
<dtml-mime type=3Dtext/plain>
Thanks for you input.
<dtml-if "document.filename">
The file you attached is <dtml-var "document.filename">
<dtml-else>
You did not attach any file
</dtml-if>
<dtml-boundary disposition=3Dattachment =
filename_expr=3D"document.filename"
name_expr=3D"document.filename" skip_expr=3D"not document.filename"
type_expr=3D"document.headers['Content-Type']" =
encode=3Dbase64><dtml-var
"document.read()"></dtml-mime>
</dtml-sendmail>

Please send me any comment on this work.

Regards,

Jephte CLAIN
minf7@educ.univ-reunion.fr

PS: r=E9pondez sur minf7@educ.univ-reunion.fr, et pas sur
ava@dde974.equipement.gouv.fr


------_=_NextPart_000_01BF3A34.F440D940
Content-Type: application/octet-stream;
	name="MIMETag.py.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="MIMETag.py.patch"

--- MIMETag.py.orig	Thu May 20 01:54:02 1999
+++ MIMETag.py	Fri Nov 26 12:40:40 1999
@@ -105,39 +105,61 @@
         self.sections =3D []
=20
         for tname, args, section in blocks:
-            args =3D parse_params(args, type=3DNone, =
disposition=3DNone,
-                                encode=3DNone, name=3DNone)
+            args =3D parse_params(args,
+                                type=3DNone, type_expr=3DNone,
+                                disposition=3DNone, =
disposition_expr=3DNone,
+                                encode=3DNone, encode_expr=3DNone,
+                                name=3DNone, name_expr=3DNone,
+                                filename=3DNone, filename_expr=3DNone,
+                                skip_expr=3DNone)
=20
             has_key=3Dargs.has_key
=20
-            if has_key('type'):=20
-                type =3D args['type']
-            else:
-                type =3D 'application/octet-stream'
-
-            if has_key('disposition'):
-                disposition =3D args['disposition']
-            else:
-                disposition =3D ''
+            if has_key('type_expr'):
+                if has_key('type'):
+                    raise ParseError, _tm('type and type_expr given', =
'mime')
+                args['type_expr']=3DVSEval.Eval(args['type_expr'], =
expr_globals)
+            elif not has_key('type'):
+                args['type']=3D'application/octet-stream'
+
+            if has_key('disposition_expr'):
+                if has_key('disposition'):
+                    raise ParseError, _tm('disposition and =
disposition_expr given', 'mime')
+                =
args['disposition_expr']=3DVSEval.Eval(args['disposition_expr'], =
expr_globals)
+            elif not has_key('disposition'):
+                args['disposition']=3D''
+
+            if has_key('encode_expr'):
+                if has_key('encode'):
+                    raise ParseError, _tm('encode and encode_expr =
given', 'mime')
+                args['encode_expr']=3DVSEval.Eval(args['encode_expr'], =
expr_globals)
+            elif not has_key('encode'):
+                args['encode']=3D'base64'
+
+            if has_key('name_expr'):
+                if has_key('name'):
+                    raise ParseError, _tm('name and name_expr given', =
'mime')
+                args['name_expr']=3DVSEval.Eval(args['name_expr'], =
expr_globals)
+            elif not has_key('name'):
+                args['name']=3D''
+
+            if has_key('filename_expr'):
+                if has_key('filename'):
+                    raise ParseError, _tm('filename and filename_expr =
given', 'mime')
+                =
args['filename_expr']=3DVSEval.Eval(args['filename_expr'], =
expr_globals)
+            elif not has_key('filename'):
+                args['filename']=3D''
=20
-            if has_key('encode'):
-                encode =3D args['encode']
-            else:
-                encode =3D 'base64'
-
-            if has_key('name'):
-                name =3D args['name']
-            else:
-                name =3D ''
+            if has_key('skip_expr'):
+                args['skip_expr']=3DVSEval.Eval(args['skip_expr'], =
expr_globals)
=20
-            if encode not in \
+            if args['encode'] not in \
             ('base64', 'quoted-printable', 'uuencode', 'x-uuencode',
              'uue', 'x-uue', '7bit'):
                 raise MIMEError, (
                     'An unsupported encoding was specified in tag')
=20
-            self.sections.append((type, disposition, encode,=20
-                                  name,  section.blocks))
+            self.sections.append((args, section.blocks))
=20
=20
     def render(self, md):
@@ -145,11 +167,34 @@
         mw =3D MimeWriter(StringIO())
         outer =3D mw.startmultipartbody('mixed')
         for x in self.sections:
+            a, b =3D x
+            has_key=3Da.has_key
+
+            if has_key('skip_expr') and a['skip_expr'].eval(md):
+                continue
+               =20
             inner =3D mw.nextpart()
-            t, d, e, n, b =3D x
+
+            if has_key('type_expr'): t=3Da['type_expr'].eval(md)
+            else: t=3Da['type']
+
+            if has_key('disposition_expr'): =
d=3Da['disposition_expr'].eval(md)
+            else: d=3Da['disposition']
+
+            if has_key('encode_expr'): e=3Da['encode_expr'].eval(md)
+            else: e=3Da['encode']
+
+            if has_key('name_expr'): n=3Da['name_expr'].eval(md)
+            else: n=3Da['name']
+           =20
+            if has_key('filename_expr'): =
f=3Da['filename_expr'].eval(md)
+            else: f=3Da['filename']
=20
             if d:
-                inner.addheader('Content-Disposition', d)
+                if f:
+                    inner.addheader('Content-Disposition', '%s;\n =
filename=3D"%s"' % (d, f))
+                else:
+                    inner.addheader('Content-Disposition', d)
=20
             inner.addheader('Content-Transfer-Encoding', e)
             if n:
@@ -163,14 +208,16 @@
             if e =3D=3D '7bit':
                 innerfile.write(render_blocks(b, md))
             else:
-                mimetools.encode(StringIO(render_blocks(b, md)),=20
+                mimetools.encode(StringIO(render_blocks(b, md)),
                                  output, e)
                 output.seek(0)
                 innerfile.write(output.read())
=20
-            if x is self.sections[-1]:
-                mw.lastpart()
-                 =20
+        # XXX what if self.sections is empty ??? does it matter that =
mw.lastpart() is called
+        # right after mw.startmultipartbody() ?
+        if x is self.sections[-1]:
+            mw.lastpart()
+
         outer.seek(0)
         return outer.read()
=20

------_=_NextPart_000_01BF3A34.F440D940--