[REVIEW+MERGE] merged work

bzr revid: hmo@tinyerp.com-20110404060015-pku4bh49l0g5eamf
This commit is contained in:
Harry (OpenERP)
2011-04-04 11:30:15 +05:30
4 changed files with 46 additions and 40 deletions
+2 -2
View File
@@ -632,8 +632,8 @@ class actions_server(osv.osv):
body = self.merge_message(cr, uid, action.message, action, context)
smtp_server_pool = self.pool.get('ir.mail_server')
msg = smtp_server_pool.pack_message(cr, uid, subject, body, subtype='html')
res_email = smtp_server_pool.send_email(cr, uid, user, [address], msg, debug=False)
msg = smtp_server_pool.pack_message(cr, uid, user, [address], subject, body, subtype='html')
res_email = smtp_server_pool.send_email(cr, uid, msg, debug=False)
if res_email:
logger.info('Email successfully sent to: %s', address)
else:
+34 -22
View File
@@ -124,14 +124,14 @@ class ir_mail_server(osv.osv):
except Exception, error:
raise osv.except_osv(
_("SMTP Connection: Test failed"),
_("Reason: %s") % error
finally: )
_("Reason: %s") % error )
finally:
try:
if smtp:smtp.quit()
except Exception:
# ignored, just a consequence of the previous exception
pass
raise osv.except_osv(_("SMTP Connection: Test Successfully!"), '')
def connect_smtp_server(self, server_host, server_port, user_name=None,
@@ -166,13 +166,20 @@ class ir_mail_server(osv.osv):
raise error
return smtp_server
def pack_message(self, cr, uid, subject, body, email_cc=None, email_bcc=None, reply_to=False,
def pack_message(self, cr, uid, email_from, email_to, subject, body, email_cc=None, email_bcc=None, reply_to=False,
attach=None, message_id=None, references=None, openobject_id=False, debug=False, subtype='plain', x_headers=None, priority='3'):
"""
Pack all message attributes into one object.
Return email.message object after packed all email attribure.
"""
if not (email_from or config.get('email_from', False)):
raise ValueError("Sending an email requires either providing a sender "
"address or having configured one")
if not email_from: email_from = config.get('email_from', False)
email_from = tools.ustr(email_from).encode('utf-8')
if x_headers is None:
x_headers = {}
@@ -181,7 +188,7 @@ class ir_mail_server(osv.osv):
if not body: body = u''
email_body = ustr(body).encode('utf-8')
email_text = MIMEText(email_body or '',_subtype=subtype,_charset='utf-8')
email_text = MIMEText(email_body or '', _subtype=subtype,_charset='utf-8')
msg = MIMEMultipart()
if not message_id and openobject_id:
@@ -221,7 +228,7 @@ class ir_mail_server(osv.osv):
msg.attach(email_text)
if attach:
for (fname,fcontent) in attach:
for (fname, fcontent) in attach:
part = MIMEBase('application', "octet-stream")
part.set_payload( fcontent )
Encoders.encode_base64(part)
@@ -229,21 +236,26 @@ class ir_mail_server(osv.osv):
msg.attach(part)
return msg
def send_email(self, cr, uid, smtp_from, smtp_to_list, message,
mail_server_id=None, smtp_server=None, smtp_port=None,
def send_email(self, cr, uid, message,
mail_server_id=None, smtp_server=None, smtp_port=None,
smtp_user=None, smtp_password=None, ssl=False, tls=True, debug=False):
"""Send an email.
If the id of a mail server is provided, send using this mail server, ignoring other smtp_* arguments.
@message : The Object of 'email.mime.base.MIMEBase' Class
If the mail_server_id is provided, send using this mail server, ignoring other smtp_* arguments.
If mail_server_id == None and smtp_server == None, use the default mail server (highest priority).
If mail_server_id == None and smtp_server is not None, use the provided smtp_* arguments.
Return messageID of message if Successfully sent Email otherwise return False
Return messageID of message if Successfully sent Email otherwise return False
"""
if not (smtp_from or config.get('email_from', False)):
smtp_from = message['From']
if not smtp_from:
raise ValueError("Sending an email requires either providing a sender "
"address or having configured one")
if not smtp_from: smtp_from = config.get('email_from', False)
smtp_from = tools.ustr(smtp_from).encode('utf-8')
email_to = message['To']
email_cc = message['Cc']
email_bcc = message['Bcc']
smtp_to_list = tools.flatten([email_to, email_cc, email_bcc])
# Get SMTP Server Details from Mail Server
mail_server = False
@@ -251,7 +263,7 @@ class ir_mail_server(osv.osv):
mail_server = self.browse(cr, uid, mail_server_id)
elif not (mail_server_id and smtp_server):
mail_server_ids = self.search(cr, uid, [], order='priority', limit=1)
mail_server = self.browse(cr, uid, server_ids[0])
mail_server = self.browse(cr, uid, mail_server_ids[0])
if mail_server:
smtp_server = mail_server.smtp_host
smtp_user = mail_server.smtp_user
@@ -267,16 +279,16 @@ class ir_mail_server(osv.osv):
def write(self, s):
self.logger.notifyChannel('email_send', loglevels.LOG_DEBUG, s)
try:
message_id = message['Message-Id']
smtp_server = smtp_server or config.get('smtp_server')
# Add email in Maildir if smtp_server contains maildir.
# Add email in Maildir if smtp_server contains maildir.
if smtp_server.startswith('maildir:/'):
from mailbox import Maildir
maildir_path = smtp_server[8:]
mdir = Maildir(maildir_path,factory=None, create = True)
mdir = Maildir(maildir_path, factory=None, create = True)
mdir.add(message.as_string(True))
return message_id
@@ -286,15 +298,15 @@ class ir_mail_server(osv.osv):
# Open Connection of SMTP Server
smtp = self.connect_smtp_server(
smtp_server,
smtp_server,
smtp_port or config.get('smtp_port', 25),
user_name=smtp_user or config,get('smtp_user', False),
user_password=smtp_password or config.get('smtp_password', False),
ssl=ssl or config.get('smtp_ssl', False),
user_name=smtp_user or config.get('smtp_user', False),
user_password=smtp_password or config.get('smtp_password', False),
ssl=ssl or config.get('smtp_ssl', False),
tls=tls, debug=debug)
try:
# Send Email
smtp.sendmail(smtp_from, email_to, message.as_string())
smtp.sendmail(smtp_from, smtp_to_list, message.as_string())
except Exception:
_logger.error('could not deliver Email(s)', exc_info=True)
return False
@@ -63,8 +63,8 @@ class partner_wizard_spam(osv.osv_memory):
#TODO: add some tests to check for invalid email addresses
#CHECKME: maybe we should use res.partner/email_send
smtp_server_pool = self.pool.get('ir.mail_server')
msg = smtp_server_pool.pack_message(cr, uid, data.subject, data.text, subtype=type_)
smtp_server_pool.send_email(cr, uid, data.email_from, [to], msg)
msg = smtp_server_pool.pack_message(cr, uid, data.email_from, [to], data.subject, data.text, subtype=type_)
smtp_server_pool.send_email(cr, uid, msg)
nbr += 1
event_pool.create(cr, uid,
{'name': 'Email(s) sent through mass mailing',
+8 -14
View File
@@ -449,30 +449,24 @@ def email_send(email_from, email_to, subject, body, email_cc=None, email_bcc=Non
if not uid:
uid = 1
if not (email_from or config['email_from']):
raise ValueError("Sending an email requires either providing a sender "
"address or having configured one")
if not email_from: email_from = config.get('email_from', False)
email_from = ustr(email_from).encode('utf-8')
mail_server_pool = pooler.get_pool(cr.dbname).get('ir.mail_server')
# Pack Message
msg = mail_server_pool.pack_message(cr, uid, subject, body, email_cc, email_bcc, reply_to,
# Pack Message into MIME Object
msg = mail_server_pool.pack_message(cr, uid, email_from, email_to, subject, body, email_cc, email_bcc, reply_to,
attach, message_id, references, openobject_id, debug, subtype, x_headers, priority)
# Send Email
# Send Email
res = False
try:
res = mail_server_pool.send_email(cr, uid, email_from, flatten([email_to, email_cc, email_bcc]), msg, ssl=ssl, debug=debug,
smtp_server=smtp_server, smtp_port=smtp_port, smtp_user=smtp_user, smtp_password=smtp_password)
res = mail_server_pool.send_email(cr, uid, msg,
smtp_server=smtp_server, smtp_port=smtp_port, smtp_user=smtp_user, smtp_password=smtp_password,
ssl=ssl, tls=True, debug=debug)
except Exception:
return False
finally:
cr.close()
return res
#----------------------------------------------------------
# SMS