Skip to content

Commit

Permalink
Add create_qreu method and move vals out for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaume Florez committed Mar 22, 2018
1 parent 895b510 commit a68f9e1
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions poweremail_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,25 @@ def get_ids_from_dict(self, addresses={}):

def send_mail(self, cr, uid, ids,
addresses, subject='', body=None, payload=None, context=None):
def create_qreu(headers, payload, **kwargs):
mail = Email(**{
'subject': kwargs.get('subject'),
'from': kwargs.get('from'),
'to': kwargs.get('to'),
'cc': kwargs.get('cc'),
'body_text': kwargs.get('body_text'),
'body_html': kwargs.get('body_html')
})
for header, value in headers.items():
mail.add_header(header, value)
# Add all attachments (if any)
for file_name in payload.keys():
mail.add_attachment(
input_b64=payload[file_name],
attname=file_name
)
return mail

if body is None:
body = {}
if payload is None:
Expand All @@ -461,8 +480,21 @@ def send_mail(self, cr, uid, ids,
subject = subject or context.get('subject', '') or ''
# Try to send the e-mail from each allowed account
# Only one mail is sent
result = []
body_html = (
tools.ustr(body.get('html', '')) or
tools.ustr(body.get('text', ''))
)
if "<br/>" not in body_html and "<br>" not in body_html:
body_html = body_html.replace('\n', '<br/>')
extra_headers = context.get('headers', {})
sent_addr = []
for account_id in ids:
account = self.browse(cr, uid, account_id, context)
sender_name = account.name + " <" + account.email_id + ">"
extra_headers.update({
'Organitzation': account.company_id.name
})
# Use sender if debug is set
sender = (Sender if config.get('debug_mode', False) else SMTPSender)
with sender(
Expand All @@ -474,35 +506,18 @@ def send_mail(self, cr, uid, ids,
):
mail = Email()
try:
sender_name = account.name + " <" + account.email_id + ">"
body_html = (
tools.ustr(body.get('html', '')) or
tools.ustr(body.get('text', ''))
)
if "<br/>" not in body_html and "<br>" not in body_html:
body_html = body_html.replace('\n', '<br/>')
mail = Email(**{
'subject': subject,
'from': sender_name,
'to': addresses_list.get('To', []),
'cc': addresses_list.get('CC', []),
'bcc': addresses_list.get('BCC', []),
'body_text': tools.ustr(body.get('text', '')),
'body_html': body_html
})
for header, value in context.get('headers', {}).items():
mail.add_header(header, value)
mail.add_header(
'Organization', account.user.company_id.name
mail = create_qreu(
headers=extra_headers, payload=payload,
**{
'subject': subject,
'from': sender_name,
'to': addresses_list.get('To', []),
'cc': addresses_list.get('CC', []),
'body_text': tools.ustr(body.get('text', '')),
'body_html': body_html
}
)
mail.add_header(
'Date', formatdate()
)
# Add all attachments (if any)
for file_name in payload.keys():
mail.add_attachment(
input_b64=payload[file_name],
attname=file_name
)
except Exception as error:
logger.notifyChannel(
Expand Down

0 comments on commit a68f9e1

Please sign in to comment.