]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
Mail generiert
authorFrank Brehm <frank@brehm-online.com>
Fri, 8 Jul 2011 09:41:46 +0000 (09:41 +0000)
committerFrank Brehm <frank@brehm-online.com>
Fri, 8 Jul 2011 09:41:46 +0000 (09:41 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@283 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateCommon.py
LogRotateMailer.py
logrotate.py
po/LogRotateCommon.de.po
po/LogRotateCommon.pot

index 3a13582008090860a82b7a9257259a876aef9573..af03d1be56b21f280f050f298b2b995926130243 100755 (executable)
@@ -479,6 +479,29 @@ def get_address_list(address_str, verbose = 0):
 
     return addresses
 
+#------------------------------------------------------------------------
+
+def to_unicode_or_bust(obj, encoding='utf-8'):
+    '''
+    Transforms a string, what is not a unicode string, into a unicode string.
+    All other objects are left untouched.
+
+    @param obj: the object to transform
+    @type obj:  object
+    @param encoding: the encoding to use to decode the object
+                     defaults to 'utf-8'
+    @type encoding:  str
+
+    @return: the maybe decoded object
+    @rtype:  object
+    '''
+
+    if isinstance(obj, basestring):
+        if not isinstance(obj, unicode):
+            obj = unicode(obj, encoding)
+
+    return obj
+
 #========================================================================
 
 if __name__ == "__main__":
index 8eb20c42d50d87626e3121377da756c0c40ccc52..9ff2628f63c2b7bbe501af2f5ea4393bbe04ecc4 100755 (executable)
@@ -20,18 +20,24 @@ import pprint
 import gettext
 import os
 import os.path
+import sys
 import pwd
 import socket
 import csv
 
+from datetime import datetime
+
 import mimetypes
 import email.utils
 from email import encoders
 from email.message import Message
 from email.mime.base import MIMEBase
 from email.mime.multipart import MIMEMultipart
+from email.mime.nonmultipart import MIMENonMultipart
 from email.mime.text import MIMEText
 
+from quopri import encodestring as _encodestring
+
 from LogRotateCommon import email_valid
 
 revision = '$Revision$'
@@ -437,8 +443,14 @@ class LogRotateMailer(object):
         return
 
     #-------------------------------------------------------
-    def send_file(self, filename, addresses, original=None,
-            mime_type='text/plain'):
+    def send_file(self,
+            filename,
+            addresses,
+            original=None,
+            mime_type='text/plain',
+            rotate_date=None,
+            charset=None
+            ):
         '''
         Mails the file with the given file name as an attachement
         to the given recipient(s).
@@ -458,6 +470,11 @@ class LogRotateMailer(object):
         @param mime_type: MIME type (content type) of the original logfile,
                           defaults to 'text/plain'
         @type mime_type:  str
+        @param rotate_date: datetime object of rotation, defaults to now()
+        @type rotate_date:  datetime or None
+        @param charset: character set of (uncompreesed) logfile, if the
+                        mime_type is 'text/plain', defaults to 'utf-8'
+        @type charset:  str or None
 
         @return: success of sending
         @rtype:  bool
@@ -479,6 +496,9 @@ class LogRotateMailer(object):
         if not original:
             original = os.path.abspath(filename)
 
+        if not rotate_date:
+            rotate_date = datetime.now()
+
         msg = _("Sending mail with attached file '%(file)s' to: %(rcpt)s") \
                 % {'file': basename,
                    'rcpt': ', '.join(map(lambda x: '"' + email.utils.formataddr(x) + '"', addresses))}
@@ -489,6 +509,20 @@ class LogRotateMailer(object):
         mail_container['X-Mailer'] = ( "pylogrotate version %s" % (self.mailer_version) )
         mail_container['From']     = self.from_address
         mail_container['To']       = ', '.join(map(lambda x: email.utils.formataddr(x), addresses))
+        mail_container.preamble = 'You will not see this in a MIME-aware mail reader.\n'
+
+        # Generate Text of the first part of mail body
+        mailtext = "Rotated Logfile:\n\n"
+        mailtext += "\t - " + filename + "\n"
+        mailtext += "\t   (" + original + ")\n"
+        mailtext += "\n"
+        mailtext += "Date of rotation: " + rotate_date.isoformat(' ')
+        mailtext += "\n"
+        mailtext = _encodestring(mailtext, quotetabs=False)
+        mail_part = MIMENonMultipart('text', 'plain', charset=sys.getdefaultencoding())
+        mail_part.set_payload(mailtext)
+        mail_part['Content-Transfer-Encoding'] = 'quoted-printable'
+        mail_container.attach(mail_part)
 
         ctype, encoding = mimetypes.guess_type(filename)
         if self.verbose > 3:
@@ -496,8 +530,34 @@ class LogRotateMailer(object):
                     % {'ctype': ctype, 'encoding': encoding }
             self.logger.debug(msg)
 
+        if encoding:
+            if encoding == 'gzip':
+                ctype = 'application/x-gzip'
+            elif encoding == 'bzip2':
+                ctype = 'application/x-bzip2'
+            else:
+                ctype = 'application/octet-stream'
+
+        if not ctype:
+            ctype = mime_type
+
+        maintype, subtype = ctype.split('/', 1)
+        fp = open(filename, 'rb')
+        mail_part = MIMEBase(maintype, subtype)
+        mail_part.set_payload(fp.read())
+        fp.close()
+        if maintype == 'text':
+            msgtext = mail_part.get_payload()
+            msgtext =  _encodestring(msgtext, quotetabs=False)
+            mail_part.set_payload(msgtext)
+            mail_part['Content-Transfer-Encoding'] = 'quoted-printable'
+        else:
+            encoders.encode_base64(mail_part)
+        mail_part.add_header('Content-Disposition', 'attachment', filename=basename)
+        mail_container.attach(mail_part)
+
         composed = mail_container.as_string()
-        if self.verbose > 2:
+        if self.verbose > 4:
             msg = _("Generated E-mail:") + "\n" + composed
             self.logger.debug(msg)
 
index 695cf5a709528238879cec63fbd05113dae3e7e6..c252ec07fa6e01967eb90d9874ac818d70e7e48f 100755 (executable)
@@ -36,7 +36,7 @@ revision = re.sub( r'Revision: ', r'r', revision )
 __author__    = 'Frank Brehm'
 __copyright__ = '(C) 2011 by Frank Brehm, Berlin'
 __contact__    = 'frank@brehm-online.com'
-__version__    = '0.5.1 ' + revision
+__version__    = '0.5.2 ' + revision
 __license__    = 'GPL3'
 
 
index 2332cd022adcaad2d74d4ecff8e06e3b8ff6f3ac..413cd0c18fb02e2f09420ec4a3417694d4888729 100644 (file)
@@ -127,3 +127,16 @@ msgstr "Ungültiger Inhalt für eine Periode: »%s«."
 #, python-format
 msgid "Total %f days found."
 msgstr "Insgesamt %f Tage gefunden."
+
+#: LogRotateCommon.py:464
+msgid "Found address entries:"
+msgstr "Gefundene Adresseinträge:"
+
+#: LogRotateCommon.py:472
+msgid "Got mail address pair:"
+msgstr "Mailadress-Paar gefunden:"
+
+#: LogRotateCommon.py:475
+#, python-format
+msgid "Found invalid mail address '%s'."
+msgstr "Ungültige Mailadresse »%s« gefunden."
index da80be630fd6513ac131fad6d06d85d3fd3f5736..8268bd3360a81833c0100b84f93e9c68a0c9807d 100644 (file)
@@ -127,3 +127,16 @@ msgstr ""
 #, python-format
 msgid "Total %f days found."
 msgstr ""
+
+#: LogRotateCommon.py:464
+msgid "Found address entries:"
+msgstr ""
+
+#: LogRotateCommon.py:472
+msgid "Got mail address pair:"
+msgstr ""
+
+#: LogRotateCommon.py:475
+#, python-format
+msgid "Found invalid mail address '%s'."
+msgstr ""