OK, I found some code which shows how to send an email with a report attached. Like I said, if you want to attach the report as PDF make sure you can print the report as PDF.
In this code I’m first searching for the reports. In other cases I made an extra field in the configuration, so the user can enter the report they want to send. When I find the report, I execute it and get the data back. This data is then added to the email.
# You need some imports here
import mimetypes
from email.encoders import encode_base64
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
from email.utils import formataddr, getaddresses
from trytond.sendmail import sendmail_transactional, SMTPDataManager
from trytond.report import get_email
def send_email(self, email=None, datamanager=None):
# do all your email stuff like getting addresses etc. You can
# find enough examples around how to do that.
# put your email together
msg = self._email(from_, to, cc, bcc, languages)
# send the email. Nothing is different from the examples.
def _email(self, from_, to, cc, bcc, languages):
pool = Pool()
# you should be thinking to put both reports into a configuration,
# so users can modify them.
# Get the report to be used for the email content
ActionReport = pool.get('ir.action.report')
action_reports = ActionReport.search([
('report_name', '=', 'project.email.testresult'),
])
assert action_reports, 'project.email.testresult not found'
EmailReport = action_reports[0]
# Get the report which will be attached as PDF
action_reports = ActionReport.search([
('report_name', '=', 'project.testresult')
])
assert action_reports, 'project.testresult not found'
TestReport = action_reports[0]
content, title = get_email(EmailReport, self, languages)
if TestReport:
msg = MIMEMultipart('mixed')
msg.attach(content)
language = list(languages)[-1]
msg.attach(self.get_mime(TestReport, self, language.code))
else:
msg = content
msg['From'] = from_
msg['To'] = ', '.join(to)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
msg['Subject'] = Header(title, 'utf-8')
msg['Auto-Submitted'] = 'auto-generated'
return msg
@classmethod
def get_mime(cls, report, record, language):
pool = Pool()
Report = pool.get(report.report_name, type='report')
with Transaction().set_context(language=language):
ext, content, _, title = Report.execute(
[record.id], {
'action_id': report.id,
})
name = name = '%s.%s' % (title, ext)
mimetype, _ = mimetypes.guess_type(name)
if mimetype:
msg = MIMENonMultipart(*mimetype.split('/'))
msg.set_payload(content)
encode_base64(msg)
else:
msg = MIMEApplication(content)
if not isinstance(name, str):
name = name.encode('utf-8')
if not isinstance(language, str):
language = language.encode('utf-8')
msg.add_header(
'Content-Disposition', 'attachment',
filename=name) # using ('utf-8', language, name) will scare gmail
# gmail doesn't display the filename
return msg
I also advice you to take a look at the trytond_notification_email-5.6.1.tar.gz or trytond_account_dunning_email-5.6.0.tar.gz. Dunning also shows how to add a report to your configuration.