EventModule_createEventFromMail.py 3.95 KB
Newer Older
1
## Script (Python) "EventModule_createEventFromMail"
Jean-Paul Smets's avatar
Jean-Paul Smets committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=theMail
##title=
##
import string

mail_message = None

try:
  id = container.strip_punctuation(theMail['headers']['message-id'])
16 17 18 19 20 21 22 23 24 25
  subject = theMail['headers'].get('subject')
  attachments = theMail['attachments']
  if subject.find('PhoneCall:') >= 0 or subject.find('j2 Voice Message') >= 0:
    mail_message = context.event.newContent(portal_type='Phone Call', id=id)
  elif subject.find('Letter:') >= 0:
    mail_message = context.event.newContent(portal_type='Incoming Letter', id=id)
  elif subject.find('j2 Fax') >= 0 or subject.find('fax from') >= 0:
    mail_message = context.event.newContent(portal_type='Incoming Fax', id=id)
  else:
    mail_message = context.event.newContent(portal_type='Mail Message', id=id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
26 27 28

  subject=str(string.join(theMail['headers'].get('subject')))

29 30 31 32 33 34 35 36 37 38
  mail_message.edit(
                  title = theMail['headers'].get('subject'),
                  date = theMail['headers'].get('date'),
                  to = theMail['headers'].get('to'),
                  sender = theMail['headers'].get('from'),
                  reply_to = theMail['headers'].get('replyto'),
                  body = theMail['body'],
                  header = theMail['headers'],
                  other_info = theMail['localpart'],
                 )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
39

40 41 42 43 44 45 46 47
  for key, attachment_data in attachments.items():
    try:
      #portal_type = context.content_type_registry.findTypeName(key, '//', attachment_data)
      portal_type = 'File'
      new_file = mail_message.newContent(portal_type = portal_type , id=key.replace('/','_'), file=attachment_data)  
    except:
      mail_message.setDescription('Error in creating attachments')
     
Jean-Paul Smets's avatar
Jean-Paul Smets committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  # We should now try to guess the user who sent it
  # Guess the mail address:
  mail_from = theMail['headers'].get('from')
  at_place = mail_from.find('@')
  mail_address = None
  if at_place != -1:
    begin = max(mail_from.rfind('<',0,at_place),mail_from.rfind(' ',0,at_place))
    end = min(mail_from.rfind('>',at_place),mail_from.rfind(' ',at_place),len(mail_from))
    mail_address = mail_from[begin+1:end]
  # find the person with this mail
  if mail_address is not None:
    kw = {'portal_type':'Email',
          'query':"SearchableText LIKE '%%%s%%'" % mail_address }
    result = context.portal_catalog.searchResults(**kw)
    for object in result:
      object = object.getObject()
      parent = object.aq_parent
      if parent.getPortalType() == 'Person':
67 68 69 70 71 72
        organisation = parent.getSubordinationValue()
        if organisation is None:
          mail_message.setSourceValue(parent)
        else:
          mail_message.setSourceValueList([parent, organisation])
      elif parent.getPortalType() == 'Organisation':
Jean-Paul Smets's avatar
Jean-Paul Smets committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        mail_message.setSourceValue(parent)
      break

  # We should look if there's already a sale opportunity
  # or a support wich can corresponds to this email
  subject = mail_message.getTitle()
  if subject != '' and subject is not None:
    if subject.find('Re: ')==0:
      subject = subject[len('Re: '):]
    kw = {'portal_type':'Sale Opportunity',
          'query':"SearchableText LIKE '%%%s%%'" % subject }
    result = context.portal_catalog.searchResults(**kw)
    for object in result:
      object = object.getObject()
      # Check if this sale opportunity corresponds to this client
      # If so, then we can assign this mail to the sale opportunity
      if mail_message.getSourceValue() in object.getClientValueList():
        mail_message.setFollowUpValue(object)
        mail_message.assign()


except:
  try:
    if mail_message is not None and hasattr(edit):
      mail_message.edit(title='Bad mail message received')
  except:
    pass

# the return of None indicates a success
# The return of anything else assumes that you are returning an error message
# and most MTA's will bounce that error message back to the mail sender
return None