lims, lims_email, lims_account_invoice, lims_sale: set email attachment name differently

This commit is contained in:
Adrián Bernardi 2021-12-23 18:13:55 -03:00
parent ae9c7c217b
commit 115c5ff020
4 changed files with 37 additions and 36 deletions

View File

@ -4,9 +4,10 @@
# the full copyright notices and license terms. # the full copyright notices and license terms.
import logging import logging
from datetime import datetime from datetime import datetime
from email import encoders
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from sql import Literal from sql import Literal
from trytond.model import Workflow, ModelView, ModelSQL, fields, Unique from trytond.model import Workflow, ModelView, ModelSQL, fields, Unique
@ -674,24 +675,23 @@ class Entry(Workflow, ModelSQL, ModelView):
if not to_addrs: if not to_addrs:
return None return None
msg = MIMEMultipart() msg = MIMEMultipart('mixed')
msg['From'] = from_addr msg['From'] = from_addr
hidden = True # TODO: HARDCODE! hidden = True # TODO: HARDCODE!
if not hidden: if not hidden:
msg['To'] = ', '.join(to_addrs) msg['To'] = ', '.join(to_addrs)
msg['Subject'] = subject msg['Subject'] = subject
msg_body = MIMEBase('text', 'plain') msg_body = MIMEText('text', 'plain')
msg_body.set_payload(body.encode('UTF-8'), 'UTF-8') msg_body.set_payload(body.encode('UTF-8'), 'UTF-8')
msg.attach(msg_body) msg.attach(msg_body)
attachment = MIMEApplication( attachment = MIMEBase('application', 'octet-stream')
attachment_data['content'], attachment.set_payload(attachment_data['content'])
Name=attachment_data['filename'], _subtype="pdf") encoders.encode_base64(attachment)
attachment.add_header('content-disposition', 'attachment', attachment.add_header('Content-Disposition', 'attachment',
filename=('utf-8', '', attachment_data['filename'])) filename=attachment_data['filename'])
msg.attach(attachment) msg.attach(attachment)
return msg return msg
def send_msg(self, from_addr, to_addrs, msg): def send_msg(self, from_addr, to_addrs, msg):

View File

@ -3,9 +3,10 @@
# The COPYRIGHT file at the top level of this repository contains # The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms. # the full copyright notices and license terms.
from datetime import datetime from datetime import datetime
from email import encoders
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from time import time from time import time
import logging import logging
@ -185,25 +186,24 @@ class Invoice(metaclass=PoolMeta):
if not to_addrs: if not to_addrs:
return None return None
msg = MIMEMultipart() msg = MIMEMultipart('mixed')
msg['From'] = from_addr msg['From'] = from_addr
hidden = True # TODO: HARDCODE! hidden = True # TODO: HARDCODE!
if not hidden: if not hidden:
msg['To'] = ', '.join(to_addrs) msg['To'] = ', '.join(to_addrs)
msg['Subject'] = subject msg['Subject'] = subject
msg_body = MIMEBase('text', 'plain') msg_body = MIMEText('text', 'plain')
msg_body.set_payload(body.encode('UTF-8'), 'UTF-8') msg_body.set_payload(body.encode('UTF-8'), 'UTF-8')
msg.attach(msg_body) msg.attach(msg_body)
for attachment_data in attachments_data: for attachment_data in attachments_data:
attachment = MIMEApplication( attachment = MIMEBase('application', 'octet-stream')
attachment_data['content'], attachment.set_payload(attachment_data['content'])
Name=attachment_data['filename'], _subtype="pdf") encoders.encode_base64(attachment)
attachment.add_header('content-disposition', 'attachment', attachment.add_header('Content-Disposition', 'attachment',
filename=('utf-8', '', attachment_data['filename'])) filename=attachment_data['filename'])
msg.attach(attachment) msg.attach(attachment)
return msg return msg
def send_msg(self, from_addr, to_addrs, msg): def send_msg(self, from_addr, to_addrs, msg):

View File

@ -3,9 +3,10 @@
# the full copyright notices and license terms. # the full copyright notices and license terms.
import logging import logging
from datetime import datetime from datetime import datetime
from email import encoders
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from string import Template from string import Template
from trytond.model import ModelSQL, ModelView, fields from trytond.model import ModelSQL, ModelView, fields
@ -578,22 +579,22 @@ class SendResultsReport(Wizard):
if not to_addrs: if not to_addrs:
return None return None
msg = MIMEMultipart() msg = MIMEMultipart('mixed')
msg['From'] = from_addr msg['From'] = from_addr
if not hide_recipients: if not hide_recipients:
msg['To'] = ', '.join(to_addrs) msg['To'] = ', '.join(to_addrs)
msg['Subject'] = subject msg['Subject'] = subject
msg_body = MIMEBase('text', 'plain') msg_body = MIMEText('text', 'plain')
msg_body.set_payload(body.encode('UTF-8'), 'UTF-8') msg_body.set_payload(body.encode('UTF-8'), 'UTF-8')
msg.attach(msg_body) msg.attach(msg_body)
for attachment_data in attachments_data: for attachment_data in attachments_data:
attachment = MIMEApplication( attachment = MIMEBase('application', 'octet-stream')
attachment_data['content'], attachment.set_payload(attachment_data['content'])
Name=attachment_data['filename'], _subtype="pdf") encoders.encode_base64(attachment)
attachment.add_header('content-disposition', 'attachment', attachment.add_header('Content-Disposition', 'attachment',
filename=('utf-8', '', attachment_data['filename'])) filename=attachment_data['filename'])
msg.attach(attachment) msg.attach(attachment)
return msg return msg

View File

@ -2,10 +2,10 @@
# The COPYRIGHT file at the top level of this repository contains # The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms. # the full copyright notices and license terms.
import logging import logging
from email import encoders
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
from trytond.model import ModelSQL, ModelView, fields from trytond.model import ModelSQL, ModelView, fields
from trytond.wizard import Wizard, StateView, StateTransition, Button from trytond.wizard import Wizard, StateView, StateTransition, Button
@ -155,22 +155,22 @@ class Sale(metaclass=PoolMeta):
if not (from_addr or to_addr): if not (from_addr or to_addr):
return None return None
msg = MIMEMultipart() msg = MIMEMultipart('mixed')
msg['From'] = from_addr msg['From'] = from_addr
msg['To'] = to_addr msg['To'] = to_addr
msg['Reply-to'] = reply_to msg['Reply-to'] = reply_to
msg['Subject'] = Header(subject, 'utf-8') msg['Subject'] = subject
msg_body = MIMEBase('text', 'plain') msg_body = MIMEText('text', 'plain')
msg_body.set_payload(body.encode('UTF-8'), 'UTF-8') msg_body.set_payload(body.encode('UTF-8'), 'UTF-8')
msg.attach(msg_body) msg.attach(msg_body)
attachment = MIMEApplication(attachment_data['content'], attachment = MIMEBase('application', 'octet-stream')
Name=attachment_data['filename'], _subtype="pdf") attachment.set_payload(attachment_data['content'])
attachment.add_header('content-disposition', 'attachment', encoders.encode_base64(attachment)
filename=('utf-8', '', attachment_data['filename'])) attachment.add_header('Content-Disposition', 'attachment',
filename=attachment_data['filename'])
msg.attach(attachment) msg.attach(attachment)
return msg return msg
@staticmethod @staticmethod