Commit c6743224 authored by Romain Courteaud's avatar Romain Courteaud

Use the python's email lib for sending message, instead of MimeWriter which is

deprecated.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@14576 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e8a766a7
...@@ -31,11 +31,14 @@ from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface ...@@ -31,11 +31,14 @@ 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 cStringIO import StringIO
from MimeWriter import MimeWriter
from base64 import encode
from mimetools import choose_boundary
from mimetypes import guess_type from mimetypes import guess_type
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email.Header import make_header
from email import Encoders
no_crawl_protocol_list = ['mailto', 'javascript', ] no_crawl_protocol_list = ['mailto', 'javascript', ]
no_host_protocol_list = ['mailto', 'news', 'javascript',] no_host_protocol_list = ['mailto', 'news', 'javascript',]
default_protocol_dict = { 'Email' : 'mailto', default_protocol_dict = { 'Email' : 'mailto',
...@@ -138,7 +141,8 @@ class Url(Coordinate, Base, UrlMixIn): ...@@ -138,7 +141,8 @@ class Url(Coordinate, Base, UrlMixIn):
return ("http://www.erp5.org", "mailto:info@erp5.org") return ("http://www.erp5.org", "mailto:info@erp5.org")
security.declareProtected(Permissions.UseMailhostServices, 'send') security.declareProtected(Permissions.UseMailhostServices, 'send')
def send(self, from_url=None, to_url=None, msg=None, subject=None, attachment_list=None): def send(self, from_url=None, to_url=None, msg=None,
subject=None, attachment_list=None):
""" """
This method was previously named 'SendMail' and is used to send email This method was previously named 'SendMail' and is used to send email
attachment_list is a list of dictionnary wich has keys : attachment_list is a list of dictionnary wich has keys :
...@@ -159,30 +163,22 @@ class Url(Coordinate, Base, UrlMixIn): ...@@ -159,30 +163,22 @@ class Url(Coordinate, Base, UrlMixIn):
if from_url is None or to_url is None: if from_url is None or to_url is None:
raise AttributeError, "No mail defined" raise AttributeError, "No mail defined"
if attachment_list == None:
# Create non multi-part MIME message.
message = MIMEText(msg, _charset='utf-8')
attachment_list = []
else:
# Create multi-part MIME message. # Create multi-part MIME message.
message = StringIO() message = MIMEMultipart()
writer = MimeWriter(message) message.preamble = "If you can read this, your mailreader\n" \
writer.addheader('From', from_url) "can not handle multi-part messages!\n"
writer.addheader('To', to_url) message.attach(MIMEText(msg, _charset='utf-8'))
writer.addheader('Subject', subject)
writer.addheader('MimeVersion', '1.0') message.add_header('Subject',
# Don't forget to flush the headers for Communicator make_header([(subject, 'utf-8')]).encode())
writer.flushheaders() message.add_header('From', from_url)
# Generate a unique section boundary: message.add_header('To', to_url)
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","UTF8")])
FirstPartFile.write(msg)
if attachment_list!=None:
for attachment in attachment_list: for attachment in attachment_list:
if attachment.has_key('name'): if attachment.has_key('name'):
attachment_name = attachment['name'] attachment_name = attachment['name']
...@@ -195,26 +191,19 @@ class Url(Coordinate, Base, UrlMixIn): ...@@ -195,26 +191,19 @@ class Url(Coordinate, Base, UrlMixIn):
attachment['mime_type'] = type attachment['mime_type'] = type
else: else:
attachment['mime_type'] = 'application/octet-stream' attachment['mime_type'] = 'application/octet-stream'
# attach it # attach it
submsg = writer.nextpart()
if attachment['mime_type'] == 'text/plain': if attachment['mime_type'] == 'text/plain':
attachment_file = StringIO(attachment['content'] ) part = MIMEText(attachment['content'], 'utf-8')
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: else:
# encode non-plaintext attachment in base64 # encode non-plaintext attachment in base64
attachment_file = StringIO(attachment['content'] ) part = MIMEBase(*attachment['mime_type'].split('/', 1))
submsg.addheader("Content-Transfer-Encoding", "base64") part.set_payload(attachment['content'])
submsg.flushheaders()
Encoders.encode_base64(part)
f = submsg.startbody(attachment['mime_type'] , [("name", attachment_name)]) part.add_header('Content-Disposition',
encode(attachment_file, f) 'attachment; filename=%s' % attachment_name)
# close the writer message.attach(part)
writer.lastpart()
# send mail to user # send mail to user
mailhost.send(message.getvalue(), to_url, from_url) mailhost.send(message.as_string(), 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