2014-11-07 14:23:04 +01:00
|
|
|
# This file is part electronic_mail_wizard module for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains
|
|
|
|
# the full copyright notices and license terms.
|
2012-09-17 12:40:18 +02:00
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from email.mime.base import MIMEBase
|
2014-09-04 11:57:02 +02:00
|
|
|
from email.utils import formatdate, make_msgid
|
2015-05-25 16:27:22 +02:00
|
|
|
from email import Encoders, charset
|
2013-12-09 12:50:02 +01:00
|
|
|
from email.header import Header
|
2015-08-12 01:30:27 +02:00
|
|
|
from time import sleep
|
2013-04-26 13:04:35 +02:00
|
|
|
from trytond.model import ModelView, fields
|
2012-09-17 12:40:18 +02:00
|
|
|
from trytond.wizard import Wizard, StateTransition, StateView, Button
|
|
|
|
from trytond.transaction import Transaction
|
|
|
|
from trytond.pool import Pool
|
2015-02-18 13:17:51 +01:00
|
|
|
from trytond.pyson import Eval
|
2015-06-12 12:04:15 +02:00
|
|
|
from trytond.modules.electronic_mail_template.template import styles_dir
|
2014-01-14 17:03:00 +01:00
|
|
|
import threading
|
|
|
|
import logging
|
2015-08-12 01:30:27 +02:00
|
|
|
import mimetypes
|
2012-09-17 12:40:18 +02:00
|
|
|
|
2012-10-25 13:49:54 +02:00
|
|
|
__all__ = ['TemplateEmailStart', 'TemplateEmailResult',
|
2014-12-18 15:56:01 +01:00
|
|
|
'GenerateTemplateEmail']
|
2012-10-25 13:49:54 +02:00
|
|
|
|
2015-08-12 01:30:27 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-01-14 17:03:00 +01:00
|
|
|
|
2012-09-17 12:40:18 +02:00
|
|
|
class TemplateEmailStart(ModelView):
|
|
|
|
'Template Email Start'
|
2012-10-25 13:49:54 +02:00
|
|
|
__name__ = 'electronic.mail.wizard.templateemail.start'
|
2012-09-17 12:40:18 +02:00
|
|
|
|
|
|
|
from_ = fields.Char('From', readonly=True)
|
|
|
|
sender = fields.Char('Sender', required=True)
|
|
|
|
to = fields.Char('To', required=True)
|
|
|
|
cc = fields.Char('CC')
|
|
|
|
bcc = fields.Char('BCC')
|
2015-02-18 15:45:07 +01:00
|
|
|
subject = fields.Char('Subject', required=True,
|
|
|
|
states={
|
2015-03-02 21:52:18 +01:00
|
|
|
'readonly': Eval('multi_lang', True),
|
|
|
|
}, depends=['multi_lang'])
|
2015-02-18 15:45:07 +01:00
|
|
|
plain = fields.Text('Plain Text Body', required=True,
|
|
|
|
states={
|
2015-03-02 21:52:18 +01:00
|
|
|
'readonly': Eval('multi_lang', True),
|
|
|
|
}, depends=['multi_lang'])
|
2015-02-18 13:17:51 +01:00
|
|
|
send_html = fields.Boolean('Send HTML',
|
|
|
|
help='Send email with text and html')
|
|
|
|
html = fields.Text('HTML Text Body',
|
|
|
|
states={
|
|
|
|
'invisible': ~Eval('send_html', True),
|
|
|
|
'required': Eval('send_html', True),
|
2015-03-02 21:52:18 +01:00
|
|
|
'readonly': Eval('multi_lang', True),
|
|
|
|
}, depends=['send_html', 'multi_lang'])
|
2012-09-17 12:40:18 +02:00
|
|
|
total = fields.Integer('Total', readonly=True,
|
|
|
|
help='Total emails to send')
|
2015-02-18 13:17:51 +01:00
|
|
|
message_id = fields.Char('Message-ID')
|
|
|
|
in_reply_to = fields.Char('In Repply To')
|
2012-09-17 12:40:18 +02:00
|
|
|
template = fields.Many2One("electronic.mail.template", 'Template')
|
2015-03-02 21:52:18 +01:00
|
|
|
multi_lang = fields.Boolean('Multi Lang',
|
|
|
|
help='Email records have multi languages.')
|
2015-02-26 16:42:14 +01:00
|
|
|
queue = fields.Boolean('Queue',
|
|
|
|
help='Put these messages in the output mailbox instead of sending '
|
|
|
|
'them immediately.')
|
2015-02-18 15:45:07 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2015-03-02 21:52:18 +01:00
|
|
|
def default_multi_lang():
|
|
|
|
return False
|
2012-09-17 12:40:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TemplateEmailResult(ModelView):
|
|
|
|
'Template Email Result'
|
2012-10-25 13:49:54 +02:00
|
|
|
__name__ = 'electronic.mail.wizard.templateemail.result'
|
2012-09-17 12:40:18 +02:00
|
|
|
|
|
|
|
name = fields.Char('Name', help='Name of Header Field')
|
|
|
|
|
|
|
|
|
|
|
|
class GenerateTemplateEmail(Wizard):
|
|
|
|
"Generate Email from template"
|
2012-10-25 13:49:54 +02:00
|
|
|
__name__ = "electronic_mail_wizard.templateemail"
|
2012-09-17 12:40:18 +02:00
|
|
|
|
|
|
|
start = StateView('electronic.mail.wizard.templateemail.start',
|
|
|
|
'electronic_mail_wizard.templateemail_start', [
|
|
|
|
Button('Cancel', 'end', 'tryton-cancel'),
|
|
|
|
Button('Send', 'send', 'tryton-ok', default=True),
|
|
|
|
])
|
|
|
|
send = StateTransition()
|
|
|
|
|
2012-10-25 13:49:54 +02:00
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(GenerateTemplateEmail, cls).__setup__()
|
|
|
|
cls._error_messages.update({
|
2014-06-02 12:37:58 +02:00
|
|
|
'template_deleted': 'This template has been deactivated or '
|
|
|
|
'deleted.',
|
2012-09-17 12:55:42 +02:00
|
|
|
})
|
|
|
|
|
2014-02-13 19:20:43 +01:00
|
|
|
def default_start(self, fields):
|
|
|
|
default = self.render_fields(self.__name__)
|
|
|
|
return default
|
|
|
|
|
|
|
|
def transition_send(self):
|
|
|
|
self.render_and_send()
|
|
|
|
return 'end'
|
|
|
|
|
2012-09-17 12:40:18 +02:00
|
|
|
def render(self, template, record, values):
|
|
|
|
'''Renders the template and returns as email object
|
2012-10-25 14:33:02 +02:00
|
|
|
:param template: Object of the template
|
|
|
|
:param record: Object of the template
|
2012-09-17 12:40:18 +02:00
|
|
|
:param values: Dicctionary values
|
|
|
|
:return: 'email.message.Message' instance
|
|
|
|
'''
|
2012-10-25 13:49:54 +02:00
|
|
|
Template = Pool().get('electronic.mail.template')
|
2012-09-17 12:40:18 +02:00
|
|
|
|
2014-01-14 15:28:22 +01:00
|
|
|
message = MIMEMultipart()
|
2015-02-18 13:17:51 +01:00
|
|
|
messageid = template.eval(values['message_id'], record)
|
|
|
|
message['message_id'] = messageid or make_msgid()
|
2012-09-17 12:40:18 +02:00
|
|
|
message['date'] = formatdate(localtime=1)
|
2015-02-18 13:17:51 +01:00
|
|
|
message['in_reply_to'] = ""
|
|
|
|
if values.get('in_reply_to'):
|
|
|
|
message['in_reply_to'] = template.eval(values['in_reply_to'],
|
|
|
|
record)
|
2012-09-17 12:40:18 +02:00
|
|
|
|
|
|
|
language = Transaction().context.get('language', 'en_US')
|
|
|
|
if template.language:
|
2014-07-04 10:39:03 +02:00
|
|
|
language = template.eval(template.language, record)
|
2012-09-17 12:40:18 +02:00
|
|
|
|
2014-02-13 19:20:43 +01:00
|
|
|
with Transaction().set_context(language=language):
|
2012-10-25 14:33:02 +02:00
|
|
|
template = Template(template.id)
|
2014-07-04 10:39:03 +02:00
|
|
|
message['from'] = template.eval(values['from_'], record)
|
|
|
|
message['to'] = template.eval(values['to'], record)
|
|
|
|
message['cc'] = template.eval(values['cc'], record)
|
|
|
|
#~ message['bcc'] = template.eval(values['bcc'], record)
|
|
|
|
message['subject'] = Header(template.eval(values['subject'],
|
|
|
|
record), 'utf-8')
|
2012-09-17 12:40:18 +02:00
|
|
|
|
2015-02-18 13:17:51 +01:00
|
|
|
plain = template.eval(values['plain'], record)
|
|
|
|
|
2015-03-17 11:48:46 +01:00
|
|
|
signature_html = None
|
2015-02-18 13:17:51 +01:00
|
|
|
if template.signature:
|
|
|
|
User = Pool().get('res.user')
|
|
|
|
user = User(Transaction().user)
|
2015-03-17 11:48:46 +01:00
|
|
|
if user.signature:
|
|
|
|
signature = user.signature.encode('utf-8')
|
|
|
|
plain = '%s\n--\n%s' % (plain, signature)
|
|
|
|
if user.signature_html:
|
|
|
|
signature_html = user.signature_html.encode('utf-8')
|
2015-02-18 13:17:51 +01:00
|
|
|
|
|
|
|
# HTML
|
|
|
|
send_html = values.get('send_html')
|
|
|
|
if send_html:
|
|
|
|
html = template.eval(values['html'], record)
|
2015-03-17 11:48:46 +01:00
|
|
|
if template.signature and signature_html:
|
|
|
|
html = '%s<br/>--<br/>%s' % (html, signature_html)
|
2015-06-12 12:04:15 +02:00
|
|
|
|
|
|
|
style = ''
|
|
|
|
if template.style:
|
|
|
|
fname = '%s/%s' % (styles_dir(), template.style)
|
|
|
|
with open(fname) as f:
|
|
|
|
style = f.read()
|
|
|
|
if template.custom_style:
|
|
|
|
style += '\n%s' % template.custom_style
|
|
|
|
elif template.custom_style:
|
|
|
|
style = '%s' % template.custom_style
|
|
|
|
|
2015-02-18 13:46:46 +01:00
|
|
|
html = """
|
2015-02-18 13:17:51 +01:00
|
|
|
<html>
|
|
|
|
<head><head>
|
2015-06-12 12:04:15 +02:00
|
|
|
<style>
|
|
|
|
%s
|
|
|
|
</style>
|
2015-02-18 13:17:51 +01:00
|
|
|
<body>
|
2015-02-18 13:46:46 +01:00
|
|
|
%s
|
2015-02-18 13:17:51 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
2015-06-12 12:04:15 +02:00
|
|
|
""" % (style, html)
|
2015-02-18 13:17:51 +01:00
|
|
|
|
2015-02-18 13:46:46 +01:00
|
|
|
# MIME HTML or Text
|
2015-02-18 13:17:51 +01:00
|
|
|
if send_html:
|
|
|
|
body = MIMEMultipart('alternative')
|
|
|
|
body.attach(MIMEText(plain, 'plain', _charset='utf-8'))
|
|
|
|
body.attach(MIMEText(html, 'html', _charset='utf-8'))
|
|
|
|
message.attach(body)
|
|
|
|
else:
|
|
|
|
message.attach(MIMEText(plain, _charset='utf-8'))
|
2015-05-25 16:27:22 +02:00
|
|
|
charset.add_charset('utf-8', charset.QP, charset.QP)
|
2014-12-01 18:07:10 +01:00
|
|
|
|
2012-09-17 12:40:18 +02:00
|
|
|
# Attach reports
|
|
|
|
if template.reports:
|
2014-12-18 12:46:55 +01:00
|
|
|
reports = template.render_reports(record)
|
2012-09-17 12:40:18 +02:00
|
|
|
for report in reports:
|
|
|
|
ext, data, filename, file_name = report[0:5]
|
|
|
|
if file_name:
|
2014-07-04 10:39:03 +02:00
|
|
|
filename = template.eval(file_name, record)
|
2012-09-17 12:40:18 +02:00
|
|
|
filename = ext and '%s.%s' % (filename, ext) or filename
|
|
|
|
content_type, _ = mimetypes.guess_type(filename)
|
|
|
|
maintype, subtype = (
|
|
|
|
content_type or 'application/octet-stream'
|
|
|
|
).split('/', 1)
|
|
|
|
|
|
|
|
attachment = MIMEBase(maintype, subtype)
|
2013-11-27 14:20:53 +01:00
|
|
|
attachment.set_payload(data)
|
|
|
|
Encoders.encode_base64(attachment)
|
2012-09-17 12:40:18 +02:00
|
|
|
attachment.add_header(
|
|
|
|
'Content-Disposition', 'attachment', filename=filename)
|
2015-02-18 13:17:51 +01:00
|
|
|
attachment.add_header(
|
|
|
|
'Content-Transfer-Encoding', 'base64')
|
2012-09-17 12:40:18 +02:00
|
|
|
message.attach(attachment)
|
|
|
|
|
|
|
|
return message
|
|
|
|
|
|
|
|
def render_fields(self, name):
|
|
|
|
'''Get the fields before render and return a dicc
|
|
|
|
:param name: Str ir.action.wizard
|
|
|
|
:return: dicc
|
|
|
|
'''
|
|
|
|
default = {}
|
|
|
|
|
2015-02-18 15:45:07 +01:00
|
|
|
pool = Pool()
|
|
|
|
Wizard = pool.get('ir.action.wizard')
|
|
|
|
Template = pool.get('electronic.mail.template')
|
2012-09-17 12:40:18 +02:00
|
|
|
active_ids = Transaction().context.get('active_ids')
|
|
|
|
|
2014-06-02 12:37:58 +02:00
|
|
|
context = Transaction().context
|
|
|
|
action_id = context.get('action_id', None)
|
|
|
|
wizard = Wizard(action_id)
|
|
|
|
template = (wizard.template and wizard.template[0]
|
|
|
|
or self.raise_user_error('template_deleted'))
|
2012-09-17 12:40:18 +02:00
|
|
|
|
2015-03-02 21:52:18 +01:00
|
|
|
total = len(active_ids)
|
|
|
|
records = Pool().get(template.model.model).browse(active_ids)
|
2014-01-15 11:21:36 +01:00
|
|
|
|
2015-03-02 21:52:18 +01:00
|
|
|
# Control how many language records are selected
|
|
|
|
langs = set()
|
|
|
|
if template.language:
|
|
|
|
for record in records:
|
|
|
|
lang = template.eval(template.language, record)
|
|
|
|
langs.add(lang)
|
|
|
|
if len(langs) > 1: # not continue; multi lang is True
|
|
|
|
break
|
|
|
|
if len(langs) == 1:
|
|
|
|
lang, = langs
|
|
|
|
with Transaction().set_context(language=lang):
|
|
|
|
template = Template(template.id)
|
|
|
|
multi_lang = True if len(langs) > 1 else False
|
|
|
|
|
2012-09-17 12:40:18 +02:00
|
|
|
default['total'] = total
|
2015-03-02 21:52:18 +01:00
|
|
|
default['multi_lang'] = multi_lang
|
2015-03-02 16:41:22 +01:00
|
|
|
default['queue'] = True if template.queue else False
|
2012-09-17 12:40:18 +02:00
|
|
|
default['template'] = template.id
|
2014-02-13 19:20:43 +01:00
|
|
|
if total > 1: # show fields with tags
|
2015-03-02 21:52:18 +01:00
|
|
|
default['from_'] = template.from_
|
2015-02-18 13:17:51 +01:00
|
|
|
default['message_id'] = template.message_id
|
|
|
|
default['in_reply_to'] = template.in_reply_to
|
2012-09-17 12:40:18 +02:00
|
|
|
default['to'] = template.to
|
|
|
|
default['cc'] = template.cc
|
|
|
|
default['bcc'] = template.bcc
|
|
|
|
default['subject'] = template.subject
|
|
|
|
default['plain'] = template.plain
|
2015-02-18 13:17:51 +01:00
|
|
|
default['html'] = template.html
|
2014-02-13 19:20:43 +01:00
|
|
|
else: # show fields with rendered tags
|
2015-03-02 21:52:18 +01:00
|
|
|
record, = records
|
|
|
|
default['from_'] = template.eval(template.from_, record)
|
2015-02-18 13:17:51 +01:00
|
|
|
default['message_id'] = template.eval(template.message_id, record)
|
|
|
|
default['in_reply_to'] = template.eval(template.in_reply_to,
|
|
|
|
record)
|
2014-07-04 10:39:03 +02:00
|
|
|
default['to'] = template.eval(template.to, record)
|
|
|
|
default['cc'] = template.eval(template.cc, record)
|
|
|
|
default['bcc'] = template.eval(template.bcc, record)
|
|
|
|
default['subject'] = template.eval(template.subject,
|
2014-02-13 19:20:43 +01:00
|
|
|
record)
|
2014-07-04 10:39:03 +02:00
|
|
|
default['plain'] = template.eval(template.plain, record)
|
2015-02-18 13:17:51 +01:00
|
|
|
default['html'] = template.eval(template.html, record)
|
2012-09-17 12:40:18 +02:00
|
|
|
return default
|
|
|
|
|
2012-10-25 13:49:54 +02:00
|
|
|
def render_and_send(self):
|
2014-03-24 11:36:23 +01:00
|
|
|
pool = Pool()
|
|
|
|
Mail = pool.get('electronic.mail')
|
2015-02-18 15:45:07 +01:00
|
|
|
Template = pool.get('electronic.mail.template')
|
2015-02-24 10:30:16 +01:00
|
|
|
EmailConfiguration = pool.get('electronic.mail.configuration')
|
|
|
|
email_configuration = EmailConfiguration(1)
|
2014-03-24 11:36:23 +01:00
|
|
|
|
2012-10-25 13:49:54 +02:00
|
|
|
template = self.start.template
|
2015-02-26 16:42:14 +01:00
|
|
|
if self.start.queue:
|
2015-03-03 16:43:16 +01:00
|
|
|
mailbox = template.mailbox_outbox \
|
|
|
|
if template.mailbox_outbox else email_configuration.outbox
|
2015-02-26 16:42:14 +01:00
|
|
|
else:
|
2015-03-03 16:43:16 +01:00
|
|
|
mailbox = template.mailbox if template.mailbox \
|
|
|
|
else email_configuration.sent
|
2012-09-17 12:40:18 +02:00
|
|
|
|
2015-02-18 15:45:07 +01:00
|
|
|
active_ids = Transaction().context.get('active_ids')
|
|
|
|
total = len(active_ids)
|
2015-03-02 21:52:18 +01:00
|
|
|
multi_lang = self.start.multi_lang
|
2015-02-18 15:45:07 +01:00
|
|
|
|
2015-03-02 21:52:18 +01:00
|
|
|
electronic_emails = set()
|
2015-12-16 17:24:04 +01:00
|
|
|
activities = []
|
2015-03-02 21:52:18 +01:00
|
|
|
for record in pool.get(template.model.model).browse(active_ids):
|
2012-09-17 12:40:18 +02:00
|
|
|
values = {}
|
2015-02-18 13:17:51 +01:00
|
|
|
values['message_id'] = self.start.message_id
|
|
|
|
if self.start.in_reply_to:
|
|
|
|
values['in_reply_to'] = self.start.in_reply_to
|
2012-10-25 13:49:54 +02:00
|
|
|
values['from_'] = self.start.from_
|
|
|
|
values['to'] = self.start.to
|
|
|
|
values['cc'] = self.start.cc
|
|
|
|
values['bcc'] = self.start.bcc
|
2015-02-18 13:17:51 +01:00
|
|
|
values['send_html'] = self.start.send_html
|
2015-02-18 15:45:07 +01:00
|
|
|
|
2015-03-02 21:52:18 +01:00
|
|
|
# multi records and one language
|
|
|
|
if not multi_lang and total > 1:
|
|
|
|
values['subject'] = template.eval(self.start.subject, record)
|
|
|
|
values['plain'] = template.eval(self.start.plain, record)
|
|
|
|
values['html'] = template.eval(self.start.html, record)
|
|
|
|
# multiple records and multiple language
|
|
|
|
elif multi_lang and total > 1 and template.language:
|
2015-02-18 15:45:07 +01:00
|
|
|
language = template.eval(template.language, record)
|
|
|
|
with Transaction().set_context(language=language):
|
|
|
|
template = Template(template.id)
|
|
|
|
values['subject'] = template.eval(template.subject, record)
|
|
|
|
values['plain'] = template.eval(template.plain, record)
|
|
|
|
values['html'] = template.eval(template.html, record)
|
2015-03-02 21:52:18 +01:00
|
|
|
# multiple records and multiple language and not template language
|
|
|
|
elif multi_lang and total > 1:
|
|
|
|
values['subject'] = template.eval(template.subject, record)
|
|
|
|
values['plain'] = template.eval(template.plain, record)
|
|
|
|
values['html'] = template.eval(template.html, record)
|
|
|
|
# a simple email
|
|
|
|
else:
|
2015-02-18 15:45:07 +01:00
|
|
|
values['subject'] = self.start.subject
|
|
|
|
values['plain'] = self.start.plain
|
|
|
|
values['html'] = self.start.html
|
2013-05-13 10:02:37 +02:00
|
|
|
|
2014-03-24 11:36:23 +01:00
|
|
|
emails = []
|
2015-02-18 13:17:51 +01:00
|
|
|
if self.start.from_:
|
2014-07-04 12:27:09 +02:00
|
|
|
emails += template.eval(self.start.from_, record).split(',')
|
2014-03-24 11:36:23 +01:00
|
|
|
if self.start.to:
|
2014-07-04 12:27:09 +02:00
|
|
|
emails += template.eval(self.start.to, record).split(',')
|
2014-03-24 11:36:23 +01:00
|
|
|
if self.start.cc:
|
2014-07-04 12:27:09 +02:00
|
|
|
emails += template.eval(self.start.cc, record).split(',')
|
2014-03-24 11:36:23 +01:00
|
|
|
if self.start.bcc:
|
2014-07-04 12:27:09 +02:00
|
|
|
emails += template.eval(self.start.bcc, record).split(',')
|
2014-03-24 11:36:23 +01:00
|
|
|
|
2015-02-20 16:35:11 +01:00
|
|
|
email_message = self.render(template, record, values)
|
|
|
|
|
|
|
|
context = {}
|
|
|
|
if values.get('bcc'):
|
|
|
|
context['bcc'] = values.get('bcc')
|
|
|
|
|
2015-02-24 10:30:16 +01:00
|
|
|
electronic_email = Mail.create_from_email(email_message,
|
2015-03-03 16:43:16 +01:00
|
|
|
mailbox, context)
|
2015-07-13 10:14:54 +02:00
|
|
|
if not electronic_email: # not configured mailbox
|
|
|
|
return
|
2015-03-02 21:52:18 +01:00
|
|
|
electronic_emails.add(electronic_email)
|
|
|
|
|
2015-12-16 17:24:04 +01:00
|
|
|
activities = [{
|
|
|
|
'record': record,
|
|
|
|
'template': template,
|
|
|
|
'mail': electronic_email,
|
|
|
|
}]
|
|
|
|
|
|
|
|
if activities:
|
|
|
|
template.add_activities(activities) # add activities
|
|
|
|
|
2016-03-11 16:58:09 +01:00
|
|
|
Transaction().commit()
|
2015-03-02 21:52:18 +01:00
|
|
|
if not self.start.queue:
|
|
|
|
electronic_email_ids = list({e.id for e in electronic_emails})
|
2016-03-10 13:34:55 +01:00
|
|
|
db_name = Transaction().database.name
|
2015-03-02 21:52:18 +01:00
|
|
|
context = Transaction().context
|
|
|
|
thread1 = threading.Thread(target=self.render_and_send_thread,
|
2016-04-13 13:04:57 +02:00
|
|
|
args=(db_name, Transaction().user, electronic_email_ids, context))
|
2015-03-02 21:52:18 +01:00
|
|
|
thread1.start()
|
|
|
|
|
2016-04-13 13:04:57 +02:00
|
|
|
def render_and_send_thread(self, db_name, user_id, electronic_email_ids, context):
|
|
|
|
with Transaction().start(db_name, user_id) as transaction:
|
2014-01-14 17:03:00 +01:00
|
|
|
pool = Pool()
|
|
|
|
Email = pool.get('electronic.mail')
|
2015-02-24 10:30:16 +01:00
|
|
|
EmailConfiguration = pool.get('electronic.mail.configuration')
|
2015-03-02 16:26:46 +01:00
|
|
|
|
|
|
|
with transaction.set_context(context):
|
2015-02-24 10:30:16 +01:00
|
|
|
email_configuration = EmailConfiguration(1)
|
2015-03-02 16:26:46 +01:00
|
|
|
draft_mailbox = email_configuration.draft
|
|
|
|
|
|
|
|
sleep(5)
|
|
|
|
|
2015-03-02 21:52:18 +01:00
|
|
|
for electronic_email in Email.browse(electronic_email_ids):
|
|
|
|
success = electronic_email.send_email()
|
|
|
|
if success:
|
2015-08-12 01:30:27 +02:00
|
|
|
logger.info('Send email: %s' % (electronic_email.rec_name))
|
2015-03-02 21:52:18 +01:00
|
|
|
else:
|
|
|
|
electronic_email.mailbox = draft_mailbox
|
2015-03-03 16:43:16 +01:00
|
|
|
electronic_email.save()
|
|
|
|
|
2016-03-11 16:58:09 +01:00
|
|
|
transaction.commit()
|