MailMessage.py 4.41 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2
##############################################################################
#
3 4 5
# Copyright (c) 2002-2006 Nexedi SARL and Contributors. All Rights Reserved.
#                         Jean-Paul Smets-Solanes <jp@nexedi.com>
#                         Kevin Deldycke          <kevin@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from Globals import InitializeClass
from AccessControl import ClassSecurityInfo

33
from Products.CMFMailIn.MailMessage import MailMessage as CMFMailInMessage
34
from Products.ERP5Type import Permissions, PropertySheet, Constraint, interfaces
Jean-Paul Smets's avatar
Jean-Paul Smets committed
35

36
from Products.ERP5.Document.Event import Event
Jean-Paul Smets's avatar
Jean-Paul Smets committed
37 38 39 40
import smtplib

from zLOG import LOG

41 42
_MARKER = []

43
class MailMessage(Event, CMFMailInMessage):
44
  """
45 46 47 48 49 50 51 52 53
  LEGACY

  The MailMessage class is deprecated. It is superceded by
  the Event class (for CRM events) and by the EmailDocument class
  (to store raw email messages).

  TODO: compatibility layer is required so that old MailMessage
  instance can mimic Event instances based on legacy data. This
  is required for example for old CRM implementations of ERP5.
54 55
  """

56 57 58
  meta_type       = 'ERP5 Mail Message'
  portal_type     = 'Mail Message'
  add_permission  = Permissions.AddPortalContent
59
  isPortalContent = 1
60
  isRADContent    = 1
61 62 63 64 65 66 67 68 69 70 71

  # Declarative security
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  # Declarative properties
  property_sheets = ( PropertySheet.Base
                    , PropertySheet.XMLObject
                    , PropertySheet.DublinCore
                    , PropertySheet.Task
                    , PropertySheet.Arrow
72 73
                    , PropertySheet.Movement
                    , PropertySheet.Event
74 75 76 77 78
                    , PropertySheet.MailMessage
                    )

  def getReplyBody(self):
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
79 80
      This is used in order to respond to a mail,
      this put a '> ' before each line of the body
81 82
    """
    reply_body = ''
83 84 85
    body = self.getBody()
    if type(body) is type('a'):
      reply_body = '> ' + body.replace('\n', '\n> ')
86 87 88 89
    return reply_body

  def getReplySubject(self):
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
90
      This is used in order to respond to a mail,
91
      this put a 'Re: ' before the orignal subject
92 93
    """
    reply_subject = self.getTitle()
94
    if reply_subject.find('Re: ') != 0:
95 96
      reply_subject = 'Re: ' + reply_subject
    return reply_subject
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  def send(self, from_url=None, to_url=None, msg=None, subject=None):
    """
      Sends a reply to this mail message.
    """
    # We assume by default that we are replying to the sender
    if from_url == None:
      from_url = self.getUrlString()
    if to_url == None:
      to_url = self.getSender()
    if msg is not None and subject is not None:
      header  = "From: %s\n"    % from_url
      header += "To: %s\n\n"    % to_url
      header += "Subject: %s\n" % subject
      header += "\n"
      msg = header + msg
      self.MailHost.send( msg )
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128


  security.declareProtected(Permissions.AccessContentsInformation, 'getTextContent')
  def getTextContent(self, default=_MARKER):
    """
    Overload EmailDocument method to add backward compatibility layer
    """
    if getattr(self, 'body', None) is not None:
      return self.getBody(default)
    else:
      if default is _MARKER:
        return Event.getTextContent(self)
      else:
        return Event.getTextContent(self, default)