wilde: utils/admin tagedmailnote, 1.1, 1.2 tagedmailnote.README, 1.2, 1.3
cvs at kolab.org
cvs at kolab.org
Wed Feb 24 17:52:42 CET 2010
Author: wilde
Update of /kolabrepository/utils/admin
In directory doto:/tmp/cvs-serv28428
Modified Files:
tagedmailnote tagedmailnote.README
Log Message:
Many improvements and enhancements:
- use email module instead of deprecated stuff
- enable case insensitive matching if tag is all lower case
- handle encoded subject lines correctly
Index: tagedmailnote
===================================================================
RCS file: /kolabrepository/utils/admin/tagedmailnote,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- tagedmailnote 19 Jan 2010 10:47:04 -0000 1.1
+++ tagedmailnote 24 Feb 2010 16:52:40 -0000 1.2
@@ -25,42 +25,57 @@
Usage: tagmailnote Tag From-Address To-Address
Sends a mail to To-Address if the mail on stdin contains Tag in its subject.
+If Tag is all lower case the match is case insensitive
"""
notesubject = "A mail tagged URGENT arrived at your company account."
notetext = """A mail with the keyword URGENT in the subject was send to your
company account. Visit your company as soon as possible."""
-import sys, smtplib, md5, mimetools, MimeWriter, rfc822, quopri, StringIO
+import sys, smtplib, md5
+import email, email.mime.nonmultipart, email.encoders, email.header
-def sendmail(fromaddr, toaddr, subject, text):
- msgid = "<%s@%s>" % (md5.new(mimetools.choose_boundary()).hexdigest()[:16],
- fromaddr.split('@')[1])
- message = StringIO.StringIO()
- writer = MimeWriter.MimeWriter(message)
- writer.addheader('MIME-Version', '1.0')
- writer.addheader('From', fromaddr)
- writer.addheader('To', toaddr)
- writer.addheader('Subject', subject)
- writer.addheader('Message-Id', msgid)
- writer.addheader('Content-Transfer-Encoding', 'quoted-printable')
- body = writer.startbody('text/plain; charset="utf-8"')
- # Add body
- body.write(quopri.encodestring(text))
+def sendmail(fromaddr, toaddr, subject, text):
+ message = email.mime.nonmultipart.MIMENonMultipart('text', 'plain',
+ charset="utf-8")
+ message.add_header('From', fromaddr)
+ message.add_header('To', toaddr)
+ message.add_header('Subject',
+ email.header.Header(subject, 'utf-8').encode())
+ message.add_header('Message-Id',
+ email.utils.make_msgid(fromaddr.split('@')[1]))
+ # write and encode body
+ message.set_payload(text)
+ email.encoders.encode_quopri(message)
# send the mail
smtp = smtplib.SMTP('localhost')
- smtp.sendmail(fromaddr, toaddr, message.getvalue())
+ smtp.sendmail(fromaddr, toaddr, message.as_string())
smtp.quit()
+def lowermaybe(string, dolower):
+ if dolower:
+ return string.decode('utf8').lower().encode('utf-8')
+ else:
+ return string
+
+
+def decoded_header_to_utf8(header):
+ return " ".join([text.decode(enc or 'ascii').encode('utf-8')
+ for text, enc in header])
+
+
if __name__ == '__main__':
if len(sys.argv) >= 4:
tag, fromaddr, toaddr = sys.argv[1:4]
- mail = rfc822.Message(sys.stdin)
- subject = mail.getfirstmatchingheader("Subject")[0]
- if subject and subject.find(tag) > -1:
+ mail = email.message_from_file(sys.stdin)
+ subject = (decoded_header_to_utf8(email.header.decode_header(
+ mail.get("Subject"))))
+ caseinsens = tag.islower()
+ if subject and (lowermaybe(subject, caseinsens)
+ .find(lowermaybe(tag, caseinsens))) > -1:
sendmail(fromaddr, toaddr, notesubject, notetext)
else:
print __doc__
Index: tagedmailnote.README
===================================================================
RCS file: /kolabrepository/utils/admin/tagedmailnote.README,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- tagedmailnote.README 24 Feb 2010 14:51:59 -0000 1.2
+++ tagedmailnote.README 24 Feb 2010 16:52:40 -0000 1.3
@@ -17,6 +17,10 @@
Connect this service to a new address by adding the following line to
the file /kolab/etc/kolab/templates/transport.template:
+If the tag ("[URGENT]" in the example) is written all lower case
+(i.e. "[urgent]") the match on the subject line is performed case
+insensitive.
+
foo-tagged at example.net tagedmailnote-bar-example-com:
Now you can use /kolab/etc/kolab/templates/virtual.template to forward mails
More information about the commits
mailing list