[MERGE] merged ysa-email-server branch
bzr revid: hmo@tinyerp.com-20110214091053-14vrxy08ibq6jlku
This commit is contained in:
commit
ed13fcdd62
|
@ -159,7 +159,7 @@ def exec_pg_command(name, *args):
|
||||||
if not prog:
|
if not prog:
|
||||||
raise Exception('Couldn\'t find %s' % name)
|
raise Exception('Couldn\'t find %s' % name)
|
||||||
args2 = (prog,) + args
|
args2 = (prog,) + args
|
||||||
|
|
||||||
return subprocess.call(args2)
|
return subprocess.call(args2)
|
||||||
|
|
||||||
def exec_pg_command_pipe(name, *args):
|
def exec_pg_command_pipe(name, *args):
|
||||||
|
@ -426,7 +426,40 @@ def generate_tracking_message_id(openobject_id):
|
||||||
"""
|
"""
|
||||||
return "<%s-openobject-%s@%s>" % (time.time(), openobject_id, socket.gethostname())
|
return "<%s-openobject-%s@%s>" % (time.time(), openobject_id, socket.gethostname())
|
||||||
|
|
||||||
def _email_send(smtp_from, smtp_to_list, message, openobject_id=None, ssl=False, debug=False):
|
def connect_smtp_server(server_host, server_port, user_name=None, user_password=None, ssl=False, tls=False, debug=False):
|
||||||
|
"""
|
||||||
|
Connect SMTP Server and returned the (SMTP) object
|
||||||
|
"""
|
||||||
|
smtp_server = None
|
||||||
|
try:
|
||||||
|
if ssl:
|
||||||
|
# In Python 2.6
|
||||||
|
smtp_server = smtplib.SMTP_SSL(server_host, server_port)
|
||||||
|
else:
|
||||||
|
smtp_server = smtplib.SMTP(server_host, server_port)
|
||||||
|
|
||||||
|
smtp_server.set_debuglevel(int(bool(debug))) # 0 or 1
|
||||||
|
|
||||||
|
|
||||||
|
if tls:
|
||||||
|
smtp_server.ehlo()
|
||||||
|
smtp_server.starttls()
|
||||||
|
smtp_server.ehlo()
|
||||||
|
|
||||||
|
#smtp_server.connect(server_host, server_port)
|
||||||
|
|
||||||
|
if smtp_server.has_extn('AUTH') or user_name or user_password:
|
||||||
|
smtp_server.login(user_name, user_password)
|
||||||
|
|
||||||
|
|
||||||
|
except Exception, error:
|
||||||
|
_logger.error('Could not connect to smtp server : %s' %(error), exc_info=True)
|
||||||
|
raise error
|
||||||
|
return smtp_server
|
||||||
|
|
||||||
|
|
||||||
|
def _email_send(smtp_from, smtp_to_list, message, openobject_id=None, ssl=False, debug=False,
|
||||||
|
smtp_server=None, smtp_port=None, smtp_user=None, smtp_password=None):
|
||||||
"""Low-level method to send directly a Message through the configured smtp server.
|
"""Low-level method to send directly a Message through the configured smtp server.
|
||||||
:param smtp_from: RFC-822 envelope FROM (not displayed to recipient)
|
:param smtp_from: RFC-822 envelope FROM (not displayed to recipient)
|
||||||
:param smtp_to_list: RFC-822 envelope RCPT_TOs (not displayed to recipient)
|
:param smtp_to_list: RFC-822 envelope RCPT_TOs (not displayed to recipient)
|
||||||
|
@ -447,7 +480,7 @@ def _email_send(smtp_from, smtp_to_list, message, openobject_id=None, ssl=False,
|
||||||
message['Message-Id'] = generate_tracking_message_id(openobject_id)
|
message['Message-Id'] = generate_tracking_message_id(openobject_id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
smtp_server = config['smtp_server']
|
smtp_server = smtp_server or config['smtp_server']
|
||||||
|
|
||||||
if smtp_server.startswith('maildir:/'):
|
if smtp_server.startswith('maildir:/'):
|
||||||
from mailbox import Maildir
|
from mailbox import Maildir
|
||||||
|
@ -456,43 +489,36 @@ def _email_send(smtp_from, smtp_to_list, message, openobject_id=None, ssl=False,
|
||||||
mdir.add(message.as_string(True))
|
mdir.add(message.as_string(True))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
oldstderr = smtplib.stderr
|
if debug:
|
||||||
|
oldstderr = smtplib.stderr
|
||||||
|
smtplib.stderr = WriteToLogger()
|
||||||
|
|
||||||
if not ssl: ssl = config.get('smtp_ssl', False)
|
if not ssl: ssl = config.get('smtp_ssl', False)
|
||||||
s = smtplib.SMTP()
|
smtp = connect_smtp_server(smtp_server, smtp_port, smtp_user, smtp_password, ssl=ssl, tls=True, debug=debug)
|
||||||
try:
|
try:
|
||||||
# in case of debug, the messages are printed to stderr.
|
smtp.sendmail(smtp_from, smtp_to_list, message.as_string())
|
||||||
if debug:
|
except Exception:
|
||||||
smtplib.stderr = WriteToLogger()
|
_logger.error('could not deliver Email(s)', exc_info=True)
|
||||||
|
return False
|
||||||
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(smtp_from, smtp_to_list, message.as_string())
|
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
s.quit()
|
smtp.quit()
|
||||||
if debug:
|
|
||||||
smtplib.stderr = oldstderr
|
|
||||||
except Exception:
|
except Exception:
|
||||||
# ignored, just a consequence of the previous exception
|
# ignored, just a consequence of the previous exception
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if debug:
|
||||||
|
smtplib.stderr = oldstderr
|
||||||
except Exception:
|
except Exception:
|
||||||
_logger.error('could not deliver email', exc_info=True)
|
_logger.error('Error on Send Emails Services', exc_info=True)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False,
|
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'):
|
attach=None, openobject_id=False, debug=False, subtype='plain', x_headers=None, priority='3',
|
||||||
|
smtp_email_from=None, smtp_server=None, smtp_port=None, ssl=False, smtp_user=None, smtp_password=None):
|
||||||
|
|
||||||
"""Send an email.
|
"""Send an email.
|
||||||
|
|
||||||
|
@ -508,11 +534,11 @@ def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=Non
|
||||||
x_headers = {}
|
x_headers = {}
|
||||||
|
|
||||||
|
|
||||||
if not (email_from or config['email_from']):
|
if not (email_from or smtp_email_from or config['email_from']):
|
||||||
raise ValueError("Sending an email requires either providing a sender "
|
raise ValueError("Sending an email requires either providing a sender "
|
||||||
"address or having configured one")
|
"address or having configured one")
|
||||||
|
|
||||||
if not email_from: email_from = config.get('email_from', False)
|
if not email_from: email_from = smtp_email_from or config.get('email_from', False)
|
||||||
email_from = ustr(email_from).encode('utf-8')
|
email_from = ustr(email_from).encode('utf-8')
|
||||||
|
|
||||||
if not email_cc: email_cc = []
|
if not email_cc: email_cc = []
|
||||||
|
@ -561,7 +587,8 @@ def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=Non
|
||||||
part.add_header('Content-Disposition', 'attachment; filename="%s"' % (fname,))
|
part.add_header('Content-Disposition', 'attachment; filename="%s"' % (fname,))
|
||||||
msg.attach(part)
|
msg.attach(part)
|
||||||
|
|
||||||
return _email_send(email_from, flatten([email_to, email_cc, email_bcc]), msg, openobject_id=openobject_id, ssl=ssl, debug=debug)
|
return _email_send(email_from, flatten([email_to, email_cc, email_bcc]), msg, openobject_id=openobject_id, ssl=ssl, debug=debug,
|
||||||
|
smtp_server=smtp_server, smtp_port=smtp_port, smtp_user=smtp_user, smtp_password=smtp_password)
|
||||||
|
|
||||||
#----------------------------------------------------------
|
#----------------------------------------------------------
|
||||||
# SMS
|
# SMS
|
||||||
|
|
Loading…
Reference in New Issue