[Zope] ZMailIn patch

Jean Jordaan jean@upfrontsystems.co.za
Fri, 7 Jun 2002 15:59:38 +0200

This is a multi-part message in MIME format.

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit

Hi there

I've modified zopeMailIn.py to look for a client based on firstly the
recipient fields, and secondly the 'cc' and 'bcc' fields (so that I 
can copy Zope on a mail I'm sending to a client, for example). I'm 
sending on the diff against ZMailIn-1_0_0 in case it's of interest ..

Jean Jordaan
Upfront Systems                         http://www.upfrontsystems.co.za

Content-Type: application/octet-stream;
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;

--- =
/usr/local/upfront/library/software/zope/products/ZMailIn/zopeMailIn.py	=
Mon Feb 11 18:56:25 2002=0A=
+++ zopeMailIn.py	Fri Jun  7 15:38:00 2002=0A=
@@ -1,4 +1,4 @@=0A=
 # This python module will send a mail message to a zope instance that =
has the relevent=0A=
 # ZMailIn client present.=0A=
@@ -12,6 +12,7 @@=0A=
 def GetLocationHTTP(host, emailaddress):=0A=
         # Attempt to get the location of the ZMailIn client for the =
given email address=0A=
                 returned =3D urllib.urlopen(host, =
         except IOError,e:=0A=
@@ -21,68 +22,101 @@=0A=
 def _make_url(host=3DzopeMailInConf.host, path=3D''):=0A=
-        =0A=
         if zopeMailInConf.username and zopeMailInConf.password:=0A=
-                =0A=
         return host[:7] + user_and_pass + host[7:] + path=0A=
 if __name__ =3D=3D '__main__':=0A=
         # This gets called by the MTA when a new message arrives.=0A=
         # The mail message file gets passed in on the stdin=0A=
-        =0A=
         f =3D sys.stdin   # First get a handle on the message file=0A=
         messageText =3D f.read()=0A=
-        =0A=
         url =3D ''=0A=
         if len(sys.argv)>1:=0A=
                 url =3D sys.argv[1]=0A=
-        =0A=
         if not url:=0A=
                 # get a url=0A=
                 mf =3D rfc822.Message(StringIO.StringIO(messageText))=0A=
-                messageTo =3D mf.get('envelope-to',mf.get('to',''))=0A=
-                if not messageTo:=0A=
-                        print "ZMailIn Error: No 'envelope-to' or 'to' =
header in message"=0A=
+                # rcc: this is not very resilient=0A=
+                # njj: it'll fail if either of these headers contain=0A=
+                # multiple addresses. Use mail objects' 'getaddr' method=0A=
+                # instead of list subscription.=0A=
+                # messageTo =3D mf.get('envelope-to',mf.get('to',''))=0A=
+                #=0A=
+                # Check for recipient first ('envelope-to', then 'to');=0A=
+                # then for cc ('cc', then 'bcc'). Then look for a=0A=
+                # ZMailIn Client: if not 'recipient_client', look for=0A=
+                # 'cc_client'.=0A=
+                #=0A=
+                name, recipient_client =3D mf.getaddr('envelope-to')=0A=
+                if not recipient_client:=0A=
+                    name, recipient_client =3D mf.getaddr('to')=0A=
+                name, cc_client =3D mf.getaddr('cc')=0A=
+                if not cc_client:=0A=
+                    name, cc_client =3D mf.getaddr('bcc')=0A=
+                if (not recipient_client) and (not cc_client):=0A=
+                        print "ZMailIn Error: No 'envelope-to', 'to', =
'cc' or 'bcc' header in message"=0A=
-                        =0A=
                 host =3D _make_url(path=3D'getZMailInClientLocation')=0A=
-                # Try to get the exact email address=0A=
-                url =3D GetLocationHTTP(host, messageTo)=0A=
-                =0A=
-                if url[:4]=3D=3D'FAIL':=0A=
+                for client_address in [recipient_client, cc_client]:=0A=
+                    if not client_address: continue=0A=
+                    # Try to get the exact email address=0A=
+                    url =3D GetLocationHTTP(host, client_address)=0A=
+                    if url[:4]=3D=3D'FAIL':=0A=
                         print "ZMailIn Error:",url=0A=
-                        sys.exit(1)=0A=
-                =0A=
-                if url=3D=3D'Not Found':=0A=
-                        # We didn't get the exact email address so try =
for a domain match only=0A=
-                        domain =3D =
+                        # sys.exit(1)=0A=
+                        url =3D None=0A=
+                    elif url=3D=3D'Not Found':=0A=
+                        # We didn't get the exact email address so try =
for a=0A=
+                        # domain match only=0A=
+                        domain =3D client_address[=0A=
+                                    string.rfind(client_address,'@'):]=0A=
                         url =3D GetLocationHTTP(host, domain)=0A=
-                =0A=
-                if url=3D=3D'Not Found' or '\n' in url:=0A=
-                        print "ZMailIn Error: ZMailIn Client does not =
-                        sys.exit(1)=0A=
-                        =0A=
-                url =3D _make_url(url)=0A=
+                        if url=3D=3D'Not Found' or '\n' in url:=0A=
+                            print "ZMailIn Error: ZMailIn Client does =
not exist %s"%client_address=0A=
+                            # sys.exit(1)=0A=
+                            url =3D None=0A=
+                    if url: break # OK, we've found one, stop looking.=0A=
+                if url:=0A=
+                    url =3D _make_url(url)=0A=
+                    print "Found client at %s"%url=0A=
+                else:=0A=
+                    sys.exit(1)=0A=
         if url:=0A=
-                =0A=
                 url =3D url + '/postZMailMessage'=0A=
                         result =3D urllib.urlopen(url, =
                 except (IOError,EOFError),e:=0A=
                         print "ZMailIn Error: Problem Connecting to =
-                =0A=
                 # if the ZMailIn Client's method returned anything, =
then 'something bad' happened.=0A=
                 if result:=0A=
                         print result=0A=
-                        =0A=
         print "ZMailIn Error: No ZMailIn Client URL found or specified."=0A=
-        =0A=
-        =0A=