+ def email(self, recipients, sender, **kwargs):
+ """
+ Generates an email with the report
+ """
+ log.debug("Sending an email from %s to %s" % (sender, recipients))
+
+ # Fetch the hostname
+ hostname = socket.gethostname()
+
+ # Create a new message
+ msg = email.message.EmailMessage()
+
+ # Set the sender
+ msg.add_header("From", sender)
+
+ # Add them to the email
+ msg.add_header("To", ", ".join(recipients))
+
+ # Set the Subject
+ msg.add_header(
+ "Subject", "[REPORT] Intrusion Prevention System Alerts from %s" % hostname,
+ ),
+
+ # Compose the content
+ content = [
+ _("To whom it may concern,"),
+ "",
+ _("The IPFire Intrusion Preventsion System is sending you the attached report."),
+ ]
+
+ # Add the content to the email
+ msg.set_content("\n".join(content))
+
+ # Generate the report & attach it to the email
+ with tempfile.NamedTemporaryFile() as f:
+ # Generate
+ self.generate(output=f.name, **kwargs)
+
+ # Attach
+ msg.add_attachment(
+ f.read(), maintype="application", subtype="pdf", filename="report.pdf",
+ )
+
+ # Show the email
+ log.debug(msg.as_string())
+
+ # Send the email
+ p = subprocess.Popen(
+ ["/usr/sbin/sendmail", "-t", "-oi", "-f", sender],
+ text=True,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ )
+
+ # Pipe the email into sendmail
+ stdout, stderr = p.communicate(msg.as_string())
+
+ if not p.returncode == 0:
+ log.error("Failed to send email. sendmail returned %s:" % p.returncode)
+ if stdout:
+ log.error(stdout)
+
+ log.debug("Successfully send email to %s" % ", ".join(recipients))
+