2010-08-25 11:36:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields, osv
|
2010-11-04 10:04:14 +00:00
|
|
|
import re
|
2011-07-20 08:54:43 +00:00
|
|
|
import logging
|
2010-08-25 11:36:24 +00:00
|
|
|
|
2012-01-24 13:17:05 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2011-07-20 08:54:43 +00:00
|
|
|
|
|
|
|
class partner_massmail_wizard(osv.osv_memory):
|
2010-08-25 11:36:24 +00:00
|
|
|
""" Mass Mailing """
|
|
|
|
|
2011-07-20 08:54:43 +00:00
|
|
|
_name = "partner.massmail.wizard"
|
2010-08-25 11:36:24 +00:00
|
|
|
_description = "Mass Mailing"
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'email_from': fields.char("Sender's email", size=256, required=True),
|
|
|
|
'subject': fields.char('Subject', size=256,required=True),
|
|
|
|
'text': fields.text('Message',required=True),
|
|
|
|
}
|
|
|
|
|
|
|
|
def mass_mail_send(self, cr, uid, ids, context):
|
2011-07-20 08:54:43 +00:00
|
|
|
"""Send the given mail to all partners whose ids
|
|
|
|
are present in ``context['active_ids']``, to
|
|
|
|
all addresses with an email set.
|
2010-08-25 11:36:24 +00:00
|
|
|
|
2011-07-20 08:54:43 +00:00
|
|
|
:param dict context: ``context['active_ids']``
|
|
|
|
should contain the list of
|
|
|
|
ids of the partners who should
|
|
|
|
receive the mail.
|
2010-08-25 11:36:24 +00:00
|
|
|
"""
|
|
|
|
nbr = 0
|
|
|
|
partner_pool = self.pool.get('res.partner')
|
2010-10-11 14:45:40 +00:00
|
|
|
data = self.browse(cr, uid, ids[0], context=context)
|
2010-08-25 11:36:24 +00:00
|
|
|
event_pool = self.pool.get('res.partner.event')
|
2011-07-20 08:54:43 +00:00
|
|
|
assert context['active_model'] == 'res.partner', 'This wizard must be started on a list of Partners'
|
|
|
|
active_ids = context.get('active_ids', [])
|
2010-08-25 11:36:24 +00:00
|
|
|
partners = partner_pool.browse(cr, uid, active_ids, context)
|
2011-07-20 08:54:43 +00:00
|
|
|
subtype = 'plain'
|
2010-11-04 10:04:14 +00:00
|
|
|
if re.search('(<(pre)|[pubi].*>)', data.text):
|
2011-07-20 08:54:43 +00:00
|
|
|
subtype = 'html'
|
|
|
|
ir_mail_server = self.pool.get('ir.mail_server')
|
|
|
|
emails_seen = set()
|
2010-08-25 11:36:24 +00:00
|
|
|
for partner in partners:
|
|
|
|
for adr in partner.address:
|
2011-07-20 08:54:43 +00:00
|
|
|
if adr.email and not adr.email in emails_seen:
|
|
|
|
try:
|
|
|
|
emails_seen.add(adr.email)
|
|
|
|
name = adr.name or partner.name
|
|
|
|
to = '"%s" <%s>' % (name, adr.email)
|
|
|
|
msg = ir_mail_server.build_email(data.email_from, [to], data.subject, data.text, subtype=subtype)
|
|
|
|
if ir_mail_server.send_email(cr, uid, msg):
|
|
|
|
nbr += 1
|
|
|
|
except Exception:
|
|
|
|
#ignore failed deliveries, will be logged anyway
|
|
|
|
pass
|
2010-08-25 11:36:24 +00:00
|
|
|
event_pool.create(cr, uid,
|
2010-10-11 14:45:40 +00:00
|
|
|
{'name': 'Email(s) sent through mass mailing',
|
2010-08-25 11:36:24 +00:00
|
|
|
'partner_id': partner.id,
|
|
|
|
'description': data.text })
|
2011-07-20 08:54:43 +00:00
|
|
|
_logger.info('Mass-mailing wizard sent %s emails', nbr)
|
2010-08-25 11:36:24 +00:00
|
|
|
return {'email_sent': nbr}
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|