diff --git a/bin/tools/misc.py b/bin/tools/misc.py index 3df6a4bd44c..0c87a1c8aeb 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -32,11 +32,22 @@ from config import config import zipfile import release import socket +import logging import re from itertools import islice import threading from which import which +import smtplib +from email.MIMEText import MIMEText +from email.MIMEBase import MIMEBase +from email.MIMEMultipart import MIMEMultipart +from email.Header import Header +from email.Utils import formatdate, COMMASPACE +from email.Utils import formatdate, COMMASPACE +from email import Encoders +import netsvc + if sys.version_info[:2] < (2, 4): from threadinglocal import local @@ -45,6 +56,8 @@ else: from itertools import izip +_logger = logging.getLogger('tools') + # initialize a database with base/base.sql def init_db(cr): import addons @@ -399,6 +412,49 @@ def html2plaintext(html, body_id=None, encoding='utf-8'): return html +def _email_send(message, openobject_id=None, debug=False): + """Low-level method to send directly a Message through the configured smtp server. + :param message: an email.message.Message to send + :param debug: True if messages should be output to stderr before being sent, + and smtplib.SMTP put into debug mode. + :return: True if the mail was delivered successfully to the smtp, + else False (+ exception logged) + """ + if openobject_id: + msg['Message-Id'] = "<%s-openobject-%s@%s>" % (time.time(), openobject_id, socket.gethostname()) + + try: + oldstderr = smtplib.stderr + s = smtplib.SMTP() + try: + # in case of debug, the messages are printed to stderr. + if debug: + smtplib.stderr = WriteToLogger() + + s.set_debuglevel(int(bool(debug))) # 0 or 1 + s.connect(smtp_server, config['smtp_port']) + if ssl: + s.ehlo() + s.starttls() + s.ehlo() + + if config['smtp_user'] or config['smtp_password']: + s.login(config['smtp_user'], config['smtp_password']) + s.sendmail(email_from, + flatten([email_to, email_cc, email_bcc]), + msg.as_string()) + finally: + s.quit() + if debug: + smtplib.stderr = oldstderr + + except Exception, e: + _logger.error('could not deliver email', exc_info=True) + return False + + return True + + def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False, attach=None, openobject_id=False, ssl=False, debug=False, subtype='plain', x_headers=None, priority='3'): @@ -412,16 +468,6 @@ def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=Non `email_to`: a sequence of addresses to send the mail to. """ - import smtplib - from email.MIMEText import MIMEText - from email.MIMEBase import MIMEBase - from email.MIMEMultipart import MIMEMultipart - from email.Header import Header - from email.Utils import formatdate, COMMASPACE - from email.Utils import formatdate, COMMASPACE - from email import Encoders - import netsvc - if x_headers is None: x_headers = {} @@ -462,20 +508,12 @@ def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=Non msg['Bcc'] = COMMASPACE.join(email_bcc) msg['Date'] = formatdate(localtime=True) - # Add OpenERP Server information - msg['X-Generated-By'] = 'OpenERP (http://www.openerp.com)' - msg['X-OpenERP-Server-Host'] = socket.gethostname() - msg['X-OpenERP-Server-Version'] = release.version msg['X-Priority'] = priorities.get(priority, '3 (Normal)') # Add dynamic X Header for key, value in x_headers.iteritems(): - msg['X-OpenERP-%s' % key] = str(value) msg['%s' % key] = str(value) - if openobject_id: - msg['Message-Id'] = "<%s-openobject-%s@%s>" % (time.time(), openobject_id, socket.gethostname()) - if attach: msg.attach(email_text) for (fname,fcontent) in attach: @@ -504,37 +542,7 @@ def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=Non netsvc.Logger().notifyChannel('email_send (maildir)', netsvc.LOG_ERROR, e) return False - try: - oldstderr = smtplib.stderr - s = smtplib.SMTP() - try: - # in case of debug, the messages are printed to stderr. - if debug: - smtplib.stderr = WriteToLogger() - - s.set_debuglevel(int(bool(debug))) # 0 or 1 - s.connect(smtp_server, config['smtp_port']) - if ssl: - s.ehlo() - s.starttls() - s.ehlo() - - if config['smtp_user'] or config['smtp_password']: - s.login(config['smtp_user'], config['smtp_password']) - s.sendmail(email_from, - flatten([email_to, email_cc, email_bcc]), - msg.as_string() - ) - finally: - s.quit() - if debug: - smtplib.stderr = oldstderr - - except Exception, e: - netsvc.Logger().notifyChannel('email_send', netsvc.LOG_ERROR, e) - return False - - return True + return _email_send(msg, openobject_id=openobject_id, debug=debug) #---------------------------------------------------------- # SMS