mailodf 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2006 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. from odf.odf2xhtml import ODF2XHTML
  21. import zipfile
  22. import sys, os, smtplib, getopt
  23. from email.mime.multipart import MIMEMultipart
  24. from email.mime.nonmultipart import MIMENonMultipart
  25. from email.mime.text import MIMEText
  26. from email.encoders import encode_base64
  27. if sys.version_info[0]==3: unicode=str
  28. def usage():
  29. sys.stderr.write("Usage: %s [-f from] [-s subject] inputfile recipients...\n" % sys.argv[0])
  30. try:
  31. opts, args = getopt.getopt(sys.argv[1:], "f:s:", ["from=", "subject="])
  32. except getopt.GetoptError:
  33. usage()
  34. sys.exit(2)
  35. fromaddr = os.getlogin() + "@" + os.getenv('HOSTNAME','localhost')
  36. subject = None
  37. for o, a in opts:
  38. if o in ("-f", "--from"):
  39. fromaddr = a
  40. if o in ("-s", "--subject"):
  41. subject = a
  42. if len(args) < 2:
  43. usage()
  44. sys.exit(2)
  45. suffices = {
  46. 'wmf':('image','x-wmf'),
  47. 'png':('image','png'),
  48. 'gif':('image','gif'),
  49. 'jpg':('image','jpeg'),
  50. 'jpeg':('image','jpeg')
  51. }
  52. msg = MIMEMultipart('related',type="text/html")
  53. msg['From'] = fromaddr
  54. # msg['Date'] = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
  55. msg['To'] = ','.join(args[1:])
  56. msg.preamble = 'This is a multi-part message in MIME format.'
  57. msg.epilogue = ''
  58. odhandler = ODF2XHTML()
  59. result = odhandler.odf2xhtml(unicode(args[0]))
  60. if subject:
  61. msg['Subject'] = subject
  62. else:
  63. msg['Subject'] = odhandler.title
  64. htmlpart = MIMEText(result,'html','us-ascii')
  65. htmlpart['Content-Location'] = 'index.html'
  66. msg.attach(htmlpart)
  67. z = zipfile.ZipFile(unicode(args[0]))
  68. for file in z.namelist():
  69. if file[0:9] == 'Pictures/':
  70. suffix = file[file.rfind(".")+1:]
  71. main,sub = suffices.get(suffix,('application','octet-stream'))
  72. img = MIMENonMultipart(main,sub)
  73. img.set_payload(z.read(file))
  74. img['Content-Location'] = "" + file
  75. encode_base64(img)
  76. msg.attach(img)
  77. z.close()
  78. server = smtplib.SMTP('localhost')
  79. #server.set_debuglevel(1)
  80. server.sendmail(fromaddr, args[1:], msg.as_string())
  81. server.quit()
  82. # Local Variables: ***
  83. # mode: python ***
  84. # End: ***