Commit 50619365 authored by Aurel's avatar Aurel

rewrite send method to allow to used attachment file


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6578 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a5f23b7c
...@@ -27,69 +27,133 @@ ...@@ -27,69 +27,133 @@
############################################################################## ##############################################################################
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5Type.Base import Base from Products.ERP5Type.Base import Base
from Products.ERP5.Document.Coordinate import Coordinate from Products.ERP5.Document.Coordinate import Coordinate
from cStringIO import StringIO
from MimeWriter import MimeWriter
from base64 import encode
from mimetools import choose_boundary
from mimetypes import guess_type
class Url(Coordinate, Base): class Url(Coordinate, Base):
""" """
A Url is allows to represent in a standard way coordinates A Url is allows to represent in a standard way coordinates
such as web sites, emails, ftp sites, etc. such as web sites, emails, ftp sites, etc.
""" """
meta_type = 'ERP5 Url' meta_type = 'ERP5 Url'
portal_type = 'Url' portal_type = 'Url'
add_permission = Permissions.AddPortalContent add_permission = Permissions.AddPortalContent
isPortalContent = 1 isPortalContent = 1
isRADContent = 1 isRADContent = 1
# Declarative security # Declarative security
security = ClassSecurityInfo() security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation) security.declareObjectProtected(Permissions.AccessContentsInformation)
# Default Properties # Default Properties
property_sheets = ( PropertySheet.Base property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem , PropertySheet.SimpleItem
, PropertySheet.Url , PropertySheet.Url
) )
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'asText') 'asText')
def asText(self): def asText(self):
return self.url_string return self.url_string
security.declareProtected(Permissions.ModifyPortalContent, 'fromText') security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
def fromText(self, text): def fromText(self, text):
self.url_string = text self.url_string = text
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'standardTextFormat') 'standardTextFormat')
def standardTextFormat(self): def standardTextFormat(self):
""" """
Returns the standard text formats for urls Returns the standard text formats for urls
""" """
return ("http://www.erp5.org","mailto:info@erp5.org") return ("http://www.erp5.org","mailto:info@erp5.org")
def send(self, from_url=None, to_url=None, msg=None, subject=None):
""" def send(self, from_url=None, to_url=None, msg=None, subject=None, attachment_list=None):
This method was previously named 'SendMail' """
This method was previously named 'SendMail' and is used to send email
attachment_list is a list of dictionnary wich has keys :
Send An Email - name : name of the attachment,
""" - content: data of the attachment
# If no URL is specified, use ourselves - mime_type: mime-type corresponding to the attachment
if from_url == None: """
from_url = self.getUrlString() # get the mailhost object
if to_url == None: try:
to_url = self.getUrlString() mailhost=self.getPortalObject().MailHost
if to_url.find('@')>=0: # We will send an email except:
if msg is not None and subject is not None: raise AttributeError, "Cannot find a Mail Host object"
header = "From: %s\n" % from_url else:
header += "To: %s\n" % to_url if from_url is None:
header += "Subject: %s\n" % subject from_url = self.getUrlString(None)
header += "\n" if to_url is None:
msg = header + msg to_url = self.getUrlString(None)
self.getPortalObject().MailHost.send( msg ) if from_url is None or to_url is None:
raise AttributeError, "No mail defined"
# Create multi-part MIME message.
message = StringIO()
writer = MimeWriter(message)
writer.addheader('From', from_url)
writer.addheader('To', to_url)
writer.addheader('Subject', subject)
writer.addheader('MimeVersion', '1.0')
# Don't forget to flush the headers for Communicator
writer.flushheaders()
# Generate a unique section boundary:
outer_boundary = choose_boundary()
# Start the main message body. Write a brief message
# for non-MIME-capable readers:
dummy_file=writer.startmultipartbody("mixed",outer_boundary)
dummy_file.write("If you can read this, your mailreader\n")
dummy_file.write("can not handle multi-part messages!\n")
submsg = writer.nextpart()
submsg.addheader("Content-Transfer-Encoding", "7bit")
FirstPartFile=submsg.startbody("text/plain", [("charset","US-ASCII")])
FirstPartFile.write(msg)
if attachment_list!=None:
for attachment in attachment_list:
if attachment.has_key('name'):
attachment_name = attachment['name']
else:
attachment_name = ''
# try to guess the mime type
if not attachment.has_key('mime_type'):
type, encoding = guess_type( attachment_name )
if type != None:
attachment['mime_type'] = type
else:
attachment['mime_type'] = 'application/octet-stream'
# attach it
submsg = writer.nextpart()
if attachment['mime_type'] == 'text/plain':
attachment_file = StringIO(attachment['content'] )
submsg.addheader("Content-Transfer-Encoding", "7bit")
submsg.addheader("Content-Disposition", "attachment;\nfilename="+attachment_name)
submsg.flushheaders()
f = submsg.startbody(attachment['mime_type'] , [("name", attachment_name)])
f.write(attachment_file.getvalue())
else:
# encode non-plaintext attachment in base64
attachment_file = StringIO(attachment['content'] )
submsg.addheader("Content-Transfer-Encoding", "base64")
submsg.flushheaders()
f = submsg.startbody(attachment['mime_type'] , [("name", attachment_name)])
encode(attachment_file, f)
# close the writer
writer.lastpart()
# send mail to user
mailhost.send(message.getvalue(), to_url, from_url)
return None
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment