Reports and email #13708
Replies: 3 comments 2 replies
-
I don't think there is anything special to email from Reports, I'm just using f-strings to format a message
def send_email(results, report_name, subject, header, ***@***.***"):
# Early return if there are no failures
if results._results[report_name]['failure'] == 0:
return
# Process the result log data into sections
logs = {
'failure': "",
'warning': "",
'info': "",
'success': "",
'default': ""
}
for log in results._results[report_name]['log']:
if log[1] in ['failure', 'warning', 'info', 'success']:
logs[log[1]] += f"{log[2]} : {log[4]}\n"
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = 'netbox'
msg['To'] = to
msg.set_content(f"""
{subject}
====================
{header}
Failure {results._results[report_name]['failure']}
Warning {results._results[report_name]['warning']}
Info {results._results[report_name]['info']}
Success {results._results[report_name]['success']}
Logs
------
Failure
-------
{logs['failure']}
Warning
-------
{logs['warning']}
Info
----
{logs['info']}
Success
-------
{logs['success']}
""")
mailer = smtplib.SMTP('localhost')
mailer.send_message(msg)
mailer.quit()
—
Mark Tinberg ***@***.***>
Division of Information Technology-Network Services
University of Wisconsin-Madison
…________________________________
From: Ian S. ***@***.***>
Sent: Thursday, September 7, 2023 4:08 AM
To: netbox-community/netbox ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [netbox-community/netbox] Reports and email (Discussion #13708)
Hi
After one of my reports have run, I need to send an email to my boss with the results, but currently I am just sending the self._results to myself and that is just CSV data, which I know my boss can't/won't read.
Are there other clever functions relating to formatting the output or do I need to process the self._results somehow, before putting it the email-body?
Best regards
—
Reply to this email directly, view it on GitHub<#13708>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAS7UMYYY2MJFWJKXZ3YCFTXZGFJ5ANCNFSM6AAAAAA4OSHSQE>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I could not get solution from Mr. Mark Tinberg to work either, so I have tried doing something simpler. When I issue this post_run command I get the CSV data in an email, in the form of a dictionary.
I copied that dict into a variable called report_result and tested the code in python3 to where the output was as I wanted, using print, instead of send_mail. Code for post_run is like this:
journalctl -xr just shows "Error during execution of report". Nothing useful. How do I see what is going wrong, since the code is working in "normal" python? |
Beta Was this translation helpful? Give feedback.
-
Not exactly with Reports, but I was able to use jinja2 in a custom script with a HTML template to email the results as HTML, which can be printed to PDF if need be. The relevant bits of code would look similar to:
and a very basic jinja2 HTML template would look like this:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
After one of my reports have run, I need to send an email to my boss with the results, but currently I am just sending the self._results to myself and that is just CSV data, which I know my boss can't/won't read.
Are there other clever functions relating to formatting the output or do I need to process the self._results somehow, before putting it the email-body?
Best regards
Beta Was this translation helpful? Give feedback.
All reactions